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

C++ my initialized string disappears when I leave the default constructor but my other member variables values don't

本文关键字:我的 其他 成员 变量值 消失了 初始化 我离开 默认 构造函数 C++ 字符串      更新时间:2023-10-16

我正在尝试测试一个刽子手程序。我遇到的问题是在我的默认构造函数中我初始化了我的成员变量。错误和 Guess 的值被保存,所以没有问题,但我的其他成员变量THE_WORD和到目前为止没有被保存。我想虽然我的问题出在我的头文件上。我想我在我的 Hangman 文件中搞砸了 THE_WORD 年和迄今为止的成员变量的声明。如果有人可以帮助解决我的问题(给我我也喜欢的解决方案),我将不胜感激,因为这已经吞噬了我的大脑很长时间。谢谢!

在我的主 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

     }

在我的播放器.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);
}

更新::在我的短代码中,我的test.h文件中有

#ifndef test_test_h
#define test_test_h
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;

class test{
public:
    test();
    void setName();
    string getName();
private:
    string name;
};
#endif

在我的实现 cpp 文件中,我有:

#include "test.h"
test::test()
{
    name="hi";
}
void test::setName()
{
    cout<<"Input name"<<endl;
    cin>>name;
}
string test::getName()
{
    return name;
}

在我的主文件中,我有:

#include "test.h"
int main(){
    test student;
    student.setName();
    cout<<student.getName()<<endl;
    test::test();
    cout<<student.getName()<<endl;//this prints out the name i set earlier instead of the default name which I don't understand

}

你在刽子手 ctor 中声明const string THE_WORD,你没有设置成员变量。简单的解决方法就是将THE_WORD放入初始值设定项列表中。

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

编辑; 好吧,您的CTOR需要被发送到words才能正常工作。

    const string THE_WORD = words[2];
    string soFar(THE_WORD.size(),'-');

创建不同于类成员变量的局部变量。

溶液

THE_WORD必须在构造函数初始化列表中初始化,因为它的类型是 const string

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

Hangman::Hangman() : THE_WORD("HANGMAN"), soFar(THE_WORD.size(),'-') {
...
}

Hangman::Hangman() : THE_WORD("HANGMAN") {
   ...
   soFar = string(THE_WORD.size(),'-');
}

您可以通过提供一个返回对THE_WORD的引用的函数来避免存储的需要。

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

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

Hangman声明中:

string THE_WORD;//I think the issue is here
private:
void setTheWord( string newWord );

在构造函数中:

setTheWord( words[2] );