The question is a duplicate in concept, but the error is articulated differently. Unresolved symbols should be understood as undefined reference. If you are new to C++, please take the time to read the errors generated by my code.
I am taking a course in C++ and OOP, and I need some help understanding what I am doing wrong and what I can do to correct it. I have a code skeleton, where I cannot change class members by changing them from static to non-static. In my current hand-out, I need to create an object of the first class in a second class, and call a method in the first class. I cannot move class members from private to public or public to private, and I cannot change them from static to non-static. My goal is to write methods in the second class that uses the setters and getters from the first class, using an object in the second class, and to call the methods in the second class in int main().
Here is a basic idea of what I have written in attempting what I described.
#include <iostream>
using namespace std;
class foo
{
private:
static string goo;
static long int doo;
public:
static void setGoo(string gooString)
{
goo = gooString;
}
static void setDoo(long int dooLongInt)
{
doo = dooLongInt;
}
static string getGoo()
{
return goo;
}
static long int getDoo()
{
return doo;
}
};
class moo
{
private:
foo fooObj;
public:
void setGoo(string gooString)
{
fooObj.setGoo(gooString);
}
void setDoo(long int dooLongInt)
{
fooObj.setDoo(dooLongInt);
}
string getGoo()
{
return fooObj.getGoo();
}
long int getDoo()
{
return fooObj.getDoo();
}
};
int main()
{
moo mainMoo;
string text = "Text";
mainMoo.setGoo(text);
mainMoo.setDoo(123);
cout << mainMoo.getGoo();
cout << mainMoo.getDoo();
return 0;
};
I get these errors when I compile it.
1>Source.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > foo::goo" (?goo@foo@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
1>Source.obj : error LNK2001: unresolved external symbol "private: static long foo::doo" (?doo@foo@@0JA)
1>C:\Users\ipyra\OneDrive\Documents\edx hands-on projects\Static class private member initialization\x64\Debug\Static class private member initialization.exe : fatal error LNK1120: 2 unresolved externals