0

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
  • 1
    @Borgleader Please avoid using https://stackoverflow.com/questions/12573816/ as a duplicate target when the question is about a specific instance of an undefined reference. There are canonical targets for every single one of those instances, and those should be used instead. – cigien Jul 22 '22 at 02:22
  • Unfortunately, the question was closed. But I want to thank you @cigien for recognizing this question is about an undefined reference, and other questions were not specific enough to help me learn C++. – Peter Smedskjaer Jul 22 '22 at 03:09
  • To clarify, I voted to close the question as well. I just picked a duplicate target that's the same as what you're asking. You should be able to see the duplicate target at the top of your question. – cigien Jul 22 '22 at 03:13

1 Answers1

1

Class static members need to initialize outside a class, that is the reason of link error.

class foo
{
private:
    static string goo;
    static long int doo;
public:
    // .... other code
};
string foo::goo = "";
long int foo::doo = 0;

Use inline keyword in C++ 17, we can defined static member inside class. http://en.cppreference.com/w/cpp/language/static

Another similar question How to initialize private static members in C++?

Robin L
  • 125
  • 8