Question

I have a problem using the interface A.

While trying to call the Initialize() function on any pointer stored in the vector aVec VisualStudio says "Error: The classtype pointer-to-incomplete is not valid".

Does anybody know how to implement this interface in a way that allows me to call an interface function on the basic pointer whitout casting it to the specific implementation type?

The error occurs in the main.cpp. I´ve attached the whole source down below:

A.hpp:

     #ifndef _A_HPP_
     #define _A_HPP_
     class A {
         private:
             int number;

         public:
             A();
             virtual ~A();
             virtual bool Initialize();

             int GetNumber();
             void SetNumber(int number);
     };
     #endif /* _A_HPP_ */

A.cpp:

     #include "A.hpp"
     A::A() {
     }

     A::~A() {
     }

     bool A::Initialize() {
        return true;
     }

     int A::GetNumber() {
        return this->number;
     }
     void A::SetNumber(int number) {
        this->number = number;
     }

B.hpp:

     #ifndef _B_HPP_
     #define _B_HPP_
     class B : public A{
         private:
             char* abc;

        public:
            B();
            ~B();
            bool Initialize();      
     };
     #endif /* _B_HPP_ */

B.cpp:

     #include "B.hpp"
     B::B(){
     }

     B::~B(){
        delete[] abc;
     }

     bool B::Initialize(){
        this->abc = new char[10];
     }

AFactory.hpp

     #ifndef _AFACTORY_HPP_
     #define _AFACTORY_HPP_
     class A;

     class AFactory {
        public:
            static A* GetInstance(int index);
     };
     #endif /* _AFACTORY_HPP_ */

AFactory.cpp

     #include "AFactory.hpp"
     #include "A.hpp"
     #include "B.hpp"
     A* AFactory::GetInstance(int index) {
        A* a;
        if(index == 0)a = new B();
        else a = 0;
        return a;
     }

main.cpp

     #include "A.hpp"
     #include "B.hpp"
     #include "AFactory.hpp"
     #include <vector>
     using namespace std;

     int main() {
        vector<A*> aVec;
        aVec.push_back(AFactory::GetInstance(0));
        aVec.push_back(AFactory::GetInstance(0));
        aVec.push_back(AFactory::GetInstance(0));

        for(int i = 0; i < 3; i++) aVec[i]->Initialize();
     }

Thank you very much! Xenogenesis

Was it helpful?

Solution

Commenters are saying the right thing, but maybe not that clear.

In your AFactory.hpp file you have class A;. This sort of statement is called "forward declaration". It states that A is a class and that details on this class will be given later. In your file main.cpp, for this or that reason, most likely because of the include preventers, the body of your your #include "A.hpp" is not included.

This means that A is still not defined. Compiler knows only that A is a class and nothing else. Because of that compiler cannot handle aVec[i]->Initialize();. The error message is saying this. Modify your include preventors to something else, like _A_FILE_HPP_.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top