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

问题描述 投票:0回答:1

这是我的课。我在线上使用'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;
};

我尝试引用“名称”类型时遇到错误,例如:

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
c++-cli clr
1个回答
3
投票

我也遇到了麻烦,正确的写作方式应该是:

literal System::String^ name = "Super Platinum Card";
© www.soinside.com 2019 - 2024. All rights reserved.