Jump to content
СофтФорум - всё о компьютерах и не только

конструктор в с++


Recommended Posts

Объясните, пож, почему если использовать конструктор с инициализироваными значения 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;}
Link to comment
Share on other sites

Потому что строка

Complex result;

может восприниматься как вызов конструктора без параметров, а может как вызов конструктора с парметрами по умолчанию.

Выход: убрать из конструктора с параметрами значения по умолчанию. В них смысла нет. Если параметры не будут указаны, то стандартный конструктор и так приравняет части к 0.

Удачи.

Link to comment
Share on other sites

Потому что строка

Complex result;

может восприниматься как вызов конструктора без параметров, а может как вызов конструктора с парметрами по умолчанию.

Выход: убрать из конструктора с параметрами значения по умолчанию. В них смысла нет. Если параметры не будут указаны, то стандартный конструктор и так приравняет части к 0.

Удачи.

Возможно, а возможно и нет. Зависить из компилятора. Стандарт C++ не указиваеть как инициализировать переменние, которие програмист не инициализировал. Возможно там будеть произволний байт (то, что било в памяти до етого). Так что всегда указивайте стойности. А иначе в полне прав - смисля из параметров по умалчанию нет.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...