你能帮助解决这个错误吗?(g++原型与类中的任何原型都不匹配)

Can you help resolve this error? (g++ prototype does not match any in class)

本文关键字:原型 任何 不匹配 g++ 解决 帮助 错误      更新时间:2023-10-16

以下是编译器输出:

[brian@brian-arch-laptop Lab1]$ g++ -g -Wall -std=c++11 objectIO.cpp main.cpp -o main
objectIO.cpp:33:14: error: prototype for ‘std::vector<Type> objectIO<Type>::loadObjects(Type, std::string, unsigned int, unsigned int)’ does not match any in class ‘objectIO<Type>’
vector<Type> objectIO<Type>::loadObjects(Type dummyObject,
^
objectIO.h:14:23: error: candidate is: static std::vector<Type> objectIO<Type>::loadObjects(std::string, unsigned int, unsigned int)
static vector<Type> loadObjects(Type dummyObject, string fileName, unsigned numObjects,unsigned numLinesPerObject);
^

从我的研究中,我发现这种错误通常发生在一个人声明了一个函数时,但当他们定义它时,它有不同数量的参数或不同类型的参数。我还发现,当使用"const"声明函数,但在定义函数时却未能使用"const"时,通常会发生此错误。

这两种情况都不符合我的情况。我有匹配的光圈,我不会让我的函数保持不变。以下是我的来源:的有问题的行

来自我的源文件(.cpp):

template <class Type>
vector<Type> objectIO<Type>::loadObjects(Type dummyObject,
string filename,
unsigned numObjects,
unsigned numLinesPerObject){/*functionality here*/}

从我的头文件(.h):

template <class Type>
class objectIO{
public:
static vector<Type> loadObjects(Type dummyObject, string fileName,
unsigned numObjects,
unsigned numLinesPerObject);
}

模板类的所有定义都应放在一个头文件中。

您的代码在类的右大括号后缺少分号:

template <class Type>
class objectIO{
public:
static vector<Type> loadObjects(Type dummyObject, string fileName,
unsigned numObjects,
unsigned numLinesPerObject);
} /* <-- RIGHT HERE */

这通常会打乱源代码中接下来出现的内容,有时还会出现一条非常错误的错误消息。