如何使用C ++ CLI文字说明符?

时间:2014-08-23 04:19:31

标签: c++-cli clr

这是我的班级。我使用'文字'在线路上收到错误。修饰符用于声明成员类型' 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;
};

当我尝试引用类型'名称'时,我收到了错误,例如:

Console::Write("Card name is ");
Console::WriteLine(CreditCardAccount::name);

错误:

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

1 个答案:

答案 0 :(得分:3)

我也遇到了这个问题,正确的写作方式应该是:

literal System::String^ name = "Super Platinum Card";