오버로딩 배우기 시작했는데.. 생각처럼 안 되네요.

kknd345의 이미지

오버로딩 배우기 시작했는데.. 생각처럼 안 되네요.
어느 부분이 오류인지 모르겠어요
비주얼 c++6.0 에서 4개 오류나는데요..
밑에 소스 코드랑 오류 내용있어요 좀 고쳐주세요 ㅜㅜ.

#include <iostream.h>

class CRectangle
{
protected:
	int left; int right; int top; int bottom;
public:
	CRectangle(int x1,int y1, int x2, int y2);
	void printf();
	CRectangle operator += (CRectangle data);
};

CRectangle::CRectangle(int x1,int y1, int x2, int y2)
{
	left=x1; top=y1; right=x2; bottom=y2;
}

CRectangle::operator += (CRectangle rect)
{
	left+=rect.left; right+=rect.right; bottom+=rect.bottom; top+=rect.top;
	return *this;
}

void CRectangle::printf()
{
	cout << left << right << top << bottom;
}

void main()
{
	CRectangle data1(100,10,10,0);
	CRectangle data2(100,100,200,200);
	data1+=data2;
}

C:\c++문제\overloading\overloading.cpp(19) : error C2556: 'int __thiscall CRectangle::operator +=(class CRectangle)' : overloaded function differs only by return type from 'class CRectangle __thiscall CRectangle::operator +=(class CRectangle)'
        C:\c++문제\overloading\overloading.cpp(10) : see declaration of '+='
C:\c++문제\overloading\overloading.cpp(19) : error C2371: '+=' : redefinition; different basic types
        C:\c++문제\overloading\overloading.cpp(10) : see declaration of '+='
C:\c++문제\overloading\overloading.cpp(33) : error C2264: '+=' : error in function definition or declaration; function not called
C:\c++문제\overloading\overloading.cpp(33) : error C2088: '+=' : illegal for class
doldori의 이미지

CRectangle& CRectangle::operator += (CRectangle rect) // (const CRectangle& rect)가 더 좋을 듯.

그 이외에...
iostream.h는 표준 헤더가 아닙니다. iostream과 namespace를 쓰세요.
main()의 반환형은 int입니다.
아울러 지금 보시는 책은 표준과 다르거나 매우 오래된 책이니 다른 책을 보시길 권합니다.

r0x2tk1t의 이미지

빨간책 보시는거 같군요.
Beginning Visual C++ 6.0 이였나?
암튼, 두껍고 오래된 책이죠.

에러는 연산자 오버로딩에서 난거 같네요.
또, 지금의 표준은
#include <iostream>
using namespace std;
입니다.

보기에 너무 오래되지 않았나하네요. ^^

日新 日日新 又日新
Google Talk::chanju_dot_jeon(at)gmail_dot_com