Problem with overloading operators == and <<

Welcome

I'm quite a beginner in C++, and I have problem with overloading operator == and <<

The only parts from my code that aren't working are overloaded operators:

part from header files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Adres.h"
#include "DataUrodzenia.h"
#include <string>
#include <iostream>

class DaneOsobowe : public DataUrodzenia, public Adres
{
      private:
      std::string Imie;
      std::string Nazwisko;
      Adres *Adresik;
      DataUrodzenia *Urodziny;
      public:
friend std::ostream & std::operator<<(std::ostream& wyjscie, DaneOsobowe const&);
friend int operator== (const DaneOsobowe &Dane1 , const DaneOsobowe &Dane2)


extract from cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
std::ostream & std::operator<<(std::ostream& wyjscie, const DaneOsobowe& Dane)
		{
		wyjscie << "\nOsoba nazywa sie" << Dane.Imie << " " << Dane.Nazwisko << "\nurodzila sie " << Dane.PokazDate() << "\nmieszka " << Dane.PodajAdres() << std::endl;
		return wyjscie;
		};

		friend int operator== (const DaneOsobowe &Dane1 , const DaneOsobowe &Dane2)
		{
		 {
                 if((Dane1.Rok == Dane2.Rok))
                 std::cout << "\nKtos tu urodzil sie w tym samym roku!";
            else
                 std::cout << "\nKtos tu jest starszy rocznikowo";
        };

        {
		    if(Dane1.Miesiac == Dane2.Miesiac)
                { std::cout << "\nKtos tu urodzil sie w tym samym miesiacu!";}
        else
                { std::cout << "\nKtos tu jest starszy miesiecznikowo";}
                }
        ;
		{
		    if(Dane1.Dzien == Dane2.Dzien)
                { std::cout << "\nKtos tu urodzil sie w tym samym dniu!";}
        else
                { std::cout << "\nJest malo prawdopodobne zeby dwie osoby urodzily sie w tym samym dniu";}
        };
		{
		    if(Dane1.Rok == Dane2.Rok && Dane1.Miesiac == Dane2.Miesiac && Dane1.Dzien == Dane2.Dzien)
		    { std::cout << "\nCzyzby klon? Urodzil sie w tym samym dniu, miesiacu i roku co druga osoba";}
		else
            { std::cout << "\n";};
		};
        };


Few words of explanation:
Sorry for "strange" names for any sort of variables, but I'm using my native language:
DaneOsobowe, Adres and DataUrodzenia are classes - DaneOsobowe contains Adres (Adress info, like city, street) and DataUrodzenia(year, month and day of birth, all integer type) and its own parameters like name and surname

Overloaded operator << was supposed to print all information about object - I found to main problems:
first, after changing some parts compilator often shouts that it cannot find operator in its base like I wrote (if it happens to me one more time, I will show you this)
next, there are problems with using methods with printing classes' information (Dane.PodajAdres() and Dane.PokazDate() ) - but, strangely, when I use them outside overloaded operator it works as they were supposed to.

Overloaded operator == was supposed to compare two objects' day, month and year of birth. There are four sections - one compares days, another month, and so on. Operator works, there are no errors when compiling, but problem is that everytime I use it, it appears that it always compares the same values, therefore showing always the same messages, no matter how much I change variables. For example, if I use for first object day=1, month=2, year=3, no matter what values I use for second object, operator always shows, that 1=1, 2=2 etc.

Any ideas? And, if you don't understand what I meant, please feel free to comment :)
There is no return value in your definition of operator ==. At least I do not see where you return an integer from the operator function.
Are the functions PokazDate and PodajAdres marked with const? You can't call non-const member functions on const objects.

Your operator== doesn't have a return statement. Usually operator== returns a bool, without printing anything.

You should really care more about indentation. It makes it easier to read the code.
Last edited on
Don't use std::operator in your declaration, it's just operator.
Also, any == should return a bool, and you don't need to declare it as a friend function.
Finally, your brackets are all over the place. Only use them when you really need to.
Topic archived. No new replies allowed.