类模板C++错误
Class Template C++ Errors
更新了文件,但我仍然收到以下错误。
- 1未解析的外部(lab12.exe第1行)
- 未解析的外部符号"public char__thiscall Pair::geetFirst(void)"(?getFirst@$Pair@D@@QAEDXZ)(lab12.obj第1行)
配对h
#pragma once
template <class T>
class Pair
{
private:
T theFirst;
T theSecond;
public:
/*Pair(const T& dataOne, const T& dataTwo);*/
Pair(T a, T b) {
theFirst = a;
theSecond = b;
}
T getFirst();
T getSecond();
};
配对.cpp
#include "stdafx.h"
#include "Pair.h"
template<class T>
T Pair<T>::getFirst()
{
return theFirst;
}
template<class T>
T Pair<T>::getSecond()
{
return theSecond;
}
Main.cpp
#include "stdafx.h"
#include "Pair.h"
#include <iostream>
using namespace std;
int main()
{
Pair <char> letters('a', 'd');
cout << letters.getFirst();
cout << endl;
system("Pause");
return 0;
}
你已经被using namespace std;
咬了一口。您有自己的pair
类,但在标准库中有一个std::pair
。编译器无法决定使用哪一个,因此会出现不明确的符号错误。
解决方案:不要使用using namespace std;
!用std::
限定标准库符号。这样可以省去很多头疼的事。
命名空间std还包含一个名为pair的模板类。
您需要将自己的对模板包装在自己的命名空间中,或者将其命名为其他名称,这样您就可以调用它而不会出现不明确的符号错误。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'