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.
Syntax error: identifier `MercedesFactory`?
-
04-06-2022 - |
문제
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.
해결책
다른 팁
You should declare MercedesFactory and others before you use class names in createFactory. You'd also need to move createFactory implementation into .cpp file.
제휴하지 않습니다 StackOverflow