I’m having a weird error (missing ‘;’, string class not recognized, etc) on an C++ abstract class (which other classes need to inherit and instanciate). Also I want derived classes to be forced to implement the abstract class methods – hence the pure virtual = 0.
Below is my abstract class definition.
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
By my relative knowledge of C++ I guess that’s everything ok, right? But when I compile on VS 2010 it gives me a bunch of compile errors – shown below.
How can I solve this? Thanks in advance.
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 ==========
You need to qualify
string:stringis in thestdnamespace, not in the global one. There’s also the option ofusing namespace std;which will import the namespace into the global one, but don’t do it, especially in headers. I’m just presenting it as an option, but you should always prefer qualifying the names.EDIT: As Als pointed out, you can import only what you need:
using std::string;. I’d still prefer qualifying the name though.