简体   繁体   中英

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:

  • illegal member initialization
  • 'class' type redefinition
  • 'return' : cannot convert

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.

Abstract Class 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);
}

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.

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:

#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.

There is also a newer thing, used mostly by Visual C++, called #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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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