Klasa Complex - problem

0

plik: Complex.h

 #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

 #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..

1

Nie jestem pewny ale chyba potrzebujesz konstruktora kopiującego

Complex( const Complex &v );

Zadeklaruj, zdefiniuj i sprawdź czy pomoże.

1
Complex::Complex(Complex &v)

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

Complex::Complex(const Complex &v)

albo

Complex::Complex(Complex v)

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

0
cypek21 napisał(a):
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):
Complex Complex::operator/=(const Complex &v)
{
	this->x /= v.x;
	this->y /= v.y;
	return *this;
}

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

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:

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 +
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.

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 ;)

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.

0

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

 
#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:

 
#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?

0
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:

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 :-)

0

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

0

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

0

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

0

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

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ę.

1 użytkowników online, w tym zalogowanych: 0, gości: 1