C++ C++;当我离开默认构造函数时,初始化字符串消失,但我的其他成员变量值不';T

C++ C++;当我离开默认构造函数时,初始化字符串消失,但我的其他成员变量值不';T,c++,constructor,C++,Constructor,我想测试一个刽子手程序。我遇到的问题是,在默认构造函数中,我初始化了成员变量。错误和猜测的值被保存,因此没有问题,但是我的其他成员变量\u WORD和soFar没有保存。我想我的问题是我的头文件。我想我把我的Hangman文件中的成员变量声明搞砸了。如果有人能帮我解决问题(给我一个我也很喜欢的解决方案),我会非常感激,因为这已经侵蚀了我的大脑很长时间了。谢谢 在我的主cpp文件中 #include <iostream> #include <string> #include

我想测试一个刽子手程序。我遇到的问题是,在默认构造函数中,我初始化了成员变量。错误和猜测的值被保存,因此没有问题,但是我的其他成员变量\u WORD和soFar没有保存。我想我的问题是我的头文件。我想我把我的Hangman文件中的成员变量声明搞砸了。如果有人能帮我解决问题(给我一个我也很喜欢的解决方案),我会非常感激,因为这已经侵蚀了我的大脑很长时间了。谢谢

在我的主cpp文件中

#include <iostream>
#include <string>
#include <vector>
#include "player.h"
#include "hangman.h"

using namespace std;

int main()
{
    Hangman game1;

    while(1)
     {



       game1=Hangman();//error object of type hangman cannot be assigned
                       //because its copy assignment is implicitly delated


     }
#包括
#包括
#包括
#包括“player.h”
#包括“hangman.h”
使用名称空间std;
int main()
{
刽子手游戏1;
而(1)
{
game1=Hangman();//无法分配Hangman类型的错误对象
//因为它的副本分配是隐式增量的
}
在我的player.h文件中,我有:

#ifndef PLAYER_H_
#define PLAYER_H_

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>


using namespace std;

class Player
{
public:
    Player();
    void MakeGuess(char &guess);
    void Win();
    void Lose();
    char Agree();
    void display();
private:
    string name;
    int score;
    string myString;
    char answer;

};

#endif
#ifndef HANGMAN_H_
#define HANGMAN_H_

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
#include "player.h"

using namespace std;

class Hangman
{
public:
    Hangman();
    void Play();
protected:
    Player player2;
    vector<string> words;
    const string THE_WORD;//I think the issue is here
    string soFar;//I think the issue is here
    string used="";
    int wrong;
    char guess;
    const int maxwrong=4;
    //void virtual RespondIncorrectGuess();
};


#endif
    #include "hangman.h"
    #include "player.h"

    Hangman::Hangman()
    {
        wrong=0;
        guess='a';
        words.push_back("MONKEY");
        words.push_back("HANGMAN");
        words.push_back("DIFFICULT");

        const string THE_WORD = words[2];//this might be the key to solving my issue

        cout<<THE_WORD<<endl;

        string soFar(THE_WORD.size(),'-');
        cout<<soFar<<endl;

    }

void Hangman::Play()
{

    cout<<"This is the word now:"<<THE_WORD<<endl;//outputs nothing
    cout<<"Now the number of - is:"<<endl;
    cout<<soFar<<endl;//outputs nothing
    exit(1);
}
#include "test.h"

test::test()
{
    name="hi";

}

void test::setName()
{
    cout<<"Input name"<<endl;
    cin>>name;

}

string test::getName()
{
    return name;
}
\ifndef PLAYER\u H_
#定义PLAYER_H_
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
职业选手
{
公众:
Player();
void MakeGuess(char&guess);
无效赢();
无效丢失();
char Agree();
void display();
私人:
字符串名;
智力得分;
字符串myString;
答案;
};
#恩迪夫
在我的刽子手档案中,我有:

#ifndef PLAYER_H_
#define PLAYER_H_

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>


using namespace std;

class Player
{
public:
    Player();
    void MakeGuess(char &guess);
    void Win();
    void Lose();
    char Agree();
    void display();
private:
    string name;
    int score;
    string myString;
    char answer;

};

#endif
#ifndef HANGMAN_H_
#define HANGMAN_H_

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
#include "player.h"

using namespace std;

class Hangman
{
public:
    Hangman();
    void Play();
protected:
    Player player2;
    vector<string> words;
    const string THE_WORD;//I think the issue is here
    string soFar;//I think the issue is here
    string used="";
    int wrong;
    char guess;
    const int maxwrong=4;
    //void virtual RespondIncorrectGuess();
};


#endif
    #include "hangman.h"
    #include "player.h"

    Hangman::Hangman()
    {
        wrong=0;
        guess='a';
        words.push_back("MONKEY");
        words.push_back("HANGMAN");
        words.push_back("DIFFICULT");

        const string THE_WORD = words[2];//this might be the key to solving my issue

        cout<<THE_WORD<<endl;

        string soFar(THE_WORD.size(),'-');
        cout<<soFar<<endl;

    }

void Hangman::Play()
{

    cout<<"This is the word now:"<<THE_WORD<<endl;//outputs nothing
    cout<<"Now the number of - is:"<<endl;
    cout<<soFar<<endl;//outputs nothing
    exit(1);
}
#include "test.h"

test::test()
{
    name="hi";

}

void test::setName()
{
    cout<<"Input name"<<endl;
    cin>>name;

}

string test::getName()
{
    return name;
}
\ifndef HANGMAN\u H_
#定义刽子手_
#包括
#包括
#包括
#包括
#包括
#包括
#包括“player.h”
使用名称空间std;
阶级刽子手
{
公众:
刽子手();
无效播放();
受保护的:
玩家2;
向量词;
const string THE_WORD;//我想问题就在这里
string soFar;//我想问题就在这里
使用的字符串=”;
int错误;
猜字符;
常数int maxError=4;
//无效虚拟响应IncorrectGuess();
};
#恩迪夫
在我的实施文件中,我有:

#ifndef PLAYER_H_
#define PLAYER_H_

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>


using namespace std;

class Player
{
public:
    Player();
    void MakeGuess(char &guess);
    void Win();
    void Lose();
    char Agree();
    void display();
private:
    string name;
    int score;
    string myString;
    char answer;

};

#endif
#ifndef HANGMAN_H_
#define HANGMAN_H_

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
#include "player.h"

using namespace std;

class Hangman
{
public:
    Hangman();
    void Play();
protected:
    Player player2;
    vector<string> words;
    const string THE_WORD;//I think the issue is here
    string soFar;//I think the issue is here
    string used="";
    int wrong;
    char guess;
    const int maxwrong=4;
    //void virtual RespondIncorrectGuess();
};


#endif
    #include "hangman.h"
    #include "player.h"

    Hangman::Hangman()
    {
        wrong=0;
        guess='a';
        words.push_back("MONKEY");
        words.push_back("HANGMAN");
        words.push_back("DIFFICULT");

        const string THE_WORD = words[2];//this might be the key to solving my issue

        cout<<THE_WORD<<endl;

        string soFar(THE_WORD.size(),'-');
        cout<<soFar<<endl;

    }

void Hangman::Play()
{

    cout<<"This is the word now:"<<THE_WORD<<endl;//outputs nothing
    cout<<"Now the number of - is:"<<endl;
    cout<<soFar<<endl;//outputs nothing
    exit(1);
}
#include "test.h"

test::test()
{
    name="hi";

}

void test::setName()
{
    cout<<"Input name"<<endl;
    cin>>name;

}

string test::getName()
{
    return name;
}
#包括“hangman.h”
#包括“player.h”
刽子手:刽子手
{
错误=0;
猜一猜;
字。推回(“猴子”);
字。推回(“刽子手”);
单词。推回(“困难”);
const string THE_WORD=words[2];//这可能是解决我的问题的关键

如果您在Hangman ctor中声明
const string单词
,那么您没有设置成员变量。简单的解决方法就是将
单词
放入初始值设定项列表中

Hangman::Hangman() : THE_WORD(words[2]) /* other members should be init here */
{ ... }

编辑;嗯,你的ctor需要发送
words
才能工作。

你在Hangman ctor中声明
const字符串,你没有设置成员变量。简单的解决方法就是将
单词
放在初始值设定项列表中

Hangman::Hangman() : THE_WORD(words[2]) /* other members should be init here */
{ ... }

编辑;嗯,您的ctor需要发送
单词才能正常工作。

问题出在构造函数中。您正在声明与成员数据同名的局部变量。只需删除类型部分,构造函数就会使用您想要的数据成员。很好

在刽子手声明中:

string THE_WORD;//I think the issue is here
private:
void setTheWord( string newWord );
在构造器中:

setTheWord( words[2] );

问题出在构造函数中。您正在声明与成员数据同名的局部变量。只需删除类型部分,构造函数就会使用您想要的数据成员。很好

在刽子手声明中:

string THE_WORD;//I think the issue is here
private:
void setTheWord( string newWord );
在构造器中:

setTheWord( words[2] );
台词

    const string THE_WORD = words[2];

    string soFar(THE_WORD.size(),'-');
创建与类成员变量不同的局部变量

解决方案

必须在构造函数初始化列表中初始化\u单词
,因为它的类型是
常量字符串

soFar
也可以在初始化列表中初始化。也可以在构造函数主体中为其赋值

使用

您可以通过提供一个返回对该单词的引用的函数来避免存储该单词

const std::string& getTheWord() const { return words[2]; }
台词

    const string THE_WORD = words[2];

    string soFar(THE_WORD.size(),'-');
创建与类成员变量不同的局部变量

解决方案

必须在构造函数初始化列表中初始化\u单词
,因为它的类型是
常量字符串

soFar
也可以在初始化列表中初始化。也可以在构造函数主体中为其赋值

使用

您可以通过提供一个返回对该单词的引用的函数来避免存储该单词

const std::string& getTheWord() const { return words[2]; }

您正在声明一个名为
的新的短期变量,该变量是隐藏成员的单词。@Biffen我以前尝试过,但后来我收到一个错误,它说没有可行的重载“=”您甚至需要将它放在初始化列表中,因为它是使用
-Wshadow
编译的常量(或编译器的任何标志)本可以帮助您理解这一点。请将
const string THE_WORD
更改为
string THE_WORD
。拥有
const
成员变量几乎总是一个坏主意。@MattMcNabb稍后在代码末尾,我调用了默认构造函数:Hangman::Hangman();(这样我可以重置《刽子手》游戏,因此我可以重置我的成员变量,但一旦我离开作用域,它们就不会被保存?我不明白为什么会发生这种情况。你正在声明一个新的短期变量,名为
the_WORD
,用于隐藏成员。@Biffen我以前尝试过,但后来我收到一个错误,说不可行。)重载“=”您甚至需要将其放在初始化列表中,因为它是使用
-Wshadow
进行常量编译的(或编译器的任何标志)本可以帮助您理解这一点。请将
const-string\u-WORD
更改为
string\u-WORD
。拥有
const
成员变量几乎总是一个坏主意。@MattMcNabb