1

I have looked at so many threads about this topic and none of them have helped. I still cannot figure out what I am doing wrong. Please help!

I am getting this error:

main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall Config::Config(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Config@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main

and here is what I have codewise. I stripped everything from the code to try and figure this out; it's extremely simple.

My Config.h file

#ifndef CONFIG_H
#define CONFIG_H

#include <string>

class Config{
public:
     Config(std::string file_name);
};

#endif

and here is my Config.cpp file

//Config class
#include "Config.h"
#include <string>

Config::Config(std::string file_name){
    //Do stuff
}

and here is where I try to use it

Config c("test");

I cannot figure this out for the life of me even after like an hour of reading through articles and threads.

Herith12
  • 69
  • 8
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Baum mit Augen Feb 17 '15 at 01:32
  • You apparently do not link against whatever Config.cpp compiles to, or fail to compile it in the first place. – Baum mit Augen Feb 17 '15 at 01:33
  • Are you using Visual Studio? (Based on the error code, you are at least using the MSVC compiler). Is Config.cpp in the project? It should be in the "Source Files" part in the Solution Explorer – The Dark Feb 17 '15 at 01:34
  • I'm using the Qt program and the header file is is under the header folder (within Qt) and the .cpp file is under source (within Qt). And as for the duplicate, I know that it is a duplicate, but no other article or thread has helped me. I spent over a hour going over information and nothing helped. Also, Baum, can you please clarify on what you mean.. I do not quite understand. – Herith12 Feb 17 '15 at 01:37
  • @Herith12 It is already explained in detail in the dupe I linked. Also "Over an hour" is not really much time to spend on a problem. – Baum mit Augen Feb 17 '15 at 01:45
  • add your IDE name as a tag, then people who use that IDE will see it and tell you how to link in your files in that IDE – M.M Feb 17 '15 at 01:46
  • If I create a new project and create a new class, it works as expected. I checked my .pro file and all and it looks the same as the new projects. I'll continue to read more. – Herith12 Feb 17 '15 at 02:18

1 Answers1

2

Make sure that you're actually linking in the Config.cpp file (or its equivalent object file). If you're only doing the main file. you'll get an 'unresolved' error like that.

For example, with the files:

Config.h :
    #ifndef CONFIG_H
    #define CONFIG_H
    #include <string>
        class Config{
        public:
            Config(std::string file_name);
        };
    #endif

Config.cpp:
    #include "Config.h"
    #include <string>
    Config::Config(std::string file_name){}

Main.cpp:
    using namespace std;
    #include "Config.h"
    int main() {
        Config c("test");
        return 0;
    }

Compiling and linking both works:

pax> g++ -o Main Main.cpp Config.cpp

But not doing Config.cpp results in an error:

pax> g++ -o Main Main.cpp
     /tmp/cc8GYPy2.o: In function `main':
       Main.cpp:(.text+0x42): undefined reference to 
         `Config::Config(std::string)'
     collect2: error: ld returned 1 exit status

That's pretty easy to discern when you're using a command line compiler since it's immediately obvious from the command line what's happening.

In an IDE, it may not be so simple but it generally boils down to ensuring the CPP source file is part of the project and will be built.

One way to test if it is: comment out the currently offending line from your main CPP file and add this to Config.cpp:

int main() { return 0;}

If it's actually being built, you should see an error complaining about duplicate main symbols.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • So you're saying that I am not compiling both of the files? – Herith12 Feb 17 '15 at 01:46
  • @Herith12, no, because I can't access your system from here. What I'm saying is that you should _check/ensure_ that you're compiling and linking together both files. For all I know, it could be a bug in QtCreator, or someone has replaced your compiler with a prank program, or you've been partaking of illicit substances. All those are less likely than what I posited, but my certainty levels don't _quite_ reach 100% :-) – paxdiablo Feb 17 '15 at 01:48