【发布时间】:2013-04-07 15:55:54
【问题描述】:
我的代码中有许多错误,我似乎无法解决问题。 当我运行代码时,我有 3 种不同类型的错误:
- 非法成员初始化
- 'class'类型重定义
- 'return':无法转换
我想做的是创建一个空间模拟器,它有一个菜单驱动的前端。 下面的代码能够从以下可能性列表中选择可能在太空中找到的项目:小行星、彗星、飞碟、火箭、空间站、宇航员、卫星、垃圾、月球。
然后需要在 spaceItem 工厂中创建此项目。
抽象类Item.h
class AItems // error C2011: 'AItems' : 'class' type redefinition
{
std::string name;
public:
AItems(std::string n)
:name(n)
{
}
//This method is for testing only
std::string GetName()
{
return name;
}
};
SpaceItem.h
#include <list>
#include <algorithm>
#include <stdexcept>
#include <memory>
#include <iostream>
using namespace std;
#include "Abstract Class Item.h"
//Lets declare all the concrete ITEMS
class Asteroid : public AItems // error C2504: 'AItems' : base class undefined
{
public: Asteroid() // error spaceitems.h(14): error C2614: 'Asteroid' : illegal member initialization: 'AItems' is not a base or member
:AItems("Asteroid"){}
};
class Comet : public AItems
{
public: Comet()
:AItems("Comet"){}
};
class FlyingSaucer : public AItems
{
public: FlyingSaucer()
:AItems("Flying Saucer"){}
};
class Rocket : public AItems
{
public: Rocket()
:AItems("Rocket"){}
};
class SpaceStation : public AItems
{
public : SpaceStation()
:AItems("Space Station"){}
};
class Astronaut : public AItems
{
public : Astronaut()
:AItems("Astronaut"){}
};
class Satellite : public AItems
{
public : Satellite()
:AItems("Satellite"){}
};
class Junk : public AItems
{
public : Junk()
:AItems("Junk"){}
};
class Moon : public AItems
{
public : Moon()
:AItems("Moon"){}};
SelectItem.h
#include <list>
#include <algorithm>
#include <stdexcept>
#include <memory>
#include <iostream>
using namespace std;
#include "Abstract Class Item.h"
#include "SpaceItems.h"
class SelectItem
{
public:
enum ITEM_TYPE
{
ASTEROID,
COMET,
FLYINGSAUCER,
ROCKET,
SPACESTATION,
ASTRONAUT,
SATELLITE,
JUNK,
MOON
};
static AItems* createItem(ITEM_TYPE itemType)
{
switch (itemType)
{
case ASTEROID:
return new Asteroid(); //error C2440: 'return' : cannot convert from 'Asteroid *' to 'AItems *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
case COMET:
return new Comet();
case FLYINGSAUCER:
return new FlyingSaucer();
case ROCKET:
return new Rocket();
case SPACESTATION:
return new SpaceStation();
case ASTRONAUT:
return new Astronaut();
case SATELLITE:
return new Satellite();
case JUNK:
return new Junk();
case MOON:
return new Moon();
}
throw "Invalid Item Type";
}
};
main.cpp
#include <list>
#include <algorithm>
#include <iostream>
using namespace std;
#include "Select Item.h"
void item_information (SelectItem::ITEM_TYPE itemtype)
{
AItems* createItem = SelectItem::createItem(itemtype);
std::cout << "Item Type" << std::endl;
std::cout << itemtype << std::endl;
delete createItem; // warning C4150: deletion of pointer to incomplete type 'AItems'; no destructor called
}
int main()
{
item_information (SelectItem::ASTEROID);
item_information (SelectItem::COMET);
item_information (SelectItem::FLYINGSAUCER);
item_information (SelectItem::ROCKET);
item_information (SelectItem::SPACESTATION);
item_information (SelectItem::ASTRONAUT);
item_information (SelectItem::SATELLITE);
item_information (SelectItem::JUNK);
item_information (SelectItem::MOON);
}
请你帮我看看哪里出错了。 谢谢
这里是错误列表:
spaceitems.h(13): 错误 C2504: 'AItems' : 基类未定义
spaceitems.h(14):错误 C2614:“Asteroid”:非法成员初始化:“AItems”不是基础或成员
spaceitems.h(19): 错误 C2504: 'AItems' : 基类未定义
spaceitems.h(20):错误 C2614:“彗星”:非法成员初始化:“AItems”不是基础或成员
spaceitems.h(25): 错误 C2504: 'AItems' : 基类未定义
spaceitems.h(26):错误 C2614:“FlyingSaucer”:非法成员初始化:“AItems”不是基础或成员
spaceitems.h(31): 错误 C2504: 'AItems' : 基类未定义
spaceitems.h(32):错误 C2614:“Rocket”:非法成员初始化:“AItems”不是基础或成员
spaceitems.h(37): 错误 C2504: 'AItems' : 基类未定义
spaceitems.h(38):错误 C2614:“SpaceStation”:非法成员初始化:“AItems”不是基础或成员
spaceitems.h(43): 错误 C2504: 'AItems' : 基类未定义
spaceitems.h(44):错误 C2614:“宇航员”:非法成员初始化:“AItems”不是基础或成员
spaceitems.h(49): 错误 C2504: 'AItems' : 基类未定义
spaceitems.h(50): error C2614: 'Satellite' : 非法成员初始化: 'AItems' 不是基础或成员
spaceitems.h(55): 错误 C2504: 'AItems' : 基类未定义
spaceitems.h(56):错误 C2614:“垃圾”:非法成员初始化:“AItems”不是基础或成员
spaceitems.h(61): 错误 C2504: 'AItems' : 基类未定义
spaceitems.h(62):错误 C2614:“Moon”:非法成员初始化:“AItems”不是基础或成员
select item.h(33): error C2440: 'return' : 无法从 'Asteroid *' 转换为 'AItems *' 指向的类型是不相关的;转换需要 reinterpret_cast、C-style cast 或 function-style cast
select item.h(35): error C2440: 'return' : 无法从 'Comet *' 转换为 'AItems *' 指向的类型是不相关的;转换需要 reinterpret_cast、C-style cast 或 function-style cast
select item.h(37): error C2440: 'return' : 无法从 'FlyingSaucer *' 转换为 'AItems *' 指向的类型是不相关的;转换需要 reinterpret_cast、C-style cast 或 function-style cast
select item.h(39): 错误 C2440: 'return' : 无法从 'Rocket *' 转换为 'AItems *' 指向的类型是不相关的;转换需要 reinterpret_cast、C-style cast 或 function-style cast
select item.h(41): error C2440: 'return' : 无法从 'SpaceStation *' 转换为 'AItems *' 指向的类型是不相关的;转换需要 reinterpret_cast、C-style cast 或 function-style cast
select item.h(43): error C2440: 'return' : 无法从 'Astronaut *' 转换为 'AItems *' 指向的类型是不相关的;转换需要 reinterpret_cast、C-style cast 或 function-style cast
select item.h(45): error C2440: 'return' : 无法从 'Satellite *' 转换为 'AItems *' 指向的类型是不相关的;转换需要 reinterpret_cast、C-style cast 或 function-style cast
select item.h(47): error C2440: 'return' : 无法从 'Junk *' 转换为 'AItems *' 指向的类型是不相关的;转换需要 reinterpret_cast、C-style cast 或 function-style cast
select item.h(49): error C2440: 'return' : 无法从 'Moon *' 转换为 'AItems *' 指向的类型是不相关的;转换需要 reinterpret_cast、C-style cast 或 function-style cast
main.cpp(13):警告 C4150:删除指向不完整类型“AItems”的指针;没有调用析构函数
我已经解决了这个问题。但是,它没有做我想做的事。我希望能够选择一个项目,然后显示该项目。
而是显示以下内容
Item Type
0
Item Type
1
Item Type
2
以此类推,直到数字 8。
程序应该向我询问项目并列出该特定项目,有人可以帮忙吗?
【问题讨论】:
-
你有包括警卫吗?
-
请发布完整错误列表,并在源中显示错误所在(行号在消息中)。
-
将所有代码放在一个文件中似乎没有显示任何错误,因此看起来最有可能的问题是缺少
include guards,或者如果您的编译器支持它#pragma once,但错误消息会要确定这一点还有很长的路要走 -
如果你已经解决了这个问题,如果没有其他答案是正确的,你可能应该添加一个答案来说明你做了什么。或者至少用解决方案更新您的问题。如果您还有其他问题,那么您应该将其作为另一个问题发布。
-
我不会数到49。请更清楚地指出哪一行产生了哪个错误。
标签: c++ visual-studio-2010 visual-c++