C++当我离开默认构造函数时,我的初始化字符串消失了,但我的其他成员变量值没有
C++ my initialized string disappears when I leave the default constructor but my other member variables values don't
我正在尝试测试一个刽子手程序。我遇到的问题是在我的默认构造函数中我初始化了我的成员变量。错误和 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] );
相关文章:
- 我应该将除 .cpp 以外的其他文件添加到 git 中吗?
- CMake:在其他目录中找不到我的头文件
- 我的 SonarQube C++扫描成功,但结果仅标记重复项,而没有标记其他标记的位置
- 为什么我的 while 循环对于特定输入中断,而对于其他输入工作正常?
- 钩子SSDT.其他驱动程序使用我的函数
- 我的 c 字符串复制函数正在损坏其他变量的堆栈
- 为什么我的Protobuf类可以解析由其他Protobuf类串联的字符串
- 我有一系列阵列.可以从一个数组中阅读一些元素和另一个数组中的其他元素
- 我应该担心动态代码生成与用C++编写的其他模块不匹配吗?
- 我的代码只会运行主函数,其他函数不会在C++中编译
- 是否有其他方法将.dll文件从一个项目复制到我的启动项目中的可执行文件旁边
- COUT不适用于我的其他Turbo C 合并
- 关于函数调用,我的程序没有读取我的其他函数。为什么?
- 为什么我需要将 -lstdc++ 添加到我的其他链接器标志中
- 如何停止 while 循环并转到我的其他语句
- CMake-在我的当前项目中使用我的其他项目代码
- 我的方形四边目一直在奇怪地旋转,但我的其他四边目旋转得很好
- 我没有发现我的其他活动
- 我的第一个大小工作,但无法为我的其他构造函数设置大小
- C++当我离开默认构造函数时,我的初始化字符串消失了,但我的其他成员变量值没有