Klasa Complex - problem

Klasa Complex - problem
C2
  • Rejestracja:ponad 12 lat
  • Ostatnio:ponad 12 lat
  • Postów:7
0

plik: Complex.h

Kopiuj
 #pragma once 


class Complex {
      private:
      double x;
      double y;
      public:
      Complex();
     Complex(double a, double b);  
     ~Complex();
     Complex(Complex &v);
      Complex operator!();
      bool operator ==(const Complex &v);
      bool operator!=(const Complex &v);
	 bool operator>(const Complex &v);
	 bool operator<(const Complex &v);
	 bool operator>=(const Complex &v);
	 bool operator<=(const Complex &v);
	 Complex & operator=(const Complex &v);
      Complex operator+(const Complex &v);
     Complex operator-(const Complex &v);
     Complex operator+=(const Complex &v);
     Complex operator-=(const Complex &v);
     	Complex operator*(const Complex &v);
	Complex operator/(const Complex &v);
		Complex operator*=(const Complex &);
	Complex operator/=(const Complex &v);
	
};

Plik:Complex.cpp

Kopiuj
 #include "Complex.h"
#include <iostream>

 
 
 
 Complex::Complex() {
           this->x = 0;
           this->y = 0; 
           }
           
Complex::Complex(double a, double b)
{
	this->x = a;
	this->y = b;
}

Complex::Complex(Complex &v)
{
	this->x = v.x;
	this->y = v.y;
}

Complex::~Complex()
{
}

Complex & Complex::operator=(const Complex &v)
{
    if(&v==this) 
    return *this;    
        
	this->x = v.x;
	this->y = v.y;
	return *this;
}

Complex Complex::operator!()
{

	return Complex( -this->x, -this->y);
}

bool Complex::operator==(const Complex &v)
{
	if(this->x==v.x && this->y==v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Complex::operator!=(const Complex &v)
{
	if(this->x!=v.x && this->y!=v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Complex::operator>(const Complex &v)
{
	if(this->x > v.x&& this->y > v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Complex::operator<(const Complex &v)
{
	if(this->x < v.x && this->y < v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Complex::operator>=(const Complex &v)
{
	if(this->x>=v.x && this->y>=v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Complex::operator<=(const Complex &v)
{
	if(this->x<=v.x && this->y<=v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

Complex Complex::operator+(const Complex &v)
{
        
	return Complex(this->x + v.x, this->y + v.y);
}

Complex Complex::operator-(const Complex &v)
{
	return Complex(this->x - v.x, this->y - v.y);
}

Complex Complex::operator*(const Complex &v)
{
   Complex wynik;
    wynik.x = this->x * v.x;
    wynik.y = this->y * v.y;
    return wynik;

}

Complex Complex::operator/(const Complex &v)
{
   Complex ret;
    ret.x = this->x * v.x;
    ret.y = this->y * v.y;
    return ret;

}

Complex Complex::operator+=(const Complex &v)
{
	this->x += v.x;
	this->y += v.y;
	return *this;
}

Complex Complex::operator-=(const Complex &v)
{
	this->x -= v.x;
	this->y -= v.y;
	return *this;
}

Complex Complex::operator*=(const Complex &v)
{
	this->x *= v.x;
	this->y *= v.y;
	return *this;
}

Complex Complex::operator/=(const Complex &v)
{
	this->x /= v.x;
	this->y /= v.y;
	return *this;
}

Wywala mi takie oto błędy:
C:\Dev-Cpp\Complex.cpp In member function Complex Complex::operator!()': C:\Dev-Cpp\Complex.cpp no matching function for call to Complex::Complex(Complex)'
C:\Dev-Cpp\Complex.cpp In member function Complex Complex::operator+(const Complex&)': C:\Dev-Cpp\Complex.cpp no matching function for call to Complex::Complex(Complex)'
C:\Dev-Cpp\Complex.cpp In member function Complex Complex::operator-(const Complex&)': C:\Dev-Cpp\Complex.cpp no matching function for call to Complex::Complex(Complex)'

Nie mogę rozgryźć dlaczego..

AN
  • Rejestracja:ponad 12 lat
  • Ostatnio:ponad 6 lat
  • Postów:62
1

Nie jestem pewny ale chyba potrzebujesz konstruktora kopiującego

Kopiuj
Complex( const Complex &v );

Zadeklaruj, zdefiniuj i sprawdź czy pomoże.

Azarien
  • Rejestracja:ponad 21 lat
  • Ostatnio:około 4 godziny
1
Kopiuj
Complex::Complex(Complex &v)

To nie jest konstruktor kopiujący. Żeby był kopiujący, musi być albo

Kopiuj
Complex::Complex(const Complex &v)

albo

Kopiuj
Complex::Complex(Complex v)

w tym przypadku skłaniałbym się ku drugiej opcji, podobnie jak w reszcie operatorów.

Sarrus
W drugiej opcji jest przekazanie przez wartość, które spowoduje wykonanie płytkiej kopii obiektu już podczas przekazywania na parametrze. Czy ja coś źle pamiętam czy coś się zmieniło?
Endrju
Nie no, to drugie to nie jest konstruktor kopiujący, bo żeby go wykonać potrzebny jest konstruktor kopiujący. To w operatorze przypisania nie musi być referencji. (Za to const nie musi być nigdzie)
_13th_Dragon
  • Rejestracja:ponad 19 lat
  • Ostatnio:3 miesiące
0
cypek21 napisał(a):
Kopiuj
Complex Complex::operator/(const Complex &v)
{
   Complex ret;
    ret.x = this->x * v.x;
    ret.y = this->y * v.y;
    return ret;
}

O żeś, nie wiedziałem! Od kiedy to?!

cypek21 napisał(a):
Kopiuj
Complex Complex::operator/=(const Complex &v)
{
	this->x /= v.x;
	this->y /= v.y;
	return *this;
}

Wow, tego też nie wiedziałem! i czemu:

Kopiuj
Complex a(6,0),b(2,0),c(a/b);
if((a/=b)!=c) cout<<"WTF?"<<endl;

Czemu nie używasz tego co zrobiłeś wcześniej, np:

Kopiuj
Complex Complex::operator+(const Complex &v)const { return Complex(x+v.x,y+v.y); } // używamy konstruktora
Complex &Complex::operator+=(const Complex &v) { return *this=operator+(v); } // używamy operatora +

Wykonuję programy na zamówienie, pisać na Priv.
Asm/C/C++/Pascal/Delphi/Java/C#/PHP/JS oraz inne języki.
robcio
  • Rejestracja:prawie 13 lat
  • Ostatnio:ponad 10 lat
  • Lokalizacja:Opole
  • Postów:533
0

po co cały czas używasz wskaźnika this w metodach. Jest on tam przez domniemanie nie musisz go używać no chyba ,że np nazwa argumentu funkcji pokrywa się z nazwą pola klasy i chcesz się do niego odnieść wtedy musisz napisać this->pole , ponieważ jak byś tego nie napisał to kompilator uznałby ,że chcesz się odnieść do parametru funkcji.


Nie odpowiadam na PW z prośbą o pomoc programistyczną.
edytowany 2x, ostatnio: robcio
MarekR22
czepiasz się, to jest tylko konwencja kodowania. jedni wstawiają this, inni stosują notację węgierską, a inni nie kombinują. Zwykła kwestia gustu i przyjętych standardów kodowania.
C2
  • Rejestracja:ponad 12 lat
  • Ostatnio:ponad 12 lat
  • Postów:7
0

Pomogło wstawienie do konstruktora kopiującego "const", cała składania ma teraz postać: Complex::Complex(const Complex &v). Dzięki za pomoc ;)

C2
  • Rejestracja:ponad 12 lat
  • Ostatnio:ponad 12 lat
  • Postów:7
0
robcio napisał(a):

po co cały czas używasz wskaźnika this w metodach. Jest on tam przez domniemanie nie musisz go używać no chyba ,że np nazwa argumentu funkcji pokrywa się z nazwą pola klasy i chcesz się do niego odnieść wtedy musisz napisać this->pole , ponieważ jak byś tego nie napisał to kompilator uznałby ,że chcesz się odnieść do parametru funkcji.

Wiem, że jest on przed domniemanie i ciężko było mi sie przestawić, ale mój prowadzący od obiektówki wymaga użycia this, żeby wskazać, że to właśnie o ten składnik nam chodzi.

_13th_Dragon
Pewnie ten wykładowca to ofiara PHP'a
C2
  • Rejestracja:ponad 12 lat
  • Ostatnio:ponad 12 lat
  • Postów:7
0

Przeciążyłem operatory We/Wy i znowu jakiś kwas a mianowicie w kodzie:

Kopiuj
 
#pragma once 


class Complex {
      public:
      double x;
      double y;
      public:
      Complex();
     Complex(double a, double b);  
     ~Complex();
     Complex(const Complex &v);
      Complex operator!();
      bool operator ==(const Complex &v);
      bool operator!=(const Complex &v);
	 bool operator>(const Complex &v);
	 bool operator<(const Complex &v);
	 bool operator>=(const Complex &v);
	 bool operator<=(const Complex &v);
	 Complex & operator=(const Complex &v);
      Complex operator+(const Complex &v);
     Complex operator-(const Complex &v);
     Complex operator+=(const Complex &v);
     Complex operator-=(const Complex &v);
     	Complex operator*(const Complex &v);
	Complex operator/(const Complex &v);
		Complex operator*=(const Complex &v);
	Complex operator/=(const Complex &v);
    std::ostream& operator<<(std::ostream &out,const Complex &v);
}

i w kodzie:

Kopiuj
 
#include "Complex.h"
#include <iostream>

 
 
 Complex::Complex() {
           this->x = 0;
           this->y = 0; 
           }
           
Complex::Complex(double a, double b)
{
	this->x = a;
	this->y = b;
}

Complex::Complex(const Complex &v)
{
	this->x = v.x;
	this->y = v.y;
}

Complex::~Complex()
{
}

Complex & Complex::operator=(const Complex &v)
{
    if(&v==this) 
    return *this;    
        
	this->x = v.x;
	this->y = v.y;
	return *this;
}

Complex Complex::operator!()
{

	return Complex( -this->x, -this->y);
}

bool Complex::operator==(const Complex &v)
{
	if(this->x==v.x && this->y==v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Complex::operator!=(const Complex &v)
{
	if(this->x!=v.x && this->y!=v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Complex::operator>(const Complex &v)
{
	if(this->x > v.x&& this->y > v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Complex::operator<(const Complex &v)
{
	if(this->x < v.x && this->y < v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Complex::operator>=(const Complex &v)
{
	if(this->x>=v.x && this->y>=v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

bool Complex::operator<=(const Complex &v)
{
	if(this->x<=v.x && this->y<=v.y)
	{
		return true;
	}
	else
	{
		return false;
	}
}

Complex Complex::operator+(const Complex &v)
{
        
	return Complex(this->x + v.x, this->y + v.y);
}

Complex Complex::operator-(const Complex &v)
{
	return Complex(this->x - v.x, this->y - v.y);
}

Complex Complex::operator*(const Complex &v)
{
   Complex wynik;
    wynik.x = this->x * v.x;
    wynik.y = this->y * v.y;
    return wynik;

}

Complex Complex::operator/(const Complex &v)
{
   Complex ret;
    ret.x = this->x / v.x;
    ret.y = this->y / v.y;
    return ret;

}

Complex Complex::operator+=(const Complex &v)
{
	this->x += v.x;
	this->y += v.y;
	return *this;
}

Complex Complex::operator-=(const Complex &v)
{
	this->x -= v.x;
	this->y -= v.y;
	return *this;
}

Complex Complex::operator*=(const Complex &v)
{
	this->x *= v.x;
	this->y *= v.y;
	return *this;
}

Complex Complex::operator/=(const Complex &v)
{
	this->x /= v.x;
	this->y /= v.y;
	return *this;
}

 std::ostream & operator<<(std::ostream &out, const Complex &v)
{
 return out << '(' << v.x << ',' << v.y << ')';


}

wywala takie błędy:

C:\Dev-Cpp\Complex.cpp In file included from Complex.cpp
C:\Dev-Cpp\Complex.h using-declaration for non-member at class scope
C:\Dev-Cpp\Complex.h expected ;' before '&' token C:\Dev-Cpp\include\c++\3.4.2\iostream:44, from Complex.cpp C:\Dev-Cpp\include\c++\3.4.2\mingw32\bits\c++config.h expected unqualified-id before "namespace" C:\Dev-Cpp\include\c++\3.4.2\mingw32\bits\c++config.h expected ,' or ;' before "namespace" C:\Dev-Cpp\include\c++\3.4.2\mingw32\bits\c++config.h expected namespace-name before ';' token C:\Dev-Cpp\include\c++\3.4.2\mingw32\bits\c++config.h <type error="error">' is not a namespace

czy ktoś pomoże?

Azarien
  • Rejestracja:ponad 21 lat
  • Ostatnio:około 4 godziny
0
Kopiuj
c:\pp\MYPROGS\C>clang++ -c Complex.cpp -Wall -Wextra
In file included from Complex.cpp:1:
./Complex.h:29:5: error: use of undeclared identifier 'std'
    std::ostream& operator<<(std::ostream &out,const Complex &v);
    ^
./Complex.h:29:30: error: use of undeclared identifier 'std'
    std::ostream& operator<<(std::ostream &out,const Complex &v);
                             ^
./Complex.h:30:1: error: expected ';' after class
}
 ^
 ;
3 errors generated.

po poprawieniu średnika i include'a, jest:

Kopiuj
c:\pp\MYPROGS\C>clang++ -c Complex.cpp -Wall -Wextra
In file included from Complex.cpp:1:
./Complex.h:30:19: error: overloaded 'operator<<' must be a binary operator (has 3 parameters)
    std::ostream& operator<<(std::ostream &out,const Complex &v);
                  ^
1 error generated.

a teraz to już sam zgadnij dlaczego :-)

edytowany 3x, ostatnio: Azarien
C2
  • Rejestracja:ponad 12 lat
  • Ostatnio:ponad 12 lat
  • Postów:7
0

może to trywialne ale nie wiem o co chodzi z tym błędem ;d

_13th_Dragon
  • Rejestracja:ponad 19 lat
  • Ostatnio:3 miesiące
0

powiedziałeś kompilatorowi że zrobisz metodę:
std::ostream& operator<<(std::ostream &out,const Complex &v);
czyli trójargumentowy operator << *this out v


Wykonuję programy na zamówienie, pisać na Priv.
Asm/C/C++/Pascal/Delphi/Java/C#/PHP/JS oraz inne języki.
edytowany 3x, ostatnio: _13th_Dragon
C2
  • Rejestracja:ponad 12 lat
  • Ostatnio:ponad 12 lat
  • Postów:7
0

czyli jak to ma w wersji finalnej wyglądać, te 3 parametry, sorry za klopot ale kompletnie tego nie widzę..

_13th_Dragon
Istnieje tylko jeden operator trójargumentowy ale z całą pewnością to nie operator << tylko ?:
Azarien
ale każda metoda przyjmuje niejawny pierwszy parametr, i jest nim this. w kodzie tego nie widać (dlatego niejawny) ale kompilator się o to właśnie czepia.
C2
  • Rejestracja:ponad 12 lat
  • Ostatnio:ponad 12 lat
  • Postów:7
0

Masz rację, pomożesz mi z prawidłowym zapisem tej funkcji ?

Azarien
  • Rejestracja:ponad 21 lat
  • Ostatnio:około 4 godziny
0

Wyciągnij operator<< poza klasę, jako globalną funkcję. Jeśli używasz w nim pól albo metod prywatnych, musisz dodać friend.

PS. w twoim przypadku nie widzę sensu, żeby pola były prywatne. Ale nawet jeśli, to wtedy powinieneś dodać gettery i settery, a tych nie widzę.

edytowany 2x, ostatnio: Azarien
deus
A po cholerę liczby zespolone mają być mutable?
Azarien
bez znaczenia; ale gettery by się przydały.
_13th_Dragon
Ten operator jest u niego funkcją globalną. Zaś to o czym mówisz jest tym friend'em tylko bez friend'a. ;P

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.