简体   繁体   English

c ++:函数不能重载

[英]c++: function cannot be overloaded

I am encountering a compile time error with the following output: 我在以下输出中遇到了编译时错误:

$ 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);

However, I do not see how that error makes sense, because I have only function declaration which I directly copied and pasted when writing the definition. 但是,我看不到该错误的含义,因为我只有在编写定义时直接复制并粘贴的函数声明。

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();
    }
};

You can't both declare and define a member function inside the class definition. 您不能在类定义内声明和定义成员函数。

(You even made the declaration private and the definition public.) (您甚至将声明设为私有,并将定义设为公开。)

Either remove the declaration or move the definition outside of the class definition (I recommend the latter). 删除声明或将定义移到类定义之外(我建议后者)。

You have fileExists twice in the class itself.Once without definition 你有fileExists在类itself.Once没有定义两次

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

and once with defintion 一次确定

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

You have two options either remove the without definition part, or remove the with definition part and provide the definition outside the class. 您有两个选择,要么删除不带定义的部分,要么删除带定义的部分,并在类外部提供定义。

Because the method is already declared, you must define it outside the class declaration: 因为该方法已经声明,所以必须在类声明之外定义它:

class VideoFile
{
    // ...
    bool fileExist(const std::string path);
    // ...
};

bool VideoFile::fileExist(const std::string& path)
{
     // ...
}

you can't overload fileExists function by another one with the same parameters by just using different scope, as you do in your example .. You may keep the two fileExists functions that you have, but you need to use different parameters , as follows 您不能像在示例中那样通过使用不同的范围来使另一个具有相同参数的fileExists函数重载。.您可以保留您拥有的两个fileExists函数,但是需要使用不同的参数,如下所示

     class VideoFile
     {
       private:

       bool fileExists();

       public:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM