gpt4 book ai didi

c++ - 不更新数组的继承机器人竞赛

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

我正在开发一个程序,假设有 3 个不同的机器人在赛道上比赛。轨道的长度假定为 100。我刚刚学习了继承,并且仍在尝试了解如何将数据成员从一个 .h 连接到另一个。当我运行我的程序时,当我调用我的任何机器人时没有任何反应。我将以其中一个为例。你能解释一下如何让他们的 Action 更新比赛二维数组吗?

robotRace.h

#ifndef ROBOTRACE_H
#define ROBOTRACE_H

using namespace std;

class robotRace {

public:

robotRace (); //constructor
static const int rows = 5;
static const int columns = 100;

protected:

int race[rows][columns]; //initial base for race floor

};// end superclass robotRace that should do no movement

#endif

robotRace.cpp

#include <iostream>
#include "robotRace.h"

using namespace std;

robotRace :: robotRace() {

for (int i = 0; i < rows; i++)

for (int j= 0; j<columns; j++)

race[i][j] = ' ';

}//end constructor

这是机器人之一及其更新数组的功能。不确定如何让它发挥作用。

FunctionRobot.h

#ifndef FUNCTIONROBOT_H
#define FUNCTIONROBOT_H
#include "robotRace.h"

using namespace std;

class FunctionRobot : public robotRace{

public:
FunctionRobot();
int position(int);
void print();
protected:


};

#endif

FunctionRobot.cpp

#include <iostream>
#include "FunctionRobot.h"
#include <cmath>

using namespace std;

FunctionRobot :: FunctionRobot (): robotRace() {

int initPos =0;
race[initPos][0] = '*';
cout <<"Initial position of Function Robot is at begin of race."<<endl;

}

int FunctionRobot :: position(int place=0){

// log with a base 2 needs to be divided by the "x"
// below is the Robots formula to determine each of their movements
double x = ( 2 * (log(place)/log(2)));
return (int) x;

}

void FunctionRobot :: print(){

for (int i;i=0; i<100; i++)
for (int j;j=0; j<1; j++)
race[position()][j];

}

这是我要求的主文件。这是基本格式。我希望让 while 循环更实用,这样用户就不必一直输入 1。我的代码也没有错误。它运行时什么也没显示。

main.cpp

#include <iostream>
#include "robotRace.h"
#include "FunctionRobot.h"
using namespace std;

int main() {

int userInput;

cout << "Welcome to the Robot Race of the year!" << endl;
cout << "For our contestants we have the amazing three!" << endl;
cout << "The contestants are Robots F, R and U" << endl;
cout << "Let the games begin! \n\n";

cout << "Enter 1 to begin. " << endl;
cin >> userInput;

FunctionRobot functionObj;
//functionObj.position();
//functionObj.print();
cout << "Ready... Set... Go!!" << endl;

while (userInput == 1) {

functionObj.position(4);
functionObj.print();

} //end while

return 0;
}

最佳答案

您的 print() 越界:

void FunctionRobot :: print(){

for (int i; i<100; i++)
for (int j; j<1; j++)
race[position()][j];

}

j 未初始化。您可以尝试 int j = 0 作为开始。与 i 类似。

此外,您知道该函数名为 PRINT 但不打印任何内容,实际上它除了调用 position() 之外什么也没做。


int FunctionRobot :: position(int place=0){

// log with a base 2 needs to be divided by the "x"
// below is the Robots formula to determine each of their movements
double x = ( 2 * (log(place)/log(2))); <-------- now x is a double
return (int) x; <-------- now x is an integer, are you sure about that?

}

精度损失发生在这里。假设 x 被赋予了 3.14 的值。然后你将它转换为一个整数(转换会自动发生,因为函数的返回类型也是一个 int),因此它将被转换为 3,因此你失去了精度。


关于main.cpp

您调用用户输入 1 然后您有:

  while (userInput == 1) {

functionObj.position(4);
functionObj.print();

} //end while

但是 userInput 不会被修改,因此您将进入一个无休止的循环。

关于c++ - 不更新数组的继承机器人竞赛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26947799/

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