Need help with implementing a pure abstract class via inheritance, using namespace to wrap all my classes to avoid conflict with others.

I have been able to build and run the code successfully if I remove namespace wrapper from my abstract class and all classes that inherit from my pure abstract class.

It seems like Visual Studio 2010 compiler is complaining that despite all classes are in the same namespace, the abstract class's pure abstract method is not implemented.

Any help would be much appreciated.

//IBaseClass.h

//namespace MyCustomNamespace
//{
class IBaseClass
{
public:
    virtual ~IBaseClass() { /*virtual destructor*/ }
//Behaviours...
    virtual bool Method001(/*some input*/) = 0;
    //virtual bool Method002(/*some input*/) = 0;
};
//} /*NAMESPACE*/

//-----------------------------------------
//ParentClass.h

//namespace MyCustomNamespace
//{
class ParentClass : virtual public IBaseClass
{
private:
    int a;

public:
    virtual ~ParentClass() { /*virtual destructor*/ }

    //getter-setter implemented in ParentClass.cpp file...
    void setA(const int aa); 
    const int getA() const; 
};
//} /*NAMESPACE*/


//-----------------------------------------
//ParentClass.h

//namespace MyCustomNamespace
//{
class ConcreteClass: public ParentClass 
{
private:
    int b;

public:
    virtual ~ConcreteClass() { /*virtual destructor*/ }

    //getter-setter...
    void setB(const int bb); 
    const int getB() const;

    bool Method001(/*some input*/); //re-declaring IBase abstract method...
};
//} /*NAMESPACE*/

//-----------------------------------------
//ConcreteClass.cpp

//namespace MyCustomNamespace
//{
    void ConcreteClass::setB(const int bb) { this->b = bb; } 
    const int ConcreteClass::getB() const { return this->a; }

    bool ConcreteClass::Method001(/*some input*/)
    {
      //implementation code goes here...
    }
//} /*NAMESPACE*/

Recommended Answers

All 8 Replies

>>It seems like Visual Studio 2010 compiler is complaining that despite all classes are in the same namespace, the abstract class's pure abstract method is not implemented.

I think you mis-interpreted the error message -- see comment below.

This works ok for me using vc++ 2010 express. The only change I had to make was make variable a in ParentClass protected so that it can be used in derived class.

#include "IBaseClass.h"
#include "ParentClass.h"
#include "concreteClass.h"
using namespace std;





namespace MyCustomNamespace
{
    void ConcreteClass::setB(const int bb) { this->b = bb; } 
    const int ConcreteClass::getB() const { return this->a; }

    bool ConcreteClass::Method001(/*some input*/)
    {
      //implementation code goes here...
        return true;
    }
} /*NAMESPACE*/


    int main()
    {
        MyCustomNamespace::ConcreteClass c;
    }

Sorry, there was a typo in my initial posting. Corrected it:

const int ConcreteClass::getB() const { return this->b; }

Error Msg is still there. Still trying to figure out why compiler has issues recognizing implementation of pure virtual method implemented in concrete class, and all classes are in the same namespace.

Please advise.

>>It seems like Visual Studio 2010 compiler is complaining that despite all classes are in the same namespace, the abstract class's pure abstract method is not implemented.

I think you mis-interpreted the error message -- see comment below.

This works ok for me using vc++ 2010 express. The only change I had to make was make variable a in ParentClass protected so that it can be used in derived class.

#include "IBaseClass.h"
#include "ParentClass.h"
#include "concreteClass.h"
using namespace std;





namespace MyCustomNamespace
{
    void ConcreteClass::setB(const int bb) { this->b = bb; } 
    const int ConcreteClass::getB() const { return this->b; }

    bool ConcreteClass::Method001(/*some input*/)
    {
      //implementation code goes here...
        return true;
    }
} /*NAMESPACE*/


    int main()
    {
        MyCustomNamespace::ConcreteClass c;
    }

>> Error Msg is still there

What error message? Please post the exact wording. As I said before your program coompiled and linked ok for me without errors or warnings after I changed private: int a; to protected: int a;

Initially posted code had a few typos. Posted code again to fix typos.

Need help with implementing a pure abstract class via inheritance, using namespace to wrap all my classes to avoid conflict with others.

I have been able to build and run the code successfully if I remove namespace wrapper from my abstract class and all classes that inherit from my pure abstract class.

It seems like Visual Studio 2010 compiler is complaining that despite all classes are in the same namespace, the abstract class's pure abstract method is not implemented.

Any help would be much appreciated.

//IBaseClass.h

//forward declaration
class ConcreteClass;

//namespace MyCustomNamespace
//{
class IBaseClass
{
public:
    virtual ~IBaseClass() { /*virtual destructor*/ }
//Behaviours...
    virtual bool Method001( const ConcreteClass &cc ) = 0;
    //virtual bool Method002(/*some input*/) = 0;
};
//} /*NAMESPACE*/

//-----------------------------------------
//ParentClass.h

//namespace MyCustomNamespace
//{
class ParentClass : virtual public IBaseClass
{
private:
    int a;

public:
    virtual ~ParentClass() { /*virtual destructor*/ }

    //getter-setter implemented in ParentClass.cpp file...
    void setA(const int aa); 
    const int getA() const; 
};
//} /*NAMESPACE*/


//-----------------------------------------
//ParentClass.h

//namespace MyCustomNamespace
//{
class ConcreteClass: public ParentClass 
{
private:
    int b;

public:
    virtual ~ConcreteClass() { /*virtual destructor*/ }

    //getter-setter...
    void setB(const int bb); 
    const int getB() const;

    bool Method001(/*some input*/); //re-declaring IBase abstract method...
};
//} /*NAMESPACE*/

//-----------------------------------------
//ConcreteClass.cpp

//namespace MyCustomNamespace
//{
    void ConcreteClass::setB(const int bb) { this->b = bb; } 
    const int ConcreteClass::getB() const { return this->a; }

    bool ConcreteClass::Method001( const ConcreteClass &cc )
    {
      //implementation code goes here...
      return false;
    }
//} /*NAMESPACE*/

The error msg that I am getting is:
C2259: MyCustomNamespace::ConcreteClass : cannot instantiate abstract class

I have also edited the code, initially there were a few typos. Thanks for pointing out the private/protected comments, good observation but that is not the problem, just a typo while posting code on my part. If you review code again, it will be a bit different.

Your comments about changing changing private to protected, that was a typo

>> Error Msg is still there

What error message? Please post the exact wording. As I said before your program coompiled and linked ok for me without errors or warnings after I changed private: int a; to protected: int a;

The error msg that I am getting is:
C2259: MyCustomNamespace::ConcreteClass : cannot instantiate abstract class

I have also edited the code, initially there were a few typos. Thanks for pointing out the private/protected comments, good observation but that is not the problem, just a typo while posting code on my part. If you review code again, it will be a bit different.

Your comments about changing changing private to protected, that was a typo.

//IBaseClass.h

//forward declaration
class ConcreteClass;
 
//namespace MyCustomNamespace
//{
class IBaseClass
{
public:
    virtual ~IBaseClass() { /*virtual destructor*/ }
//Behaviours...
    virtual bool Method001( const ConcreteClass &cc ) = 0;
    //virtual bool Method002(/*some input*/) = 0;
};
//} /*NAMESPACE*/

//-----------------------------------------
//ParentClass.h

//namespace MyCustomNamespace
//{
class ParentClass : virtual public IBaseClass
{
private:
    int a;

public:
    virtual ~ParentClass() { /*virtual destructor*/ }

    //getter-setter implemented in ParentClass.cpp file...
    void setA(const int aa); 
    const int getA() const; 
};
//} /*NAMESPACE*/


//-----------------------------------------
//ParentClass.h

//namespace MyCustomNamespace
//{
class ConcreteClass: public ParentClass 
{
private:
    int b;

public:
    virtual ~ConcreteClass() { /*virtual destructor*/ }

    //getter-setter...
    void setB(const int bb); 
    const int getB() const;

    bool Method001( const ConcreteClass &cc ); //re-declaring IBase abstract method...
};
//} /*NAMESPACE*/

//-----------------------------------------
//ConcreteClass.cpp

//namespace MyCustomNamespace
//{
    void ConcreteClass::setB(const int bb) { this->b = bb; } 
    const int ConcreteClass::getB() const { return this->b; }

    bool ConcreteClass::Method001( const ConcreteClass &cc )
    {
      //implementation code goes here...
    }
//} /*NAMESPACE*/

Need help with implementing a pure abstract class via inheritance, using namespace to wrap all my classes to avoid conflict with others.

I have been able to build and run the code successfully if I remove namespace wrapper from my abstract class and all classes that inherit from my pure abstract class.

It seems like Visual Studio 2010 compiler is complaining that despite all classes are in the same namespace, the abstract class's pure abstract method is not implemented.

Any help would be much appreciated.

//IBaseClass.h

//forward declaration
class ConcreteClass;
 
//namespace MyCustomNamespace
//{
class IBaseClass
{
public:
    virtual ~IBaseClass() { /*virtual destructor*/ }
//Behaviours...
    virtual bool Method001(/*some input*/) = 0;
    //virtual bool Method002( const ConcreteClass &cc ) = 0;
};
//} /*NAMESPACE*/

//-----------------------------------------
//ParentClass.h

//namespace MyCustomNamespace
//{
class ParentClass : virtual public IBaseClass
{
private:
    int a;

public:
    virtual ~ParentClass() { /*virtual destructor*/ }

    //getter-setter implemented in ParentClass.cpp file...
    void setA(const int aa); 
    const int getA() const; 
};
//} /*NAMESPACE*/


//-----------------------------------------
//ParentClass.h

//namespace MyCustomNamespace
//{
class ConcreteClass: public ParentClass 
{
private:
    int b;

public:
    virtual ~ConcreteClass() { /*virtual destructor*/ }

    //getter-setter...
    void setB(const int bb); 
    const int getB() const;

    bool Method001( const ConcreteClass &cc ); //re-declaring IBase abstract method...
};
//} /*NAMESPACE*/

//-----------------------------------------
//ConcreteClass.cpp

//namespace MyCustomNamespace
//{
    void ConcreteClass::setB(const int bb) { this->b = bb; } 
    const int ConcreteClass::getB() const { return this->a; }

    bool ConcreteClass::Method001( const ConcreteClass &cc )
    {
      //implementation code goes here...
    }
//} /*NAMESPACE*/

Your code compiled ok for me without errors or warnings using vc++ 2010 express. I just copied it all into one *.cpp file.

Thank you for your help. I figured out what the problem was. Have you tried compiling my sample code with the namespace braces un-commented for all the classes (.h and .cpp files)? It should give you an Error Mgs "error C2259: 'MyCustomNamespace::ConcreteClass' : cannot instantiate abstract class due to following members:
'bool MyCustomNamespace::IBaseClass::Method001(const ConcreteClass &)' : is abstract" in the output window...doesn't matter if you add them all in one file or multiple files.

Your code compiled ok for me without errors or warnings using vc++ 2010 express. I just copied it all into one *.cpp file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.