Unresolved external symbol error in c++

前端 未结 2 1116
甜味超标
甜味超标 2021-01-27 10:27

I am trying to do a simple hw problem involving namespace, static data members and functions. I am getting an unresolved external symbol error

Error   1   erro         


        
相关标签:
2条回答
  • 2021-01-27 10:40

    you need a line in your cpp file

    double JWong::SavingsAccount::annualInterestRate = 0.7;  // or whatever you like 
    
    0 讨论(0)
  • 2021-01-27 11:02

    I'm presuming you are actually compiling the .cpp file (because the other functions link).

    The error is likely due to not defining the annualInterestRate static variable.

    You have declared it (in the class header), but it's not defined. In your cpp file add:

    // static member definition
    double JWang::SavingsAccount::annualInterestRate = ...;
    

    See an article highlighting the difference between declaration and definition of static members.

    Section 9.4.2 of the C++ Standard says "The definition for a static data member shall appear in a namespace scope enclosing the member’s class definition."

    0 讨论(0)
提交回复
热议问题