这是我的班级。我在使用“literal”修饰符声明成员类型“name”时遇到错误。
ref class CreditCardAccount
{
public:
static CreditCardAccount ();
CreditCardAccount (long number, double limit);
void SetCreditCardLimit (double amount);
bool MakePurchase (double amount);
void MakeRepayment (double amount);
void PrintStatement ();
long GetAccountNumber ();
static short GetNumOfAccounts ();
literal String name = "Super Platinum Card";
private:
initonly long accountNumber;
double currentBalance;
double creditLimit;
static short numOfAccounts;
static double interestRate;
};
Run Code Online (Sandbox Code Playgroud)
当我尝试引用类型“名称”时出现错误,例如:
Console::Write("Card name is ");
Console::WriteLine(CreditCardAccount::name);
Run Code Online (Sandbox Code Playgroud)
错误:
error C2146: syntax error : missing ';' before identifier 'String'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2146: syntax error : missing ';' before identifier 'name'
error C3845: 'CreditCardAccount::name': only static data members can be initialized inside a ref class or value type
Run Code Online (Sandbox Code Playgroud)
我也遇到了这个问题,正确的写法应该是:
literal System::String^ name = "Super Platinum Card";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
882 次 |
| 最近记录: |