我遇到以下输出的编译时错误:
$ g++ -ggdb `pkg-config --cflags opencv` -o `basename main.cpp .cpp` main.cpp StringMethods/StringMethods.cpp `pkg-config --libs opencv` -std=c++0x
In file included from main.cpp:2:0:
VideoFile.hpp:32:10: error: ‘bool VideoFile::fileExists(const string&)’ cannot be overloaded
bool fileExists(const std::string & path)
^
VideoFile.hpp:15:10: error: with ‘bool VideoFile::fileExists(const string&)’
bool fileExists(const std::string & path);
但是,我看不出这个错误有什么意义,因为我只有在编写定义时直接复制和粘贴的函数声明。
class VideoFile
{
private:
std::string filePath;
bool fileExists(const std::string & path);
public:
VideoFile(const std::string & path)
:filePath(path)
{
filePath = StringMethods::trim(path);
if (!fileExists(filePath))
throw std::runtime_error("The file: " + filePath + " was not accessible");
}
~VideoFile()
{
}
bool fileExists(const std::string & path)
{
std::ifstream file(path);
return file.good();
}
};
原文由 RedShirt 发布,翻译遵循 CC BY-SA 4.0 许可协议
您不能在类定义中同时声明和定义成员函数。
(您甚至将声明设为私有,将定义设为公开。)
删除声明或将定义移到类定义之外(我推荐后者)。