Link to home
Start Free TrialLog in
Avatar of BofADev
BofADev

asked on

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;
}
ASKER CERTIFIED SOLUTION
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BofADev
BofADev

ASKER

This makes sense now.  So the reason I have to decare dblLicenseFee and not the other members is because it is static?
Avatar of BofADev

ASKER

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.
Avatar of BofADev

ASKER

Ok I moved it after the class and it works.  Interesting that it matters where you put things :)
> 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.