C++ unresolved external symbol

17,293

Solution 1

static string name;

As it is static, this line only declares name - you need to define it too. Simply place this below your class definition:

string CPractice::name;

If you end up moving your class to a corresponding header and implementation file, make sure you place this definition in the implementation file. It should only be defined in a single translation unit.

Solution 2

I think you're trying to compile with gcc, when you should be compiling with g++. See What is the difference between g++ and gcc? for more on this.

You also need to add string CPractice::name; below your class definition.

Solution 3

You only declared name in the class, static variables need to be defined like so outside of the class:

string CPractice::name ="hello" ;

Solution 4

Since name is a static data member you should initialize it :) and not count on the default instance related constructor.

Add this after the class definitions (yep, I know its confusing since your member is a private one, but this is only an initialization) :

string CPractice::name;
Share:
17,293
Silvio Marijic
Author by

Silvio Marijic

Updated on June 04, 2022

Comments

  • Silvio Marijic
    Silvio Marijic almost 2 years

    Hi iam begginer at c++ i have class with static methods and i cant access them it throws me an error

        1>------ Build started: Project: CPractice, Configuration: Debug Win32 ------
    1>  Source.cpp
    1>Source.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > CPractice::name" (?name@CPractice@@0V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@A)
    1>c:\users\innersoft\documents\visual studio 2012\Projects\CPractice\Debug\CPractice.exe : fatal error LNK1120: 1 unresolved externals
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    

    and here is my code

    #include <iostream>
    #include <stdio.h>
    #include <cstdlib>
    #include <string>
    
    using namespace std;
    
    class CPractice
    {
        public:
            static void setName(string s)
            {
                name = s;
            }
            static string getName()
            {
                return name;
            }
        private:
            static string name;
    };
    
    
    int main()
    {
    
    
        CPractice::setName("Name");
        cout << "\n" << CPractice::getName();
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
  • Silvio Marijic
    Silvio Marijic about 11 years
    i thought that its defined allready when i declared it.Its works, thanks a lot
  • Joseph Mansfield
    Joseph Mansfield about 11 years
    @SilvioMarijic It's a common mistake that people have. The reason it isn't a definition is to avoid having multiple definitions. If it were a definition and you included the header it was in into many other files, you would have multiple definitions of the same static member. Anyway, don't forget to say thanks by accepting the answer that helped you the most.