little_greg Posted March 9, 2010 Report Share Posted March 9, 2010 Объясните, пож, почему если использовать конструктор с инициализироваными значения Complex(double r = 0.0, double i = 0.0); и при этом использовать конструктор по умолчанию Complex(), то появляется ошибка о неоднозначности (error: call of overloaded ‘Complex()’ is ambiguous; note: candidates are: Complex::Complex(double, double); note: Complex::Complex()); #include <iostream>class Complex{private: double real; double image;public: Complex(); Complex(double r = 0.0, double i = 0.0); ~Complex(); Complex operator+(const Complex& cn); Complex operator-(const Complex& cn); Complex operator~(); Complex operator*(const Complex& cn); friend Complex operator*(double factor, Complex& cn); friend std::ostream& operator<<(std::ostream& os, const Complex& cn); friend std::istream& operator>>(std::istream& is, Complex& cn);};Complex::Complex() //эта строка подчеркивается как ошибочная{real = 0.0;image = 0.0;}Complex::Complex(double r, double i) //эта строка подчеркивается как ошибочная{real = r;image = i;}Complex::~Complex(){}Complex Complex::operator+(const Complex& cn){Complex result; //эта строка подчеркивается как ошибочнаяresult.real = real + cn.real;result.image = image + cn.image;return result;}Complex Complex::operator-(const Complex& cn){Complex result; //эта строка подчеркивается как ошибочнаяresult.real = real - cn.real;result.image = image - cn.image;return result;}Complex Complex::operator~(){Complex result; //эта строка подчеркивается как ошибочнаяresult.image = -image;return result;}Complex Complex::operator*(const Complex& cn){Complex result; //эта строка подчеркивается как ошибочнаяresult.real = real * cn.real - image * cn.image;result.image = real * cn.image + image * cn.real;return result;}Complex operator*(double factor, Complex& cn){Complex result; //эта строка подчеркивается как ошибочнаяresult.real = factor * cn.real;result.image = factor * cn.image;return result;}std::ostream& operator<<(std::ostream& os, const Complex& cn){os << "(" << cn.real << ", " << cn.image << "i)";return os;}std::istream& operator>>(std::istream& is, Complex& cn){is >> cn.real >> cn.image; return is;}using namespace std;int main(){Complex a(3.0, 4.0);Complex c; //эта строка подчеркивается как ошибочнаяcout << "Enter complex number:\n";while (cin >> c){ cout << "c is " << c << endl; cout << "complex merger " << ~c << endl; cout << "a = " << a << endl; cout << "a + c = " << a + c << endl; cout << "a - c = " << a - c << endl; cout << "a * c = " << a * c << endl; cout << "2 * c = " << 2 * c << endl; cout << "Enter complex number:\n";}return 0;} Quote Link to comment Share on other sites More sharing options...
OGR Posted March 9, 2010 Report Share Posted March 9, 2010 Потому что строка Complex result; может восприниматься как вызов конструктора без параметров, а может как вызов конструктора с парметрами по умолчанию. Выход: убрать из конструктора с параметрами значения по умолчанию. В них смысла нет. Если параметры не будут указаны, то стандартный конструктор и так приравняет части к 0. Удачи. Quote Link to comment Share on other sites More sharing options...
Darhazer Posted March 11, 2010 Report Share Posted March 11, 2010 Потому что строка Complex result; может восприниматься как вызов конструктора без параметров, а может как вызов конструктора с парметрами по умолчанию. Выход: убрать из конструктора с параметрами значения по умолчанию. В них смысла нет. Если параметры не будут указаны, то стандартный конструктор и так приравняет части к 0. Удачи. Возможно, а возможно и нет. Зависить из компилятора. Стандарт C++ не указиваеть как инициализировать переменние, которие програмист не инициализировал. Возможно там будеть произволний байт (то, что било в памяти до етого). Так что всегда указивайте стойности. А иначе в полне прав - смисля из параметров по умалчанию нет. Quote Link to comment Share on other sites More sharing options...
Lion HC Posted March 11, 2010 Report Share Posted March 11, 2010 public: Complex(); explicit Complex(double r, double i = 0.0); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.