dimanche 25 décembre 2016

GNU g++ compiler error when trying to use headers and multiple files [duplicate]

This question already has an answer here:

okay, I have been learning C++ for several months now and everything has been great except for one thing: I cannot for the life of me get working over multiple source and header files to work.. nothing I do works.. I have just spent three hours trying to research my problems online and nothing works..

here is an example:

lets say I have three simple files: main.cpp, test.cpp and test.h

main.cpp:

#include "test.h"

int main() {

    AA<int> testAA; //just create an instance of the class declared in test.h and defined in test.cpp

    return 0;
}

test.cpp:

#include "test.h"

template<typename T>
T& AA<T>::access() {

    return this->val;
}

test.h:

template<typename T>
class AA {

    T val;

public:

    AA() { ;}

    T& access();
};

now... using the gnu g++ compiler: g++ -std=c++11 main.cpp test.cpp

that will compile absolutely fine..

but if I make this simple change to main.cpp:

#include "test.h"

int main() {

    AA<int> testAA;
    testAA.access(); // <----- this line

    return 0;
}

then the compiler complains of an undefined reference to 'AA::access()'...

so what am I doing wrong??? is it that I just dont understand the GCC c++ compiler? Am I not using an option within the compiler or something cause I cannot figure it out...

Aucun commentaire:

Enregistrer un commentaire