Следующий код:gcc компиляция ошибки, инициализация объекта интерпретируется как указатель на функцию?
class Angle {
protected:
double rad;
Angle(double r) : rad(r){}
};
class Radian : public Angle {
public:
Radian(double rad) : Angle(rad){}
};
class Latitude : public Angle {
public:
Latitude(const Angle&a) : Angle(a){}
Latitude operator-(const Latitude &other){
return Latitude(Radian(rad-other.rad));
}
};
int main(){
double d = 0.7;
Latitude lat1(Radian(0.8));
Latitude lat2(Radian(d*1));
Latitude lat3(Radian(d));
Latitude diff2 = lat1 - lat2;
Latitude diff3 = lat1 - lat3;
return 0;
}
дает ошибку компилятора:
$ g++ bug.cpp
bug.cpp: In function ‘int main()’:
bug.cpp:26:26: error: no match for ‘operator-’ (operand types are ‘Latitude’ and ‘Latitude(Radian)’)
Latitude diff3 = lat1 - lat3;
^
bug.cpp:26:26: note: candidate is:
bug.cpp:15:13: note: Latitude Latitude::operator-(const Latitude&)
Latitude operator-(const Latitude&l){
^
bug.cpp:15:13: note: no known conversion for argument 1 from ‘Latitude(Radian)’ to ‘const Latitude&’
Если я пытаюсь сделать lat3 - lat3
это даст эту ошибку, указывая, что он интерпретирует lat3
как функцию указателя:
error: ISO C++ forbids using pointer to a function in subtraction [-fpermissive]
lat3 - lat3;
^
lat2 - lat2
в порядке.
Так что же здесь происходит? Почему lat2 и lat3 интерпретируются как разные типы? Что именно означает Latitude(Radian)
в сообщении об ошибке?
Выход компилятора из gcc 4.9.1, аналогичные результаты получены с версиями gcc с 4.1 по 4.9. Платформа - 64-битная RHEL6.
Или использовать Latitude lat3 {Радиан (d)}; – senfen
Или измените линию на Latitude lat3 (Radian ((double) d)); –