Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

Getting a linker error I don't understand

Can someone tell me why I'm getting a error LNK2001: unresolved external symbol "private: static double Dog::dblLicenseFee"(?dblLicenseFee@Dog@@0NA) with the below code.

#include <iostream>
#include <string>
using namespace std;

class Dog {
      public:
            Dog();
            void SetNameBreedAge(string, string, int);
            void DisplayNameBreedAge();
      private:
            string strName, strBreed;
            int intAge;
            static double dblLicenseFee;
};

Dog::Dog(){
      strName = "";
      strBreed = "";
      intAge = 0;
      dblLicenseFee = 12.25;
}

void Dog::DisplayNameBreedAge(){
      cout << strName << strBreed << intAge << dblLicenseFee << endl;
}

void Dog::SetNameBreedAge(string strName, string strBreed, int intAge){
      
}

int main(){
      Dog myDog;
      myDog.SetNameBreedAge ("Fluffy", "Lab", 1);
      myDog.DisplayNameBreedAge();
      return 0;
}
0
BofADev
Asked:
BofADev
  • 3
  • 2
1 Solution
 
efnCommented:
You declared Dog::dblLicenseFee but did not define it, so you said it exists somewhere, but did not allocate storage for it.  You can do that by adding a definition:

double Dog::dblLicenseFee;

If you separate the class declaration from the implementation, this definition should go in the implementation file, not the header file with the class declaration.
0
 
BofADevAuthor Commented:
This makes sense now.  So the reason I have to decare dblLicenseFee and not the other members is because it is static?
0
 
BofADevAuthor Commented:
Ok I put double Dog::dblLicenseFee; right between using namespace std; and class Dog and now it is telling me that Dog : is not a class or namespace.
0
 
BofADevAuthor Commented:
Ok I moved it after the class and it works.  Interesting that it matters where you put things :)
0
 
efnCommented:
> So the reason I have to decare dblLicenseFee and not the other members is because it is static?

Yes.  The non-static members get storage allocated when you create an object of the class, but a static member is independent of any instance, so you have to allocate it separately.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now