错误C2440:'初始化' :无法转换为' BaseMenu' to' BaseMenu *'

时间:2017-05-30 02:05:39

标签: c++

我真的希望你们都能帮我解决这个问题。我是网站的新手,也是C ++的新手(只学习了大约一个月)。我的编译器是VS2012。我正在为一个程序设置一组菜单。菜单为每个类使用简单的switch语句。每个菜单都源自基本菜单类。由于菜单类本身似乎没有任何问题,因此我现在不包括它们,因为它们都在单独的.cpp文件中,并带有自己的.h头文件。我包括基本菜单.cpp和.h文件,因为我认为它们是我的问题的一部分。我还包括我的主.cpp文件。

BaseMenu.h

    #ifndef BaseMenu_M
    #define BaseMenu_M


    #include<string>
    #include<iostream>



    using std::cout;


    class BaseMenu
    {
    public:

        BaseMenu() { m_MenuText = "This is where the menu text choices will appear"; }// Constructor providing menu text to each derived menu
        virtual ~BaseMenu() { }             // virtual destructor
        virtual BaseMenu getNextMenu(int iChoice, bool& iIsQuitOptionSelected);         // used to set up the framework
        virtual void printText()                                                                    // member function to display the menu text
        {
            cout << m_MenuText << std::endl;
        }

    protected:
        std::string m_MenuText;     // string will be shared by all derived classes
    };

    #endif

BaseMenu.cpp

    #include "stdafx.h"
    #include "BaseMenu.h"
    #include<iostream>


    BaseMenu::BaseMenu(void)
    {
    }


    BaseMenu::~BaseMenu(void)
    {
    }

主.cpp文件

    #include "stdafx.h"
    #include "BaseMenu.h"
    #include "TicketSalesMenu.h"
    #include "MainMenu.h"
    #include "ListMenu.h"
    #include "AdministrativeTasksMenu.h"
    #include "basemenu.h"


    #include <iostream>
    #include <string>

    using std::cin;



    int tmain (int argc, _TCHAR* argv[])
    {


        BaseMenu* aCurrentMenu = new MainMenu;          // Pointer to the current working menu

        bool isQuitOptionSelected = false;
        while (!isQuitOptionSelected)                   // set to keep menus running until the quit option is selected
        {

            aCurrentMenu->printText();                  // call and print the menu text for the currently active menu

            int choice = 0;                             // Initializing choice variable and setting it to 0
            cin >> choice;

            BaseMenu* aNewMenuPointer = aCurrentMenu->getNextMenu(choice, isQuitOptionSelected); // This will return a new object, of the type of the new menu we want. Also checks if quit was selected //**This is the line that the error is reported**//

            if (aNewMenuPointer) 
            {
                delete aCurrentMenu;                    // clean up the old menu
        aCurrentMenu = aNewMenuPointer;                 // updating the 'current menu' with the new menu
            }
        }

        return true;    
    }

出于某种原因,我无法弄明白,我收到了

  

错误C2440:&#39;初始化&#39; :无法转换为&#39; BaseMenu&#39;到&#39; BaseMenu *&#39;。此错误位于第35行的主.cpp文件中,即

    "BaseMenu* aNewMenuPointer = aCurrentMenu->getNextMenu(choice, isQuitOptionSelected);"

我看过这个网站和其他人的多个类似问题。我尝试过的解决方案之一导致我的所有菜单类都出现多个链接错误。我花了3天的时间才把错误减少到这一个剩余的错误,我感到很茫然。

1 个答案:

答案 0 :(得分:0)

编译器告诉你这个问题。 getNextMenu返回一个实际的BaseMenu对象,但您尝试将其指定给指向BaseMenu对象的指针。