写了一个简单的抽象工厂,为什么编译不通过

guicaixinni 2008-09-13 12:08:11
#include <iostream>

using namespace std;

class plant {

public:
virtual void do_something() = 0;
};

class plantA : public plant {

public:
plantA() {
cout<<"plant A"<<endl;
}

void do_something() {
cout<<"plant A do something"<<endl;
}
};

class plantB : public plant {

public:
plantB() {
cout<<"plant B"<<endl;
}

void do_something() {
cout<<"plant B do something"<<endl;
}
};

class fruit {

public:
virtual createFruit() = 0;
};

class fruitA : public fruit {
public:
fruitA() {
cout<<"fruit A"<<endl;
}

void createFruit() {
cout<<"create friut A"<<endl;
}
};


class fruitB : public fruit {
public:
fruitB() {
cout<<"fruit B"<<endl;
}

void createFruit() {
cout<<"create fruit B"<<endl;
}
};

class abstractFactory {
public:
virtual createInstance() = 0;
};

class factoryA : public abstractFactory{
public:
factoryA() {
}

void createInstance() {
plantA *ap = new plantA;
friutA *fP = new fruitA;
}
};


class factoryB : public abstractFactory {
public:
factoyB() {
}

void createInstance() {
plantB *bp = new plantB;
friutB *fP = new fruitB;
}
};


void main()
{

abstractFactory *creater = new factoryA();
creater->createInstance();
}





报错:













main.cpp
D:\S_CODE\template_func2\abstract factory\main.cpp(47) : error C2555: 'fruitA::createFruit' : overriding virtual function differs from 'fruit::createFruit' only by return type or calling convention
D:\S_CODE\template_func2\abstract factory\main.cpp(35) : see declaration of 'fruit'
D:\S_CODE\template_func2\abstract factory\main.cpp(59) : error C2555: 'fruitB::createFruit' : overriding virtual function differs from 'fruit::createFruit' only by return type or calling convention
D:\S_CODE\template_func2\abstract factory\main.cpp(35) : see declaration of 'fruit'
D:\S_CODE\template_func2\abstract factory\main.cpp(74) : error C2555: 'factoryA::createInstance' : overriding virtual function differs from 'abstractFactory::createInstance' only by return type or calling convention
D:\S_CODE\template_func2\abstract factory\main.cpp(64) : see declaration of 'abstractFactory'
D:\S_CODE\template_func2\abstract factory\main.cpp(84) : warning C4183: 'factoyB': member function definition looks like a ctor, but name does not match enclosing class
D:\S_CODE\template_func2\abstract factory\main.cpp(86) : error C2555: 'factoryB::createInstance' : overriding virtual function differs from 'abstractFactory::createInstance' only by return type or calling convention
D:\S_CODE\template_func2\abstract factory\main.cpp(64) : see declaration of 'abstractFactory'
D:\S_CODE\template_func2\abstract factory\main.cpp(96) : error C2259: 'factoryA' : cannot instantiate abstract class due to following members:
D:\S_CODE\template_func2\abstract factory\main.cpp(69) : see declaration of 'factoryA'
D:\S_CODE\template_func2\abstract factory\main.cpp(96) : warning C4259: 'int __thiscall abstractFactory::createInstance(void)' : pure virtual function was not defined
D:\S_CODE\template_func2\abstract factory\main.cpp(66) : see declaration of 'createInstance'
D:\S_CODE\template_func2\abstract factory\main.cpp(96) : error C2259: 'factoryA' : cannot instantiate abstract class due to following members:
D:\S_CODE\template_func2\abstract factory\main.cpp(69) : see declaration of 'factoryA'
D:\S_CODE\template_func2\abstract factory\main.cpp(96) : warning C4259: 'int __thiscall abstractFactory::createInstance(void)' : pure virtual function was not defined
D:\S_CODE\template_func2\abstract factory\main.cpp(66) : see declaration of 'createInstance'
执行 cl.exe 时出错.

abstract factory.exe - 1 error(s), 0 warning(s)

...全文
80 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
机智的呆呆 2008-09-13
  • 打赏
  • 举报
回复
virtual void createFruit() = 0;
guicaixinni 2008-09-13
  • 打赏
  • 举报
回复
谢谢

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧