简体   繁体   English

C ++代码错误(非法成员初始化,“类”类型的重新定义,“返回”:无法转换)

[英]C++ Error in code (illegal member initialization, 'class' type redefinition, 'return' : cannot convert)

I have a number of errors in my code that I can't seem to fix the problem. 我的代码中有许多错误,似乎无法解决问题。 When I run the code I have 3 different type of errors: 当我运行代码时,我有3种不同类型的错误:

  • illegal member initialization 非法成员初始化
  • 'class' type redefinition “类”类型的重新定义
  • 'return' : cannot convert 'return':无法转换

What I'm trying to do is create a space simulator, which have a menu driven front end. 我正在尝试创建一个空间模拟器,该模拟器具有一个菜单驱动的前端。 The code below is to have the ability to select an item that can be potentially found in space from the following list of possibilities: asteroid, comet, flying saucer, rocket, space station, astronaut, satellite, junk, moon. 下面的代码可以从以下可能性中选择可以在太空中找到的物品:小行星,彗星,飞碟,火箭,空间站,宇航员,卫星,垃圾,月球。

This item will then need to be created in an spaceItem factory. 然后需要在spaceItem工厂中创建该项目。

Abstract Class Item.h 抽象类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 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 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 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);
}

Can you help me see where I'm going wrong please. 您能帮我看看我要去哪里了。 Thank you 谢谢

Here is the list of errors: 这是错误列表:

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

and so on until number 8. 依此类推,直到编号8。

The program should ask me for item and list that particular item, can anyone help with this please? 该程序应要求我提供项目并列出该特定项目,任何人都可以帮忙吗?

I'd say put in some double include guards, as stated! 我会说,如前所述,加入了一些双重包括卫队!

But, I did try, and visual studio/c++ 2010 compiles this just fine: 但是,我确实尝试过,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);
}

You need include guards . 您需要包括警卫

If you include a header multiple times in a source file, the compiler will complain about multiple definitions. 如果您在源文件中多次包含标头,则编译器将抱怨多个定义。 It's because the header files are quite literally included into the source file. 这是因为头文件确实包含在源文件中。

The most common and standard way of doing this is to use the preprocessor to make sure contents are not included more than once. 最常见的标准方法是使用预处理器,以确保不多次包含内容。

Some random header file: 一些随机头文件:

#ifndef SOME_RANDOM_HEADER_FILE
#define SOME_RANDOM_HEADER_FILE

// Contents of header file

#endif // SOME_RANDOM_HEADER_FILE

This will make sure that everything between the #ifndef and #endif will only be included once in a source file. 这样可以确保#ifndef#endif之间的所有内容在源文件中仅包含一次。

There is also a newer thing, used mostly by Visual C++, called #pragma once . 还有一种较新的东西,它主要由Visual C ++使用, #pragma once称为#pragma once You place it at the top of your header file and the compiler will make sure that the header file is never included more than once in a source file. 将其放在头文件的顶部,编译器将确保头文件在源文件中不会被多次包含。

It should also be noted that these methods won't stop a header file from being included in multiple source files. 还应注意,这些方法不会阻止头文件包含在多个源文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM