内部的变量访问/修改不起作用

时间:2017-02-13 18:48:00

标签: c++ class console codeblocks iostream

我试图制作类似于菜单显示应用程序的东西(具体来说它显示了一个菜单,我选择了一个选项(在这种情况下编号为1,这有效)然后我希望它能给我看一些东西那样:

[Name]
-----------------
[Decription]

Press any key....

按下任何按钮后,我希望它显示父菜单
在这种情况下,它应该像Glowne(主菜单)执行测试项目的show函数,然后它应该返回到Glowne,同时显示:

Test String
---------------
Test Description

Press any key....

问题是我得到了

<empty line>
4763656---------------------------------------
<empty line>
<empty line>

并且应用程序崩溃。

源代码如下:
hpstream.h

#ifndef HPSTREAM_H
#define HPSTREAM_H

#include <iostream>
#include <string>
#include <list>
#include <conio.h>
#include <windows.h>
#include <stdio.h>
using namespace std;

class Item;

class Menu
{
public:
    list<Item> Children;
    list<Item>::iterator it;
    Menu* Parent;
    void ShowMain();
    void SelectOption(int howMany);
    void Show();
    void AddChildren(Item& objekt);

private:
    bool FirstShow;
};

class hpstream
{
public:
    static void GetHelp();
};

class Item
{
public:
    string Name;
    string Description;
    bool isMenu;
    Menu* ChildrenMenu;
    Menu* Parent;
    void Set(string _text, string _Desciption, Menu* _Parent);
    void Show();
};

#endif // HPSTREAM_H

和hpstream.cpp:

#include "hpstream.h"
#include <iostream>
#include <string>
#include <list>
#include <conio.h>
#include <windows.h>
#include <stdio.h>
using namespace std;

Menu MainMenu;

void hpstream::GetHelp()
{
    Item test;
    test.Set("Test String", "Test Description", &MainMenu);
    MainMenu.AddChildren(test);
    MainMenu.Show();
}

void Menu::ShowMain()
{
    MainMenu.Show();
}

void Menu::SelectOption(int howMany)
{
    bool done = false;
    int Option = -1;
    while(!done)
    {
        char character = getch();
        //converts pressed key to number, returns to selected list position (with iterator) and shows it
        if (character >= 49 && character < 49 + howMany) //1-8
        {
            Option = character - 48;
            for (int i = 0; i < (howMany - Option); it--);
            it -> Show();
            done = true;
        }
    }
}

void Menu::Show()
{
    system("cls");
    it = Children.begin();
    for (; it != Children.end();)
    {
        int shown = 0;
        cout << "Select number (for test 1)" << endl;
        for (int i = 1; (i < 9) && (it != Children.end()); it++)
        {
            cout << i << ". " << it->Name << endl;
            shown = i; // so it will contain as many positions as possible but less or equal to 8
        }
        SelectOption(shown); //here it's like SelectOption(1);
    }
}

void Menu::AddChildren(Item& objekt)
{
    Children.push_back(objekt);
}

void Item::Set(string _text, string _Desciption, Menu* _Parent)
{
    this -> Name = _text;
    this -> Description = _Desciption;
    this -> Parent = _Parent;
}

void Item::Show()
{
    if (!isMenu)
    {
        system("cls");
        cout << this -> Name << endl;
        cout << "---------------------------------------" << endl << endl;
        cout << this -> Description << endl << endl;
        cout << "Press any key...";
        getch();
        this -> Parent -> Show();
    }
}

和main.cpp(用于执行):

#include <iostream>
#include "hpstream.h"

using namespace std;

int main()
{
    hpstream::GetHelp();
    return 0;
}

我知道我不应该在同一个档案中放置多个班级,但我真的不得不这样做 Item :: Show()函数中存在问题(它负责生成&#34;接口&#34;在开头显示)

作为&#34;起点&#34;执行的函数是hpstream :: GetHelp(const char * _topic)

如果变量名称存在任何问题,则在翻译源代码时会遗漏它们,问题与此无关。

提前感谢您的帮助。

0 个答案:

没有答案