Search code examples
c++-cliclr

How to use C++ CLI literal specifier?


Here is my class. I'm getting errors on the line using the 'literal' modifier to declare the member type '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;
};

I'm getting errors when I try to reference the type 'name', for example:

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

Errors:

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

Solution

  • I had trouble with this too, the correct way to write should be:

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