[英]I have an error LNK2019: unresolved external symbol error (small amount code to look at)
[英]I am working on a small class template lab for school and my code will not compile, it gives me an unresolved external symbol
编译代码时出现无法解决的外部错误,我无法弄清楚问题是什么。 我非常肯定模板和函数是根据分配使用和创建的,但我只是无法将其编译。 对这个问题的任何帮助将不胜感激。
#pragma once
#include<iostream>
using namespace std;
template <class P>
class Pair
{
private:
P firstLetter;
P secondLetter;
public:
Pair(const P&, const P&);
P getSecondLetter();
P getFirstLetter();
};
template <class P>
P Pair<P>::getFirstLetter()
{
return firstLetter;
}
template <class P>
P Pair<P>::getSecondLetter()
{
return secondLetter;
}
主要:#include
#include "Pair.h"
using namespace std;
int main()
{
Pair<char> letters('a', 'd');
cout << "\nThe first letter is: " << letters.getFirstLetter();
cout << "\nThe second letter is: " << letters.getSecondLetter();
cout << endl;
system("Pause");
return 0;
}
快好了。 只需添加构造函数(这可能是您想要的那个)
template <class P>
Pair<P>::Pair(const P &p1, const P &p2) : firstLetter(p1), secondLetter(p2) { }
对于完成的版本,如下所示:
#pragma once
#include<iostream>
using namespace std;
template <class P>
class Pair
{
private:
P firstLetter;
P secondLetter;
public:
Pair(const P&, const P&);
P getSecondLetter();
P getFirstLetter();
};
template <class P>
Pair<P>::Pair(const P &p1, const P &p2) : firstLetter(p1), secondLetter(p2) { }
template <class P>
P Pair<P>::getFirstLetter()
{
return firstLetter;
}
template <class P>
P Pair<P>::getSecondLetter()
{
return secondLetter;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.