c : 函数不能重载

新手上路,请多包涵

我遇到以下输出的编译时错误:

 $ 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 许可协议

阅读 336
2 个回答

您不能在类定义中同时声明和定义成员函数。

(您甚至将声明设为私有,将定义设为公开。)

删除声明或将定义移到类定义之外(我推荐后者)。

原文由 molbdnilo 发布,翻译遵循 CC BY-SA 3.0 许可协议

你有 fileExists 在类本身两次。一次没有定义

 bool fileExists(const std::string & path);

一次有定义

  bool fileExists(const std::string & path)
{
    std::ifstream file(path);
    return file.good();
}

您有两个选择,要么删除无定义部分,要么删除有定义部分并在类外部提供定义。

原文由 Gaurav Sehgal 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏