编译器使用参数C ++触发默认构造函数而不是构造函数

时间:2015-03-02 17:18:41

标签: c++ constructor

因此,以下代码应该使用targetInt数组为randomTarget对象设定种子,而是触发默认的targetInt构造函数,这将导致值为0的对象数组而不是我想要的随机整数。

驱动程序.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;
}

1 个答案:

答案 0 :(得分:0)

检查你的原型:

void initalize(randomTarget a);

你说a应该是一个randomTarget对象,它被创建为传入内容的副本

您希望a成为传入内容的引用;即其类型应为randomTarget&

这就像没有涉及任何功能一样:如果,在主要的,你有

randomTarget a = random;

然后修改a不会改变random。但是,如果你有

randomTarget &a = random;

然后修改a将更改random

相关问题