2015-03-02 68 views
-3

因此,下面的代碼應該將randomTarget對象與一個targetInt數組一起播種,但是會引發默認的targetInt構造函數,從而產生一個值爲0而不是隨機整數我想要的。編譯器啓動默認構造函數而不是帶參數的構造函數C++

驅動程序.cpp文件

using namespace std; 


int highGuesses; 
int lowGuesses; 
int sumGuesses; 
int numGuesses; 
int avgGuess; 
const int size = 25; 

void initalize(randomTarget a) 
{ 
     int r; 
     int seed[size]; 
     for (int j = 0; j < size; j++) 
     { 
      r = rand() % 100 + 1; 
      seed[j] = r; 
     } 
     a = randomTarget(seed); 
} 




int main(int argc, const char * argv[]) { 
    srand(time(NULL)); 
    randomTarget random; 
    randomTarget random2; 
    initalize(random); 
    initalize(random2); 

    randomTarget r3; 

    r3 = random + random2; 

    r3.printRandom(); 


} 

targetInt.h

using namespace std; 

class targetInt{ 

    int target; 
    bool known; 
    bool active; 
    int count; 
    //void copy(const targetInt& src); 

public: 
    //targetInt& operator=(const targetInt& src); 
    targetInt& operator+(const targetInt& src); 
    bool operator!=(const targetInt& src); 
    bool operator<(const targetInt & src); 
    bool operator>(const targetInt& src); 
    bool operator==(const targetInt& src); 
    bool operator<=(const targetInt& src); 
    bool operator>=(const targetInt& src); 
    targetInt& operator+=(targetInt& src); 
    targetInt& operator-=(targetInt& src); 
    targetInt& operator-(const targetInt& src); 

    int getTarget(){return target;}; 
    void turnOff(){active = false;} 
    targetInt(){known = false; active = true; target = 0; count = 0; } 
    targetInt(int value){known = false; active = true; target = value; count= 0;} 
    int query(int guess); 
    bool isActive(){return active == true;} 
    //targetInt(const targetInt& src) {copy(src);}; 


}; 

randomTargets.cpp

using namespace std; 


randomTarget randomTarget::operator+(const randomTarget& src) 
{ 

    randomTarget temp; 
    for (int i = 0; i<size; i++) 
    { 

     temp.targets[i] = this->targets[i] + src.targets[i]; 
    } 

    return temp; 
} 

randomTarget& randomTarget::operator+=(const randomTarget& src) 
{ 
    for(int i = 0; i< size ; i++) 
    { 
     this->targets[i] = this->targets[i] + src.targets[i]; 
    } 
    return *this; 
} 

randomTarget& randomTarget::operator-=(const randomTarget& src) 
{ 
    for(int i = 0; i< size ; i++) 
    { 
     this->targets[i] = this->targets[i] - src.targets[i]; 
    } 
    return *this; 
} 


bool randomTarget::operator==(const randomTarget& src) 
{ 
    return this == &src; 
} 

bool randomTarget::operator!=(const randomTarget& src) 
{ 
    return this != &src; 
} 



randomTarget randomTarget::operator-(const randomTarget& src) 
{ 

    randomTarget temp; 
    for (int i = 0; i<size; i++) 
    { 

     temp.targets[i] = this->targets[i] - src.targets[i]; 
    } 

    return temp; 
} 


randomTarget::randomTarget() 
{ 


    for (int i = 0; i < size; i++) 
    { 
     targets[i] = targetInt(0); 
    } 
    count = 0; 
} 

randomTarget::randomTarget(int value[]) 
{ 

    for(int i = 0; i < size; i++) 
    { 
     targets[i] = targetInt(value[i]); 
    } 
    count = 0; 
} 

void randomTarget::printRandom() 
{ 
    for(int i = 0; i< size; i++) 
    { 
     cout << targets[i].getTarget() << " "; 
    } 
    cout << endl; 
} 



bool randomTarget::identify(int guess) 
{ 
    defunct = false; 
    int count = size; 
    bool found = false; 
    int id; 
    for (int i = 0; i < size; i++) 
    { 
     id = targets[i].query(guess); 
     if (id == 0) 
     { 
      found = true; 
      targets[i].turnOff(); 
      count--; 
      if (2 * count < size) defunct = true; 
      cout << "Correct!" << endl; 
     } 
     else if(id < 1) 
     { 
      cout << "Too Low!" << endl; 
     } 
     cout << "Too High!" << endl; 
    } 

    return found; 
} 

targetInt.cpp

int targetInt::query(int guess){ 
    known = false; 

    if (guess < target) 
    { 
     count++; 
     return -1; 
    } 

    else if (guess > target) 
    { 
     count++; 
     return 1; 
    } 

    known = true; 
    active = false; 
    count++; 
    return 0; 
} 

targetInt& targetInt::operator+(const targetInt& src) 
{ 
    int newTarget = src.target + target; 
    targetInt i(newTarget); 
    return i; 
} 


targetInt& targetInt::operator-(const targetInt& src) 
{ 
    int newTarget = src.target - target; 
    targetInt i(newTarget); 
    return i; 
} 

targetInt& targetInt::operator-=(targetInt& src) 
{ 
    target = target - src.target; 
    return *this; 
} 

targetInt& targetInt::operator+=(targetInt& src) 
{ 
    target = target + src.target; 
    return *this; 
} 


bool targetInt::operator==(const targetInt& src) 
{ 
    return target == src.target; 
} 

bool targetInt::operator!=(const targetInt& src) 
{ 
    return target != src.target; 
} 

bool targetInt::operator<(const targetInt& src) 
{ 
    return target < src.target; 
} 

bool targetInt::operator>(const targetInt& src) 
{ 
    return target > src.target; 
} 

bool targetInt::operator<=(const targetInt& src) 
{ 
    return target <= src.target; 
} 

bool targetInt::operator>=(const targetInt& src) 
{ 
    return target >= src.target; 
} 
+0

其中是randomTarget類? – sithereal 2015-03-02 17:20:01

+2

你的'initialize'函數只是局部變量,所以不起作用。 – juanchopanza 2015-03-02 17:23:14

+0

附註:請不要定義['using namespace std;'](http://stackoverflow.com/q/1452721/332733)它通常是一個糟糕的主意。 – Mgetz 2015-03-02 17:24:09

回答

0

檢查原型:

void initalize(randomTarget a); 

你說a應該是創建的,是一個複製的什麼得到傳入一個randomTarget對象

你想a成爲參考。通過什麼;即其類型應該是randomTarget&

這個作品一樣,如果有不涉及任何功能:如果在主,你有

randomTarget a = random; 

然後修改a不會改變random。但是,如果你有

randomTarget &a = random; 

然後修改a將改變random

+0

他們真正想要的是一個函數,它返回一個合適的初始化的'randomTarget'。 – juanchopanza 2015-03-02 17:25:59