Question

I googled this error message but I can't find the relation to my case.

I tried to make a cars abstract factory:

#include <iostream>
using namespace std;

class IFactory
{
public:
    enum FACTORIES { MERCEDES, AUDI, SKODA };
    virtual const char* getCar() = 0;
    static IFactory* createFactory(FACTORIES factory)
    {
        if(factory == MERCEDES)  
            return new MercedesFactory;
        if(factory == AUDI) 
            return new AudiFactory;
        else 
            return new SkodaFactory;
    }
};

class AudiFactory : public IFactory
{
public:
    const char* getCar()
    {
        return "Audi";
    }
};

class MercedesFactory : public IFactory
{
public:
    const char* getCar()
    {
        return "EClass";
    }
};

class SkodaFactory : public IFactory
{
public:
    const char* getCar()
    {
        return "Octavia";
    }
};

I've got 3 errors:

error C2061: syntax error : identifier 'MercedesFactory'
error C2061: syntax error : identifier 'AudiFactory'
error C2061: syntax error : identifier 'SkodaFactory'

On lines:

return new MercedesFactory
return new AudiFactory
return new SkodaFactory

(Each error in each line).

Can you please help me find the problem? thanks in advance.

Was it helpful?

Solution

You are trying to create your classes before the compiler has seen their definitions. Move the body of your createFactory function to below your class definitions, preferably in your source file. You should avoid putting whole functions inside classes anyway.

OTHER TIPS

You should declare MercedesFactory and others before you use class names in createFactory. You'd also need to move createFactory implementation into .cpp file.

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