gpt4 book ai didi

c++ - 模板编译

转载 作者:行者123 更新时间:2023-11-28 08:19:41 25 4
gpt4 key购买 nike

有人可以向我解释如何避免以下错误吗?

我想我正在按照我阅读的有关模板编译的所有内容进行操作,但仍然出现错误。很抱歉问这个问题 - 这看起来微不足道,但我被卡住了!

谢谢,保罗

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 ==========

主要

#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

关于c++ - 模板编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6312428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com