在抽象类/函数上编译错误

时间:2012-03-01 16:03:07

标签: c++ compiler-errors abstract-class

我在C ++抽象类(其他类需要继承和实例化)上有一个奇怪的错误(缺少&#39 ;;',字符串类未被识别等)。此外,我希望派生类被强制实现抽象类方法 - 因此纯虚拟= 0。

下面是我的抽象类定义。

Drawable.h:

#ifndef __myns__managers__Drawable_h__
#define __myns__managers__Drawable_h__

#include <string>

namespace myns
{
    namespace managers
    {
        class Drawable;
    }
}

namespace myns
{
    namespace managers
    {
        class Drawable
        {
            public:
                virtual float getX() = 0;
                virtual float getY() = 0;
                virtual string getImageFilePath() = 0;
            protected:
                int drawableId;

        };
    }
}

#endif

根据我对C ++的相关知识,我猜这一切都好,对吧?但是当我在VS 2010上编译时,它给了我一堆编译错误 - 如下所示。

我该如何解决这个问题?提前谢谢。

VS Build Output:

1>------ Build started: Project: InteractiveMembranes_vc10, Configuration: X86 Debug Win32 ------
1>  OutputManager.cpp
1>(project dir)\managers\drawable.h(23): error C2146: syntax error : missing ';' before identifier 'getImageFilePath'
1>(project dir)\managers\drawable.h(23): error C2433: 'interactivemembranes::managers::Drawable::string' : 'virtual' not permitted on data declarations
1>(project dir)\managers\drawable.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>(project dir)\managers\drawable.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>(project dir)\managers\drawable.h(23): warning C4183: 'getImageFilePath': missing return type; assumed to be a member function returning 'int'
1>(project dir)\managers\drawable.h(23): error C2253: 'interactivemembranes::managers::Drawable::getImageFilePath' : pure specifier or abstract override specifier only allowed on virtual function
1>  Application.cpp
1>(project dir)\managers\drawable.h(23): error C2146: syntax error : missing ';' before identifier 'getImageFilePath'
1>(project dir)\managers\drawable.h(23): error C2433: 'interactivemembranes::managers::Drawable::string' : 'virtual' not permitted on data declarations
1>(project dir)\managers\drawable.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>(project dir)\managers\drawable.h(23): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>(project dir)\managers\drawable.h(23): warning C4183: 'getImageFilePath': missing return type; assumed to be a member function returning 'int'
1>(project dir)\managers\drawable.h(23): error C2253: 'interactivemembranes::managers::Drawable::getImageFilePath' : pure specifier or abstract override specifier only allowed on virtual function
1>  Generating Code...
1>  Compiling...
1>  TestPersistence.cpp
1>  Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

1 个答案:

答案 0 :(得分:6)

您需要符合string的条件:

virtual std::string getImageFilePath() = 0;

string位于std命名空间中,而不是全局命名空间中。还有using namespace std;选项可以将命名空间导入全局命名空间,但不会这样做,尤其是在标题中。我只是将它作为一个选项提出,但你应该总是更喜欢对这些名字进行限定。

编辑:正如Als指出的那样,您只能导入所需内容:using std::string;。我仍然更喜欢这个名字。