[英]C++ Error in code (illegal member initialization, 'class' type redefinition, 'return' : cannot convert)
我的代碼中有許多錯誤,似乎無法解決問題。 當我運行代碼時,我有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。
該程序應要求我提供項目並列出該特定項目,任何人都可以幫忙嗎?
我會說,如前所述,加入了一些雙重包括衛隊!
但是,我確實嘗試過,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);
}
您需要包括警衛 。
如果您在源文件中多次包含標頭,則編譯器將抱怨多個定義。 這是因為頭文件確實包含在源文件中。
最常見的標准方法是使用預處理器,以確保不多次包含內容。
一些隨機頭文件:
#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
稱為#pragma once
。 將其放在頭文件的頂部,編譯器將確保頭文件在源文件中不會被多次包含。
還應注意,這些方法不會阻止頭文件包含在多個源文件中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.