Przeładowaniem operatora <<

0

Przeładowałem na potrzeby swojej klasy operator << tak aby możliwy był zapis cout<<obiekt_mojej_klasy. Podczas kompilacji pojawiają się błędy, nie wiem dlaczego...

#include <iostream>
using namespace std;

class punkt
{
friend ostream & operator<<(ostream &ekran, punkt &p);

private:
	int x,y;
	

public:
	punkt(int a = 1, int b = 1): x(a), y(b)
	{
	}

};

ostream & operator<<(ostream &ekran, punkt &p)
{
	ekran << "\tx = " << p.x << ", y = " << p.y<< endl;

	return ekran;
}

void main()
{
       punkt p1;
       cout<<p1;

       system("pause");
}

0
#include <iostream>
using namespace std;

class Punkt
{
private:
    int x,y;
public:
    Punkt(int a = 1, int b = 1): x(a), y(b) {}
    friend ostream& operator<<(ostream& ekran, Punkt& p);
};

ostream& operator<<(ostream& ekran, Punkt& p)
{
    ekran << "\tx = " << p.x << ", y = " << p.y;
    return ekran;
}

int main()
{
    Punkt p1;
    cout<<p1;

    cin.sync();
    cin.get();
    return 0;
}

Jedyne błędy jakie widzę w tym kodzie to fakt że masz void zamiast int, brak returna w main oraz brak #include potrzebnego dla system(). Jeśli nie napiszesz jaki widzisz bląd to nikt ci z ręki nie wywróży...
Kod który tutaj wrzucam działa.

0

W tym zapisie działa, moje niedopatrzenie... Ale jak mam ten program w takich dwóch plikach to jest problem... Wtedy pierwszy błąd to: punkt.h(5) : error C2143: syntax error : missing ';' before '&'

punkt.h

#include <iostream>

class punkt
{
friend ostream & operator<<(ostream &ekran, punkt &p);

private:
        int x,y;
       

public:
        punkt(int a = 1, int b = 1): x(a), y(b)
        {
        }

};

main.cpp

#include <iostream>
#include "punkt.h"
using namespace std;


ostream & operator<<(ostream &ekran, punkt &p)
{
        ekran << "\tx = " << p.x << ", y = " << p.y<< endl;

        return ekran;
}

void main()
{
       punkt p1;
       cout<<p1;

       system("pause");
}
0

Jeśli w pliku *.h nie masz using namespace std to musisz pisać
std::ostream

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