无法解析未定义的参考
Unable to resolve Undefined reference
我在功能模板中遇到问题,我试图在Functions.cpp
中实例化函数。但是这样做后,它给了我汇编错误。以下是我遇到的错误。如果有人能够提供帮助,将非常感谢它!谢谢!
错误
Functions.cpp:81:15: error: template-id ‘remove<int>’ for ‘int
CS150::remove(int*, int*, int*)’ does n template declaration
template int remove<int>(int *first, int*last, int* val);
^~~~~~~~~~~
Functions.cpp:56:5: note: candidate is: template<class T> T*
CS150::remove(T*, T*, T*)
T* remove(T *first, T *last, T* val)
^~~~~~
function.cpp
template <typename T>
T* remove(T *first, T *last, T* val)
{
T result = first;
while (first!=last)
{
if (!(*first == val))
{
*result = *first;
++result;
}
++first;
}
return result;
}
template int remove<int>(int *first, int*last, int* val);
functions.h
template <typename T>
T* remove(T *first, T *last, const T& val);
驱动程序文件
static void TestRemove1(void)
{
cout << "***** Remove1 *****" << endl;
int i1[] = { 5, -7, 4, 10, -21, 15, 9 };
int size = sizeof(i1) / sizeof(int);
CS150::display(i1, i1 + size);
int item = -1;
int * newend = CS150::remove(i1, i1 + size, item);
cout << "remove " << item << ", new list: ";
CS150::display(i1, newend);
}
static void TestRemove2(void)
{
cout << "***** Remove2 *****" << endl;
int i1[] = {5, -7, 4, 10, -7, 15, 9};
int size = sizeof(i1) / sizeof(int);
CS150::display(i1, i1 + size);
int item = -7;
int *newend = CS150::remove(i1, i1 + size, item);
cout << "remove " << item << ", new list: ";
CS150::display(i1, newend);
}
您不需要template
关键字。以下可能会起作用(我仍然不确定为什么您根本需要此行):
int remove<int>(int *first, int*last, int* val);
通过编写remove<int>
实例化模板,因此它不再是模板。
您的模板函数签名表示它返回T*
,但是您的功能定义和实例返回T
(int
)。您需要使它们匹配。
相关文章:
- GTest EXPECT_EQ给出未定义的错误参考
- 在VSCode中编译C++给了我一个未定义的参考
- Armadillo正在为共享的lapack库获取未定义的参考
- 我对C++程序有一个未定义的参考,不知道如何处理它
- 未定义的符号参考'pthread_rwlock_wrlock@@GLIBC_2.2.5'
- 新的C ,未定义的参考
- ROS中未定义的参考误差到OpenCV图像转换器
- 使用Arduino IDE的构造函数的未定义参考错误
- 未定义的参考错误链接天然函数Android
- QT的倍频程:未定义的参考
- 未定义的参考,差异使用 CUDA 的 OpenCV 中的双边过滤器
- MSYS2静态QT未定义的参考问题
- LLVM-5.0 MakeFile未定义的参考失败
- 尝试在Windows结果上使用GLOG未定义的参考
- 从链接中缺少什么,导致未定义的参考
- 带有静态库的Android NDK:链接器始终抛出“未定义的参考”
- Lambdas的C 向量 - 未定义的参考
- __extern_inline func导致未定义的参考错误
- OPENCV筛选未定义的参考问题
- C 指针未定义的参考