<method>不是<class> </class> </method>的成员

时间:2011-11-03 20:31:25

标签: c++ visual-c++

这是我之前做过的非常基本的事情,但我不明白为什么我现在遇到问题。我正在创建一个对象'foo',以便与文件之间的iostream活动轻松连接。

直到今天,我添加了几个似乎在定义文件的上下文中找到问题的方法,几乎​​整个项目都没有问题。

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*/ }

    }

关于我为创建此问题所做的工作的任何线索?

编辑:我包含了真正的代码,只更改了类签名并删除了实现。我已经对语法进行了三次检查,以确保我没有拼错在不起作用的方法的范围分辨率中的类名。

2 个答案:

答案 0 :(得分:6)

我们无法知道(因为你没有发布真正的C ++),但我的水晶球说:

你有两份“foo.h”。您编辑的那个不是#include指令找到的那个。

答案 1 :(得分:0)

如果我在你发布的代码中添加了适当的return语句,它就会编译。

我仍然认为最可能的原因是某个地方出现错字 - 如果您无法向我们展示您尝试编译的确切代码,请找一个可以显示代码并要求他们为您仔细检查的代码