gpt4 book ai didi

c++ - 类对象(char)在函数后发生变化

转载 作者:行者123 更新时间:2023-11-28 00:00:53 25 4
gpt4 key购买 nike

我是 C++ 的新手,但这让我很困惑。我正在研究 RPG 的基本代码,但类的这个角色让我感到难过。我在这里隔离了有问题的部分(删掉了 1000 行),问题仍然存在。

这是程序的类和 header :

#include <iostream>
#include <cstdlib>
using namespace std;

unsigned long errorcount;
// I know this is bad coding, but it's not going to be in the end product...

class character {
public:
void setgender(char newgender);
char getgender() const;
private:
char gender;
};

void character::setgender(char newgender) {
switch (newgender) {
case 'M': gender = 'M'; break;
case 'F': gender = 'F'; break;
default: gender = '0'; errorcount++; break;
}
std::cout << "\nDuring setgender function: " << gender;
return;
}

char character::getgender() const {
std::cout << "\nDuring getgender function: " << gender;
return gender;
}

接下来的部分让我摸不着头脑。我开始了以下代码:

void PlayerCharacterCreation(character Player) {
string newgender;
while(true) {
std::cout << "\nAre you male or female?" << "\n1. Male" << "\n2. Female" << "\n::";
std::cin >> newgender;
if (newgender == "1") { Player.setgender('M'); break; }
if (newgender == "2") { Player.setgender('F'); break; }
std::cout << "\nInvalid response. Try again.";
}
std::cout << "\nAfter setgender function: " << Player.getgender();
}

void PlayerCreationTest() {
character Test;
PlayerCharacterCreation(Test);
char playergender = Test.getgender();
if (playergender != 'M' && playergender != 'F') { errorcount++; }
std::cout << "\nAfter getgender function: " << playergender;
std::cout << "\n\nOUTPUT BEGINS NOW\nGender: " << playergender << "\n";
std::cout << "OUTPUT ENDS. Total Errors: " << errorcount << ".";
return;
}

int main() {
PlayerCreationTest();
return 0;
}

现在据我所知,这一切都没有问题 - (GCC) 编译器不会报错,并且它在一定程度上工作得很好。但是如果我运行它,我会得到以下输出:

Are you male or female?
1. Male
2. Female
1
During setgender function: M
During getgender function: M
After setgender function: M
During getgender function: @
After getgender function: @
OUTPUT BEGINS NOW
Gender: @
OUTPUT ENDS. Total Errors: 1.

更糟糕的是,如果我选择选项“2”,则输出只有在它没有意义时才相同:

Are you male or female?
1. Male
2. Female
2
During setgender function: F
During getgender function: F
After setgender function: F
During getgender function: @
After getgender function: @
OUTPUT BEGINS NOW
Gender: @
OUTPUT ENDS. Total Errors: 1.

换句话说,预期的输出在 PlayerCharacterCreation() 的最后一行和 PlayerCreationTest() 的下一行之间的某处出现严重错误。

不过,据我所知,“角色”类应该在函数之间保持不变,而不是像这样随意改变。

我希望这足以让别人弄清楚我做错了什么,但我稍微尝试了一下,并设法进一步改变了输出字符。

通过在主函数的开头添加“srand(0)”行,我可以将选项 1 和选项 2 的“@”更改为“y”。

通过在主函数的开头添加“GenderTest()”行,我可以将“@”更改为“F”,用于两个选项。如果我添加这两行,那么只有紧靠“PlayerCreationTest()”行上方的那一行似乎很重要。这很奇怪,因为完整的代码总是返回一个“l”(小写的 L)而不是“@”,而且 main 函数与上面写的完全一样。

最佳答案

As far as I can tell, though, the "character" class should stay the same between functions, not change all willy-nilly like this.

嗯,你错了。它们确实保持不变,因为它们是独立的变量PlayerCharacterCreation创建本地 character (Test 的拷贝),在函数结束时,对象被销毁。

原文character你传递给PlayerCharacterCreation从未改变,你会得到一些奇怪的输出,因为从未为 character 设置性别.

PlayerPlayerCharacterCreation是一个全新的character , 它不是 Test :)

如果要修改character传递给 PlayerCharacterCreation ,你必须通过引用传递它(还有一些其他方法,比如传递指针,返回 Player ,但这是最好的方法):

void PlayerCharacterCreation(character& Player);
^^^
reference

关于c++ - 类对象(char)在函数后发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39050365/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com