开发者

Template Compilation

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, 开发者_如何学C0 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


#ifndef NonTemplateFunctionFriend_H
#define NonTemplateFunctionFriend_H

    #include <iostream>

    template<class T> class Paolo {

    private:
        T myOnlyMember;
    public:
        Paolo(): myOnlyMember(1000) {};
        T getMyOnlyMember();
    };

   template<class T>
   T Paolo<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

Paolo<T>::


Template code must be defined before use, not just declared.


You defined a free function in NonTemplateFunctionFriend.cpp, not a member function of Paolo<T>:

    template<class T>   T Paolo<T>::getMyOnlyMember() {
            return myOnlyMember;
    }

That is, you missed the Paolo<T>:: in the definition.


Paolo: Your code doesn't link because your code does not define the member function `Paulo::getMyOnlyMember. You do have a definition in NonTemplateFunctionFriend.cpp that looks somewhat like what you want, but to the compiler it is a completely different function template.

That said, never do this in a header:

#include "NonTemplateFunctionFriend.cpp"

Never do this in a header, either:

template<class T>
T Paolo<T>::getMyOnlyMember() {
    return myOnlyMember;
}

The problem here is that if multiple files include the header and create a getMyOnlyMember for the same type you will have an unhappy linker. Add the inline qualifier and everything will be cool.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜