Can someone pelase explain to me how I can avoid the error below?
I think I am following all I have read on template compilation but still get an error. Sorry for asking this – it seems trivial but I am stuck!
Thanks,
Paolo
1>------ Build started: Project: BitsAndPieces, Configuration: Release Win32 ------
1> NonTemplateFunctionFriend_main.cpp
1>NonTemplateFunctionFriend_main.obj : error LNK2001: unresolved external symbol "public: int __thiscall Paolo<int>::getMyOnlyMember(void)" (?getMyOnlyMember@?$Paolo@H@@QAEHXZ)
1>\\na-13\agnolucp\my documents\visual studio 2010\Projects\BitsAndPieces\Release\BitsAndPieces.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
main
#include "NonTemplateFunctionFriend.h"
#include<iostream>
using namespace std;
int main() {
Paolo<int> Me;
cout << Me.getMyOnlyMember() << endl;
return 1;
}
NonTemplateFunctionFriend.h
#ifndef NonTemplateFunctionFriend_H
#define NonTemplateFunctionFriend_H
#include <iostream>
template<class T> class Paolo {
private:
T myOnlyMember;
public:
Paolo(): myOnlyMember(1000) {};
T getMyOnlyMember();
};
#include "NonTemplateFunctionFriend.cpp"
#endif
NonTemplateFunctionFriend.cpp
#ifndef NonTemplateFunctionFriend_CPP
#define NonTemplateFunctionFriend_CPP
#include "NonTemplateFunctionFriend.h"
template<class T> T getMyOnlyMember() {
return myOnlyMember;
}
#endif
You can separate the file as long as you’re including it inside the header file (templates must be fully defined within the same translation unit, lacking proper support for the export keyword).
But you’re not qualifying your function definition with the class