我的代码中有很多错误,似乎无法解决问题。 当我运行代码时,我有3种不同类型的错误:
我要做的是创建一个空间模拟器,它有一个菜单驱动的前端。 下面的代码是能够从以下可能性列表中选择可能在太空中找到的物品:小行星,彗星,飞碟,火箭,空间站,宇航员,卫星,垃圾,月亮。
然后需要在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): error C2504: 'AItems' : base class undefined
spaceitems.h(14): error C2614: 'Asteroid' : illegal member initialization: 'AItems' is not a base or member
spaceitems.h(19): error C2504: 'AItems' : base class undefined
spaceitems.h(20): error C2614: 'Comet' : illegal member initialization: 'AItems' is not a base or member
spaceitems.h(25): error C2504: 'AItems' : base class undefined
spaceitems.h(26): error C2614: 'FlyingSaucer' : illegal member initialization: 'AItems' is not a base or member
spaceitems.h(31): error C2504: 'AItems' : base class undefined
spaceitems.h(32): error C2614: 'Rocket' : illegal member initialization: 'AItems' is not a base or member
spaceitems.h(37): error C2504: 'AItems' : base class undefined
spaceitems.h(38): error C2614: 'SpaceStation' : illegal member initialization: 'AItems' is not a base or member
spaceitems.h(43): error C2504: 'AItems' : base class undefined
spaceitems.h(44): error C2614: 'Astronaut' : illegal member initialization: 'AItems' is not a base or member
spaceitems.h(49): error C2504: 'AItems' : base class undefined
spaceitems.h(50): error C2614: 'Satellite' : illegal member initialization: 'AItems' is not a base or member
spaceitems.h(55): error C2504: 'AItems' : base class undefined
spaceitems.h(56): error C2614: 'Junk' : illegal member initialization: 'AItems' is not a base or member
spaceitems.h(61): error C2504: 'AItems' : base class undefined
spaceitems.h(62): error C2614: 'Moon' : illegal member initialization: 'AItems' is not a base or member
select item.h(33): 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
select item.h(35): error C2440: 'return' : cannot convert from 'Comet *' to 'AItems *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
select item.h(37): error C2440: 'return' : cannot convert from 'FlyingSaucer *' to 'AItems *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
select item.h(39): error C2440: 'return' : cannot convert from 'Rocket *' to 'AItems *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
select item.h(41): error C2440: 'return' : cannot convert from 'SpaceStation *' to 'AItems *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
select item.h(43): error C2440: 'return' : cannot convert from 'Astronaut *' to 'AItems *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
select item.h(45): error C2440: 'return' : cannot convert from 'Satellite *' to 'AItems *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
select item.h(47): error C2440: 'return' : cannot convert from 'Junk *' to 'AItems *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
select item.h(49): error C2440: 'return' : cannot convert from 'Moon *' to 'AItems *' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
main.cpp(13): warning C4150: deletion of pointer to incomplete type 'AItems'; no destructor called
I have solved the problem. However, it not doing what I wanted it to do. I wanted the ability to select an item and then display the item.
Instead it displaying the following
Item Type 0 Item Type 1 Item Type 2
等等,直到8号。
该程序应该向我询问项目并列出该特定项目,有人可以帮忙吗?
答案 0 :(得分:0)
如上所述,我会说放入一些双重包括守卫!
但是,我确实尝试了,visual studio / c ++ 2010编译就好了:
#include "stdafx.h"
#include <list>
#include <algorithm>
#include <stdexcept>
#include <memory>
#include <iostream>
class AItems
{
std::string name;
public:
AItems(std::string n)
:name(n)
{
}
//This method is for testing only
std::string GetName()
{
return name;
}
};
//Lets declare all the concrete ITEMS
class Asteroid : public AItems
{
public: Asteroid()
: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"){}};
#include <list>
#include <algorithm>
#include <stdexcept>
#include <memory>
#include <iostream>
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();
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";
}
};
#include <list>
#include <algorithm>
#include <iostream>
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;
}
int _tmain(int argc, _TCHAR* argv[])
{
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);
}
答案 1 :(得分:0)
您需要include guards。
如果在源文件中多次包含标头,编译器会抱怨多个定义。这是因为头文件完全包含在源文件中。
最常见和最标准的方法是使用预处理器确保内容不会被包含多次。
一些随机头文件:
#ifndef SOME_RANDOM_HEADER_FILE
#define SOME_RANDOM_HEADER_FILE
// Contents of header file
#endif // SOME_RANDOM_HEADER_FILE
这将确保#ifndef
和#endif
之间的所有内容仅在源文件中包含一次。
还有一个更新的东西,主要由Visual C ++使用,名为#pragma once
。将它放在头文件的顶部,编译器将确保头文件永远不会在源文件中包含多次。
还应注意,这些方法不会阻止头文件包含在多个源文件中。