N
Nick Overdijk
With this code:
#include <iostream>
using namespace std;
class car {
public:
car (float speed) :
speed(speed) {}
car () :
speed(0) {}
void cruise(float speed) {
this->speed = speed;
cout << "New speed: " << getSpeed() << endl;
}
void brake(float power) {
this->speed -= power*power/4;
}
float getSpeed() {
return speed;
}
private:
float speed;
};
class racer : public car {
public:
void boost(float power) {
cout << "BOOST! ";
cruise(getSpeed() + power*power/3);
}
};
class tank : public car {
public:
bool shoot(float aimTime) {
cout << "Shot ";
if (aimTime > 5.0) {
cout << "hits!" << endl;
return true; //hit!
} else {
cout << "misses!" << endl;
return false; //miss!
}
}
};
class racetank : public racer, public tank {
public:
bool boostShoot(float power, float aimTime) {
boost(power*2);
return shoot(aimTime*2);
}
};
int main() {
racetank mycar;
mycar.car::cruise(50);
mycar.boost(20);
mycar.car::brake(5);
mycar.boostShoot(35, 1.4);
return 0;
}
MinGW's GCC gives me this error, twice:
error: `car' is an ambiguous base of `racetank'
How come the compiler still complains about ambiguity?
It knows the function is not overloaded (?)
It knows the function is not virtual.
Can't the compiler be sure that when I call the function like that, it's
the same function?
I know the solution to this is to use virtual inheritance, but I was
wondering why this happens.
Thanks in advance,
#include <iostream>
using namespace std;
class car {
public:
car (float speed) :
speed(speed) {}
car () :
speed(0) {}
void cruise(float speed) {
this->speed = speed;
cout << "New speed: " << getSpeed() << endl;
}
void brake(float power) {
this->speed -= power*power/4;
}
float getSpeed() {
return speed;
}
private:
float speed;
};
class racer : public car {
public:
void boost(float power) {
cout << "BOOST! ";
cruise(getSpeed() + power*power/3);
}
};
class tank : public car {
public:
bool shoot(float aimTime) {
cout << "Shot ";
if (aimTime > 5.0) {
cout << "hits!" << endl;
return true; //hit!
} else {
cout << "misses!" << endl;
return false; //miss!
}
}
};
class racetank : public racer, public tank {
public:
bool boostShoot(float power, float aimTime) {
boost(power*2);
return shoot(aimTime*2);
}
};
int main() {
racetank mycar;
mycar.car::cruise(50);
mycar.boost(20);
mycar.car::brake(5);
mycar.boostShoot(35, 1.4);
return 0;
}
MinGW's GCC gives me this error, twice:
error: `car' is an ambiguous base of `racetank'
How come the compiler still complains about ambiguity?
It knows the function is not overloaded (?)
It knows the function is not virtual.
Can't the compiler be sure that when I call the function like that, it's
the same function?
I know the solution to this is to use virtual inheritance, but I was
wondering why this happens.
Thanks in advance,