逻辑错误,通过引用传递的数量明显更高
Logic error, number passed by reference significantly higher
下面我粘贴了一些我写的代码。我遇到的问题是,当 turnmessage 将一个数字传递给 found()(通过引用)时,它会以某种方式增加。例如,如果传入 3,我会收到 300 万。
(以下在文件Projectmain.cpp)
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "project1.h"
using namespace std;
project1 game;
string str(int number){
stringstream ss;
ss << number;
string result = ss.str();
return result;
}
void project1::setNames(){
string name;
cout<< "Player one, enter your name: ";
cin >> name;
playerOneName = name;
cout<< "Player two, enter your name: ";
cin >> name;
}
void project1::resetBoard(){
for(int i = 0; i < 3; i++){
RowOne[i] = str(i + 1);
RowTwo[i] = str(i + 4);
RowThree[i] = str(i + 7);
}
int used[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1};
turn = 1;
}
void project1::printBoard(){
for(int i = 0; i < 3; i++){
cout << " | " << RowOne[i] << " | ";
}
cout << endl;
for(int i = 0; i < 3; i++){
cout << " | " << RowTwo[i] << " | ";
}
cout << endl;
for(int i = 0; i < 3; i++){
cout << " | " << RowThree[i] << " | ";
}
cout << endl;
}
string project1::PlayerOneName(){
return playerOneName;
}
string project1::PlayerTwoName(){
return playerTwoName;
}
bool project1::found(int& number){
cout << " your number after pass: " << number;
int i = 0;
bool found = false;
while(i < 9 && found == false){
cout << used[i];
if(used[i] == number)
found = true;
i++;
}
return found;
}
void project1::turnmessage(){
int number = -1;
bool found;
do{
if(turn == 1)
cout << PlayerOneName();
else
cout <<PlayerTwoName();
cout << " it's your turn!" << endl << "Enter the number you wish to use: ";
cin >> number;
number--;
bool found = game.found(number);
if (found == true)
cout << "Sorry, that has already been used. Please try again.";
}while(found == true);
}
int main(){
game.setNames();
game.resetBoard();
game.printBoard();
game.turnmessage();
return 0;
}
(以下在文件项目1.h中)
#include<string>
#include<iostream>
using namespace std;
class project1{
public:
void setNames();
void resetBoard();
void printBoard();
void turnmessage();
string PlayerOneName();
string PlayerTwoName();
bool found(int&);
private:
int playerOneScore;
int playerTwoScore;
string playerOneName;
string playerTwoName;
string RowOne[3];
string RowTwo[3];
string RowThree[3];
int used[9];
int turn;
};
我认为问题就在这里,resetBoard
:
int used[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1};
这将创建一个名为 used
的局部变量,该变量等于 -1 的数组,而不是更改used
数据成员以保存所有 -1。 若要解决此问题,请尝试将此代码替换为以下内容:
for (int i = 0; i < 9; i++) {
used[i] = -1;
}
那里可能还有其他问题,但这肯定是可疑的。
希望这有帮助!
number
没有更改为某个大值,project1::found
是这个std::cout
的输出让你感到困惑:
while(i < 9 && found == false){
cout << used[i];
如果将其更改为此内容,则很明显发生了什么:
while(i < 9 && found == false){
cout << std::endl << used[i];
^^^^^^^^^
您也可以在此处添加std::endl
:
cout << " your number after pass: " << number << std::endl ;
相关文章:
- 如何修复此错误:未定义对"距离(浮点数,浮点数,浮点数,浮点数,浮点数)"的引用
- 我的项目不会像"undefined reference to `grpc::g_core_codegen_interface'"那样使用未定义的引用错误进行编译
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- C++引用错误
- 已定义函数时出现 G++ "未定义的引用"错误
- 错误:使用通用引用的声明冲突
- 链接 cmake 时出现未定义的引用错误
- 错误:未定义对'oboe::AudioStreamBuilder::openStream(oboe::AudioStream**)'的引用
- 链接器错误:未定义对"Reference_Genome::seq[abi:cxx11]"的引用
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 错误:无法解析对重载函数的引用;你的意思是调用它吗?
- 在 gtest 中初始化堆栈上的引用变量的隔离错误
- 运行时错误:引用绑定到类型为"int"的空指针
- 隐式重新解释引用时强制转换,没有警告/错误
- 只有级联分类器会发出未定义的引用错误
- 错误:未定义对cv::cudacodec::createVideoReader的引用
- 模板流运算符重载错误:引用初始化无效,与basic_istream和basic_ifstream之间的差异有关
- 对于我的 ComplexNumber 中的某些方法,我得到了一个奇怪的未定义错误引用.cpp,不过我对模板很陌生
- 从对象调用成员对象,错误:引用非常量值的初始值必须是左值
- Qt并发错误:引用非静态成员