2014-11-21 36 views
-1

我想調試我的程序,看看它是否會運行正常,我不斷收到所有構造函數繼承超類的構造函數的錯誤。我的子類的所有.h文件給出相同的錯誤。你能解釋並幫助我嗎?錯誤:類的原型不匹配任何類 - 繼承

ERROR: main.cpp中:41:錯誤:調用沒有匹配的函數 'RandomRobot :: RandomRobot(INT,INT &)'

RandomRobot.h:10:注意:候選是:RandomRobot: :RandomRobot() RandomRobot.h:7:注:RandomRobot :: RandomRobot(常量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 

的問題或錯誤顯示了main.cpp中,你看到[1]等我不能機器人」不要讓.obj版本正常工作,所以我試圖將它們稱爲矢量。 ()中的信息無法連接到RandomRobot.cpp文件中的位置函數。我想這是我的問題,我不知道如何讓他們工作。

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

您是否定義了構造函數robotRace()? – Venkatesh 2014-11-21 18:48:38

+1

我們必須猜測錯誤是什麼嗎?這是一款遊戲嗎? – 2014-11-21 18:52:12

+0

我在我的標題中添加了錯誤,但被要求將其刪除...我現在添加它,並且我添加了robotRace的初始化 – narue1992 2014-11-21 18:53:07

回答

0
robots[2] = new RandomRobot (2, ranNum); 

我不知道這是什麼是應該做的,但是,因爲你的RandomRobot構造函數沒有參數,它不能做到這一點!

+0

雅我現在看到了。我將回到我的原始代碼RandomRobot randObj。格式,以便我可以調用(2,ranNum)的權利。回到繪圖板 – narue1992 2014-11-21 19:27:14

+0

@AlyssaCooke:祝你好運! – 2014-11-21 20:08:43