我正在尝试调试我的程序并查看它是否运行正常并且我继续在继承超类的构造函数的所有构造函数上出错。我的子类的所有.h文件都给出了相同的错误。你能解释一下并帮助我吗?
错误: main.cpp:41:错误:没有匹配函数来调用'RandomRobot :: RandomRobot(int,int&)'
RandomRobot.h:10:注意:候选人是:RandomRobot :: RandomRobot() RandomRobot.h:7:注意:RandomRobot :: RandomRobot(const RandomRobot&)
我的一个机器人:
RandomRobot.h“
#ifndef RANDOMROBOT_H
#define RANDOMROBOT_H
#include "robotRace.h"
using namespace std;
class RandomRobot : public robotRace {
public:
RandomRobot();
int Rposition(int, int);
void print();
int getRan();
protected:
};
#endif
RandomRobot.cpp
#include <iostream>
#include "RandomRobot.h"
using namespace std;
RandomRobot :: RandomRobot() : robotRace(){
//initial position of robot will be placed in row 3. position 0 is 'X' track variable
// position 1 is for a different robot so next position which is 2 is where RandomRobot is
track[2][0] = 'R';
}
// 20% : forward 1
// 30% : forward 2
//35% : don't move
// 15% : forward 6
int RandomRobot :: Rposition(int rows, int columns){
int place =0;
srand(time(NULL));
int val= rand() % 100;
if (val > 15 || val < 20)// 20%
place = 1;
else if (val > 20 || val <30) //30%
place = 2;
else if (val> 30 || val <35)// 35%
place = 0;
else if (val < 15)// 15%
place = 6;
int movement =+ place;
cout <<"R Robot has moved: " << movement << " spaces."<<endl;
track[rows][columns+movement] = 'R';
return movement;
}
下面是我的超类h文件。它最初在()中没有变量。
#ifndef ROBOTRACE_H
#define ROBOTRACE_H
using namespace std;
class robotRace {
public:
robotRace (); //constructor
static const int rows = 5;
static const int columns = 5;
void printRace();
protected:
char track[rows][columns];
char 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++)
{
if (i == 0) //top boundary
{
track[i][j] = 'X';
}
else if (i == 4 && j < 4) //bottom boundary
{
track[i][j] = 'X';
}
else if (i == 4 && j < 5) //prints finish line
{
track[i][j] = 'F';
}
else if ((i >= 0 && i <= 4) && j == 0) //left boundary
{
track[i][j] = 'X';
}
else
{
track[i][j] = ' ';
}
}
}
race[0][0] = ' ';
}//end constructor
在你看到机器人[1]等的main.cpp上显示的问题或错误。我无法让.obj版本正常工作,所以我试图将它们称为向量。 ()中的信息意味着不能连接到RandomRobot.cpp文件中的postion函数。我想这是我的问题,我不知道如何让它们继续工作。
vector < robotRace* > robots(3);
robotRace raceObj;
int seconds = 0;
static int ranNum = 0;
static int funNum = 0;
static int unpreNum = 0;
int rpos = 0;
int mpos = 0;
int upos = 0;
for (int i =0; i < rows; i++)
{
for (int j=0; j < columns; j++){
int k =0;
while (k < columns)
{
robots[1] = new FunctionRobot (1, funNum);
robots[2] = new RandomRobot (2, ranNum);
robots[3] = new UnpredictedRobot (3, unpreNum);
// randomObj.Rposition(1, ranNum);
// functionObj.Mposition(0, funNum);
// unpredictObj.Uposition(2, unpreNum);
//Increment movements
答案 0 :(得分:0)
robots[2] = new RandomRobot (2, ranNum);
我不知道应该做什么但是,因为你的RandomRobot
构造函数没有参数,所以它无法做到!