繁体   English   中英

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

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

我的代码中有许多错误,似乎无法解决问题。 当我运行代码时,我有3种不同类型的错误:

  • 非法成员初始化
  • “类”类型的重新定义
  • '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): 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.

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