Abstract Class Problem

Bear with me, this issue is a bit difficult to explain I guess, it involves the following files:

1
2
3
4
5
6
7
8
9
10
11
12
class Structure { // Abstract Class

    public:

        virtual ~Structure() = 0;
        virtual int getCost() = 0;
        virtual int getBuildTime() = 0;
        virtual std::string getDescription() = 0;
};

Structure::~Structure()
{}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class House : public Structure { // Concrete(pun intended?) Class

    public:

        House();
        ~House();
        int getCost();
        int getBuildTime();
        std::string getDescription();

};

House::House()
{}

House::~House()
{}

int House::getCost()
{
    return 500;
}

int House::getBuildTime()
{
    return 10;
}

std::string House::getDescription()
{
    return "Lovely house to live in!";
}


1
2
3
4
5
class StructureFactory : public Structure {
    
    public:
        virtual Structure* createStructure() = 0;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
template <class StructureType>
class StructureCreator : public StructureFactory {

    public:
        
        Structure* createStructure();
};


template <class StructureType>
Structure* StructureCreator<StructureType>::createStructure()
{
    return new StructureType;
}


1
2
3
4
5
6
7
8
9
10
int main()
{
    StructureCreator<House> appCreator; // This line is the problem
    // It compiles fine if my Structure class is not abstract
    // Fails to compile if i make my structure class abstract

    Structure *p = appCreator.createStructure();

    return 0;
}


Using VS2010 with SP1 - VC++ Compiler spits out the following:


error C2259: 'StructureCreator<StructureType>'
: cannot instantiate abstract class
with
[
StructureType=House
]
due to following members:

int Structure::getCost(void) : is abstract
see declaration of 'Structure::getCost

int Structure::getBuildTime(void) : is abstract
see declaration of 'Structure::getBuildTime

std::string Structure::getDescription(void) : is abstract
see declaration of Structure::getDescription'



Intellisense:
Error 1 error C2259: 'StructureCreator<StructureType>' : cannot instantiate abstract class

2 IntelliSense: object of abstract class type "StructureCreator<House>" is not allowed:


Any help appreciated, my brain is melting on this one :S
class StructureFactory : public Structure
Explain.
Wow.... ne555.... I should be banned for that :(

No idea why I inherited from Structure, must have been an oversight from all my copy/paste, since I have a lot of subclasses.

Thanks!
Last edited on
Topic archived. No new replies allowed.