2011-11-03 64 views
-2

這是我以前做過的非常基本的東西,但我不明白爲什麼我現在遇到問題。我正在創建一個對象'foo',以便與文件中的iostream活動進行交互。<Method>不是<Class>的成員

直到今天,我幾乎已經構建了幾乎整個項目,直到今天,我添加了幾個方法,似乎在定義文件的上下文中找到問題。

了foo.h:

#ifndef FOO_H 
    #define FOO_H 

    #include <string> 
    #include <fstream> 
    #include <vector> 

    using std::string; 
    using std::fstream; 
    using std::vector; 

    namespace FileSys 
    { 
     class foo 
     { 
     public: 
      foo(); 
      /** 
      * Attempts to open a file from directory dir 
      * @param dir 
      *    The path of the file to be opened 
      */ 
      foo(string dir); 
      /** 
      * Attempts to find a directory using the parent file's path as the root directory and extending the directory by the child string 
      * If child is null, the foo is replicated. Fails when parent.isDirectory() returns false. 
      * @param parent 
      *    The parent file to be used as the root path 
      * @param child 
      *    The child path to be used for extending the parent path 
      */ 
      foo(foo &parent, string child); 
      /** 
      * Attempts to find a directory using the parent path as the root directory and extending the directory by the child string 
      * If child is null, the foo is created with the parent path as the full path. 
      * @param parent 
      *    The parent path to be used as the root path 
      * @param child 
      *    The child path to be used for extending the parent path 
      */ 
      foo(string parent, string child); 
      /** 
      * Attempts to clone reference to a previously existing foo file 
      * @param file 
      *    The file to be cloned 
      */ 
      foo(foo &file); 

      /** 
      * Returns the working directory of this file 
      */ 
      string getDir(); 

      /** 
      * Writes the content supplied to the file, overwriting any existing data 
      */ 
      bool writeTrunc(string content); 

      /** 
      * Writes the content supplied to the file, appending it to the end of the file 
      */ 
      bool writeApp(string content); 

      /** 
      * Concatenates two files together. No implementation yet. 
      */ 
      bool concatFiles(foo file1, foo file2); 

      /** 
      * Reads from file 
      * 
      * @param length 
      *    How many bytes will be retrieved from the file 
      */ 
      string read(int length); 

      /** 
      * Reads from file 
      * 
      * @param length 
      *    How many bytes will be retrieved from the file 
      * 
      * @param position 
      *    Position to start reading from 
      */ 
      string read(int length, int position); 

      /** 
      * Returns everything from the file 
      */ 
      string readWholeFile(); 

      /** 
      * Returns true if the file exists 
      */ 
      bool exists(); 

      /** 
      * Attempts to delete the current file 
      * @return Returns true if successful and false otherwise 
      *    The path of the file to be opened 
      */ 
      bool deleteFile(); 

      /** 
      * Returns true if this foo is a directory, rather than a file 
      */ 
      bool isDirectory(); 

      /** 
      * Returns the absolute path of the foo 
      */ 
      string getAbsolutePath(); 

      /** 
      * Returns the parent directory of this foo 
      */ 
      foo getParentFile(); 

      /** 
      * Creates all non-existent folders in mPath, creating the directory for this foo. 
      */ 
      bool mkdirs(); 

      /** 
      * Clean the file attributes 
      */ 
      bool cleanFile(); 

      /** 
      * Returns a list of all the directories and files contained within this directory 
      */ 
      vector<foo> list(); 

      /** 
      * Deletes the file or directory from the file system 
      */ 
      bool destroy(); 

      /** 
      * Overrides the '=' operator to perform an assignment properly 
      */ 
      foo& operator=(foo &file); 

      /** 
      * Overrides the '==' operator to perform an equality comparison on two foo 
      */ 
      bool operator==(foo &file); 

      /** 
      * Overrides the '!=' operator to perform an inequality comparison on two foo 
      */ 
      bool operator!=(foo &file); 

      ~foo(); 
     protected: 
     private: 

      string mPath; 
      fstream mFile; 
      bool mExists; 
      bool mIsFile; 

      /** 
      * Opens the file for operation 
      * 
      * @param openMode 
      *   The mode the file should be opened in. Use ios::openmode modes to determine the open mode. 
      * 
      * @return Returns true if successfully opened and false if failed 
      */ 
      bool openFile(int openMode); 

      /** 
      * Indicates whether this foo is a file or a directory 
      * Returns true if a file and false if a directory. 
      */ 
      bool isFile(); 

      /** 
      * Checks to see if the directory exists and attempts to create it if its parents exist 
      * 
      * @param dir 
      *   The directory to be created 
      * 
      * @return Returns true if successful 
      */ 
      bool mkdirs(foo dir); 
    }; 

} 

#endif // foo_H 

Foo.cpp中:

#include "foo.h" 

    #include <string> 
    #include <fstream> 
    #include <vector> 
    #include <errno.h> 
    #ifdef HAVE_UNISTD_H 
    #include <unistd.h> 
    #endif /* HAVE_UNISTD_H */ 
    #include <sys/stat.h> 

    using std::string; 
    using std::fstream; 
    using std::vector; 

    #ifdef _WIN32 
    #include <direct.h> 
    using namespace std; 
    #define mkdir(path,mode) _mkdir(path) 
    #define getcwd _getcwd 
    #endif 

    #define ROOT_WORKING_DIRECTORY "SomeDir" 

    namespace FileSys 
    { 
     //////////////////////////////////////////// 
     // Ctors 
     //////////////////////////////////////////// 
     foo::foo() 
     { /*implementation*/ } 

     foo::foo(string dir) 
     { /*implementation*/ } 

     foo::foo(foo &file) 
     { /*implementation*/ } 

     foo::foo(foo &parent, string child) 
     { /*implementation*/ } 

     foo::foo(string parent, string child) 
     { /*implementation*/ } 

     //////////////////////////////////////////// 
     // Public methods 
     //////////////////////////////////////////// 
     string foo::getDir() 
     { /*implementation*/ } 

     bool foo::deleteFile() 
     { /*implementation*/ } 

     bool foo::exists() 
     { /*implementation*/ } 

     bool foo::writeApp(string content) 
     { /*implementation*/ } 

     bool foo::writeTrunc(string content) 
     { /*implementation*/ } 

     string foo::read(int length) 
     { /*implementation*/ } 

     string foo::read(int length, int position) 
     { /*implementation*/ } 

     string foo::readWholeFile() 
     { /*implementation*/ } 

     bool foo::isDirectory() 
     { /*implementation*/ } 

     string foo::getAbsolutePath() 
     { /*implementation*/ } 

     foo foo::getParentFile() 
     { /*implementation*/ } 

     bool foo::mkdirs() 
     { /*implementation*/ } 

     foo& foo::operator=(foo &file) 
     { /*implementation*/ } 

     bool foo::operator==(foo &file) //err 2039: 'operator==' is not a member of foo 
     { /*implementation*/ } 

     bool foo::operator!=(foo &file) //err 2039: 'operator!=' is not a member of foo 
     { /*implementation*/ } 

     bool foo::cleanFile() 
     { /*implementation*/ } 

     vector<foo> foo::list() //err 2039: 'list' is not a member of foo 
     { /*implementation*/ } 

     bool foo::destroy() //err 2039: 'destroy' is not a member of foo 
     { /*implementation*/ } 

     //////////////////////////////////////////// 
     // Private methods 
     //////////////////////////////////////////// 
     bool foo::openFile(int openMode) 
     { /*implementation*/ } 

     bool foo::isFile() 
     { /*implementation*/ } 

     bool foo::mkdirs(foo dir) 
     { /*implementation*/ } 

     //////////////////////////////////////////// 
     // Dtors 
     //////////////////////////////////////////// 
     foo::~foo() 
     { /*implementation*/ } 

    } 

任何線索,我做了什麼來創建這個問題?

編輯:我包含了真實的代碼,只更改了類簽名並刪除了實現。我有三重檢查了語法,以確保在沒有工作的方法的作用域分辨率中沒有拼錯類名。

+6

你可能在某個地方犯了一個錯字,但是我們沒有辦法告訴你,因爲你沒有發佈真正的代碼。 –

+1

你確定在你的任何頭文件中沒有'destroy'的預處理器定義嗎?在定義之前嘗試'#undef destroy'。 –

+0

@DeepYellow,如果有一個宏重命名該函數,它將反映在錯誤消息中。 –

回答

5

我們無法知道(因爲你沒有真正發佈C++),但我的水晶球說:

你「了foo.h」的兩個副本。你編輯的那個不是#include指令找到的那個。

+0

我也考慮過這個問題,但是我保存了兩個文件,在項目的目錄中用單獨的文件編輯器打開它們,它們都反映了最近的更改。 – nukeforum

0

如果我將適當的返回語句添加到您發佈的代碼中,它會進行編譯。

我仍然認爲最可能的原因是某處出現拼寫錯誤 - 如果您無法向我們展示您正在編譯的確切代碼,請找到可以顯示代碼的人並要求他們仔細檢查它您。

相關問題