So the following code is supposed to seed the randomTarget object with an array of targetInt but instead is firing the default targetInt constructor which results in an array of objects with value 0 instead of the random integers I want.
driver .cpp file
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;
}