Ask about multi file in C++

Please help me !
I have 1 class and it separates 2 file: .h and .cpp
=====Vector3D.h=========
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#ifndef VECTOR3D_H_
#define VECTOR3D_H_

class Vector3D {
protected:
	int x;
	int y;
	int z;
public:
	Vector3D();
	Vector3D(int, int, int);
	Vector3D operator+(const Vector3D&);
	virtual ~Vector3D();
	friend ostream& operator<<(ostream&, const Vector3D&);
};

#endif 

========Vector3D.cpp=============
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
#include "Vector3D.h"

Vector3D::Vector3D() {
	this->x = 0;
	this->y = 0;
	this->z = 0;
}

Vector3D::Vector3D(int x, int y, int z) {
	this->x = x;
	this->y = y;
	this->z = z;
}

Vector3D::~Vector3D() {
	//Remove all
}

Vector3D::operator+(const Vector3D& v){
	Vector3D c = new Vector3D();
	c.x = x + v.x;
	c.y = y + v.y;
	c.z = z + v.z;
	return c;
}

ostream& operator<<(ostream& os, const Vector3D& v){
	os<<"("<<v.x<<", "<<v.y<<", "<<v.z<<")";
	return os;
}

=======Main.cpp============
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include "Vector3D.h"

using namespace std;

int main(int argc, char **argv) {
	Vector3D *vec = new Vector3D(2, 4, 6);
	cout<<*vec;
	return 0;
}

Everything's good. But For declare friend fucntion, has error. How to declare friend function in prototype ? Please help me ?
it's std::ostream
prototype:
friend std::ostream& operator<<(std::ostream&, const Vector3D&);
declare
1
2
3
4
std::ostream& operator<<(std::ostream& os, const Vector3D& v){
	os<<"("<<v.x<<", "<<v.y<<", "<<v.z<<")";
	return os;
}

But still error !!
Maybe you should post the error...
I used Eclipse, here're my errors
--------------


**** Build of configuration Debug for project FirstCplus ****

**** Internal Builder is used for build ****
g++ -IC:/MinGW/bin -O0 -g3 -Wall -c -fmessage-length=0 -ocpp\Vector3D.o ..\cpp\Vector3D.cpp
In file included from ..\cpp\Vector3D.cpp:8:
..\cpp\Vector3D.h:20: error: ISO C++ forbids declaration of 'ostream' with no type
..\cpp\Vector3D.h:20: error: 'ostream' is neither function nor member function; cannot be declared friend
..\cpp\Vector3D.h:20: error: expected ';' before '&' token
..\cpp\Vector3D.cpp:29: error: ISO C++ forbids declaration of 'operator+' with no type
..\cpp\Vector3D.cpp:29: error: prototype for 'int Vector3D::operator+(const Vector3D&)' does not match any in class 'Vector3D'
..\cpp\Vector3D.h:18: error: candidate is: Vector3D Vector3D::operator+(const Vector3D&)
..\cpp\Vector3D.h: In function 'std::ostream& operator<<(std::ostream&, const Vector3D&)':
..\cpp\Vector3D.h:12: error: 'int Vector3D::x' is protected
..\cpp\Vector3D.cpp:38: error: within this context
..\cpp\Vector3D.h:13: error: 'int Vector3D::y' is protected
..\cpp\Vector3D.cpp:38: error: within this context
..\cpp\Vector3D.h:14: error: 'int Vector3D::z' is protected
..\cpp\Vector3D.cpp:38: error: within this context
Build error occurred, build is stopped
Time consumed: 359 ms.
And I want to say more, error cause by friend function declaration. Because, when i delete this friend function, everything's good !!
**** Build of configuration Debug for project FirstCplus ****

**** Internal Builder is used for build ****
g++ -IC:/MinGW/bin -O0 -g3 -Wall -c -fmessage-length=0 -ocpp\Vector3D.o ..\cpp\Vector3D.cpp
In file included from ..\cpp\Vector3D.cpp:8:
..\cpp\Vector3D.h:20: error: ISO C++ forbids declaration of 'ostream' with no type
..\cpp\Vector3D.h:20: error: 'ostream' is neither function nor member function; cannot be declared friend
..\cpp\Vector3D.h:20: error: expected ';' before '&' token
..\cpp\Vector3D.cpp:35: error: expected constructor, destructor, or type conversion before '&' token
Build error occurred, build is stopped
Time consumed: 310 ms.
I resoved. Just have to add #include <iosfwd> in header file.
Topic archived. No new replies allowed.