【问题标题】:c++ accessors mutators not reading cin switch case statement时间:2019-05-10 标签:c++accessorsmutatorsnotreadingcin switch case statement
【发布时间】:2017-03-13 05:35:37
【问题描述】:

我为此使用 Ubuntu 终端 g++ 编译器,我很难调试

这是我的代码: 嘘

 class S
    {
    protected: 
        string name;
        bool space;
    public:
        S();
        S(string,bool);       
        void setName(string name);
    string getName();
};

在我的 S.cpp 中

string S::getName() 
{   
    return (name);  
}

void S::setName(string name) // I tried to change to other variable names but gave me segmentation errors
{
   name = name;
}

S::S() // default constructor
{
    name = "";
    space = true;
}

    S::S(string name, bool space) // non-default constructor
{
        name = name;
        space= space;
    }

我有另一个类,我要实现流程 在我的 Main.cpp 中

    void Main::mainmenu()
{
    //cout displays//

    cout<<"Please input your choice: ";
    cin>>choice;
    option(choice);
}
    void Main::option(int choice)
{

    static string shapename;
    static bool space;
    const int size = 100;
    static S s[size];
    static int number =0;
    static int count = 0;

    while(choice !=999)
    { 

        switch(choice)
        {
            case 1: 
            {
                cout<<"Please enter name of Shape:";
                cin>>shapename;
                s[size].setName(shapename);
                cin.clear();
                cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                  count++;
                mainmenu();
                cin>>number;
                option(number);

            }
            break;
            case 2:
            {

                 for(int x = 0;x<count;x++)
                {   
                    cout<<count<<endl;
                    cout<<shapename<<endl; // this is to test if the string reaches case 2 and it displays the correct input
                    cout<<s[x].getName()<<endl; // however this method prints a blank.
                }

                mainmenu();
                cin>>number;
                option(number); 
            }
            break;
            default:break;
        } // end of switch      
    } // end of while  
}

我只是在我的int main() 中运行mainmenu()

我正在尝试在我的 main.cpp 上做一个切换案例,其中案例 1 我让用户输入他们想要的名称,我使用类 S 中的 set 方法来获取名称,在我的案例 2 中,我想要使用 get 方法检索并打印出用户输入的名称。

我将 static 放在我的声明前面,因为我认为调用可能会影响值。

如何使用 switch case 语句检索我的 get/set 方法?我正在使用 Ubuntu 终端来执行此操作,这对我来说很难看到传入的值。

【问题讨论】:

  • 运行多次后你会得到啊,呃,堆栈溢出。再想想代码的逻辑

标签: c++ while-loop switch-statement accessor mutators


【解决方案1】:

让我们看看这是否能帮到你一点。

考虑一下:

void S::setName(string name)
{
   name = name;
}

你在这里所做的是用 name 的值初始化变量 name,也就是你用它自己的值设置 name。

这称为可变阴影。

为避免这种情况,最佳做法是为您的私有数据成员添加前缀,例如

class S
{
protected: 
    string _name;
    bool _space;
    ...
};

因此,例如,您的 setter 将是

void S::setName(string name) 
{
   _name = name;
}

另一种方法是使用 this 显式访问您的成员,如下所示

void S::setName(string name) 
{
   this->name = name;
}

如果你想为你的数据成员添加前缀,使用 this 或两者都使用,这是一个个人喜好问题。

【讨论】:

  • 前缀是个好主意,但在使用下划线之前you should be aware of the rules.
  • 是的,我完全同意@user4581301
  • 在最后一个示例中,您应该显示私有成员名称的定义。
【解决方案2】:
 s[size].setName(shapename);

总是写入同一个位置,并且它在数组之外。很确定你想要

 s[count].setName(shapename);

一旦你修复了这个令人讨厌的问题,你就可以处理setName 中的名称阴影错误,而不会触发段错误。

题外话:

所有这些静态变量都应该是类成员变量。

在while 循环中递归不是一个好的选择。两者都试图实现相同的目标。

【讨论】:

    猜你喜欢
    • 2020-06-15
    • 2020-10-01
    • 2015-06-14
    • 2011-07-08
    • 2012-02-03
    • 2017-04-15
    • 2021-07-31
    • 2016-01-27
    • 1970-01-01
    相关资源
    最近更新 更多