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

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

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.