Question

I am trying to run a main.cpp which access 3 different classes. For some reason, I am getting a unresolved external symbol error. From what I've seen online, its clearly a linking error somewhere, but I cannot find it. I've listed the error below, but it's got a lot of info in it and im having trouble finding out exactly what it means.

The error: main.obj:-1: error: LNK2001: unresolved external symbol "public: __thiscall AtpReader::AtpReader(class std::basic_string,class std::allocator >)" (??0AtpReader@@QAE@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)

My code is:

main.cpp:

#include <iostream>
#include "atlasobject.h"
#include "atp.h"
#include "atpreader.h"
using namespace std;
int main()
{
    AtpReader reader("E:/doc.txt");
    return 0;
}

AtpReader.h:

#ifndef ATPREADER_H
#define ATPREADER_H
#include "atp.h"

class AtpReader
{
public:
    AtpReader();
    AtpReader(string filename);

    void atpReadHeader();
    void atpRead();
    string decryptLine(string line);

    ATP readerATP;

private:
    string file;
};
#endif // ATPREADER_H

atp.h:

#ifndef ATP_H
#define ATP_H
#include "atlasobject.h"
#include "vector"

struct Image{
    string Dim;
    string Vox;
    string Ori;
    char* data;
};

class ATP
{
public:
    ATP();
    vector<AtlasObject> listOfMaps;

private:
    Image referenceImage;
};
#endif // ATP_H

and AtlasObject.h:

#ifndef ATLASOBJECT_H
#define ATLASOBJECT_H
#include <string>

using namespace std;
class AtlasObject{

public:
    //virtual void create();
    AtlasObject();

    void set_uid(string id);
    void set_label(string l);
    void set_offset(string o);
    void set_mapInfo(string info);
    void set_data(char* inData);
    void set_isEncrypted(int encrypted);

    string get_uid();
    string get_label();
    string get_offset();
    string get_mapInfo();
    char* get_data();
    int get_isEncrypted();

protected:
    string uid;
    string label;
    string offset;
    string mapInfo;
    char *data;
    int isEncrypted;
};
#endif // ATLASOBJECT_H

my AtpReader.cpp is:

#include "atpreader.h"

#include <iostream>
#include <fstream>
#include <stdint.h>
#include <sstream>

AtpReader::AtpReader()
{
    printf("AtpReader()\n");
}

AtpReader::AtpReader(string filename)
{
    printf("AtpReader(%s)\n",filename.c_str());
}
Was it helpful?

Solution

I see you did not include AtpReader.h in AtpReader.cpp, but probably you just missed it when you made copy/paste, to insert it here, because if you didn't really include it, the error would have been different. Also, I see you're including in your main.cpp both "atlasobject.h" and "atp.h" and you don't really need that.

Later edit: Your problem is in the atp.h...you constructor is declared but never defined. Do this: ATP(){};

OTHER TIPS

Try using g++ in linux terminal make the object files of each of the source codes and then link the object files and run the executable

g++ -c atp.cpp AtpReader.cpp AtlasObject.cpp
g++ -o exe atp.o AtpReader.o AtlasObject.o
./exe

AtpReader.cpp is not getting built or the its object file is not getting linked to final executable. Check if AtpReader.obj/.o is created in build directory.

Because of the linker error you are getting and assuming that this is some of your actual code. Since I can't see any function inlining, global constants or variables being used out of scope I think the problem is located in the AtpReader.cpp, are you missing an #include AtpReader.h there?

With just a function prototype, the compiler can continue without error, but the linker cannot resolve a call to an address because there is no function code or variable space reserved. You will not see this error until you create a call to the function that the linker must resolve.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top