模板类运算符"unresolved externals"
template class operators "unresolved externals"
在我的类中:
#ifndef __MYVECTOR_CLASS__
#define __MYVECTOR_CLASS__
template<class Type>
class MyVector{
....
MyVector& operator=(const MyVector& source); //works
friend MyVector<Type> operator+(MyVector<Type> lhs, const MyVector<Type> &rhs); //doesnt
....
};
template<class Type>
MyVector<Type>& MyVector<Type>::operator=(const MyVector &v){
if (_d != v._d){
_d = v._d;
_deleteArray();
_createArray(_d);
}
_assignValues(v._vector, (v._vector + _d));
return *this;
};
template<class Type>
MyVector<Type> operator+(MyVector<Type> lhs, const MyVector<Type> &rhs){
if (lhs._d == rhs._d){
for (int index = 0; index < lhs._d; index++){
lhs._vector[index] += rhs._vector[index];
}
}
return lhs;
};
#endif // __MYVECTOR_CLASS__
不包括其他非操作员功能,因为它们都可以正常工作。不知道为什么它不起作用。
在源文件中:
int main(){
MyVector<float> a(10);
MyVector<float> b(10);
a = b; // works alone
a = a + b; //breaks
return 0;
}
和错误:
- 错误
1 错误 LNK2001:未解析的外部符号"类"MyVector __cdecl 运算符+(class MyVector,class我的矢量)"
错误 2 错误 LNK1120:1 个未解析的外部
已编辑:
添加了构造函数。
template<class Type>
MyVector<Type>::MyVector(int size){
_d = size;
_createArray(_d);
_assignValues(0);
}
如果您尝试使用 coliru 进行编译,您将收到以下警告:
主.cpp:15:82: 警告:好友声明"我的矢量" operator+(MyVector, const MyVector&)' 声明 非模板函数 [-Wnon-模板友元] friend MyVector operator+(MyVector lhs, const MyVector &rhs);不
这应该可以作为正在发生的事情的线索。将代码更改为:
template<typename T>
friend MyVector<T> operator+(MyVector<T> lhs, const MyVector<T> &rhs);
它会建立。
friend 函数本身是一个模板,其模板参数与 class MyVector
的参数分开。
更新:虽然上述似乎确实有效,但在阅读了这个问题和这个问题之后,我想知道这是否更正确:
// forward-declare class MyVector
template<class Type>
class MyVector;
// forward-declare operator+()
template<class Type>
MyVector<Type> operator+(MyVector<Type> lhs, const MyVector<Type> &rhs);
template<class Type>
class MyVector{
// declare that the already-declared operator+<>() is a friend
friend MyVector<Type> operator+<>(MyVector<Type> lhs, const MyVector<Type> &rhs);
};
相关文章:
- CppSQLite 抛出"LNK2019: unresolved external symbol"错误
- Visual Studio中的wxwidgets应用程序给出错误"LNK2019 unresolved external symbol"
- 我的游戏引擎的 spdlog 日志记录出现奇怪的"unresolved external symbol"错误
- 如何修复"unresolved external symbol _gladLoadGLLoader referenced in function _main"和"unresolved external
- 尝试使用 CPP 构建类时"unresolved external..."
- 通过覆盖虚拟函数来获取'unresolved external symbol'
- 如何解决问题"unresolved extenal symbol _DllMainCRTStartup@12"?
- 运行时动态加载库时出错"unresolved external symbol"
- 为什么 VS C++链接器返回具有混合 DLL 转发的"LNK2001: unresolved external symbol"?
- Visual Studio 2017 中的 MySQL 连接器/C++ 错误"Unresolved external symbol _get_driver_instance"
- CMake 和 Visual Studio 出现"unresolved external symbol"错误
- DLL 注入编译 "unresolved external symbol" Visual Studio C++
- "Error LNK2001: unresolved external symbol _main"仍然存在
- QT 单元测试 moc "unresolved external symbol" for QMetaObject
- "error LNK2019: unresolved external fatal"和"error LNK1120: 1 unresolved externals"
- LNK 2001 unresolved with C++ SFML
- 模板类运算符"unresolved externals"
- MVS2015 SDL2 不断给出"unresolved externals"错误
- 视觉"1 unresolved externals" C++
- 为什么我会"Fatal error LNK1120: 1 unresolved externals"