2016-09-02 2 views
-1

나는 다음과 같은 출력 컴파일 타임 오류가 발생하고있다 : 내가 직접 복사에만 함수 선언을 가지고 있기 때문에, 그 오류가 의미가 얼마나 그러나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(); 
    } 
}; 

답변

4

클래스 정의 내에서 멤버 함수를 선언하고 정의 할 수는 없습니다.

는 (당신은 선언 개인 및 정의를 공개했다.)

하나는 선언을 제거하거나 클래스 정의 (나는 후자를 추천합니다)의 외부 정의를 이동합니다.

4

당신은 정의

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

없이 클래스 itself.Once에 두 번 fileExists이 한 번 (고화질)

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

에 당신도 두 가지 옵션이 정의가없는 부분을 제거하거나 제거 the 정의 부분과 클래스 외부의 정의를 제공합니다.

1

방법이 이미 선언되어 있기 때문에, 당신은 클래스 선언 외부로 정의해야합니다 : 당신처럼, 단지 서로 다른 범위를 사용하면하여 동일한 매개 변수를 다른 하나에 의해 fileExists 함수를 오버로드 할 수

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

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

당신의 예제에서 .. 당신은 가지고있는 두 개의 fileExists 함수를 유지할 수 있지만 다음과 같이 다른 매개 변수를 사용해야합니다.

 class VideoFile 
    { 
     private: 

     bool fileExists(); 

     public: 

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