클래스에서 대입연산자를 프렌드로 선언하면 에러가 나는이유가

gyxor의 이미지

<code>
#include<iostream>
#include<iomanip>
using namespace std;

int q=10;

class pooh{
private:
public:
int a;
pooh(int i)
{
a=i;
}
virtual void v()
{
cout << a;
}
friend void operator = (pooh & h1,pooh & h2)
{
h1.a = h2.a;
}
};


int main()
{
pooh e(11);
pooh e1(55);

e = e1;

e.v();

return 0;
}
</code>
이렇게 컴파일을 하면

Compiling...
Cpp1.cpp
C:\Djgpp\Study\Cpp1.cpp(22) : error C2801: 'operator =' must be a <Unknown> member
C:\Djgpp\Study\Cpp1.cpp(23) : error C2143: syntax error : missing ';' before '.'
C:\Djgpp\Study\Cpp1.cpp(23) : error C2501: 'h1' : missing storage-class or type specifiers
C:\Djgpp\Study\Cpp1.cpp(23) : error C2059: syntax error : '.'
C:\Djgpp\Study\Cpp1.cpp(23) : error C2238: unexpected token(s) preceding ';'
C:\Djgpp\Study\Cpp1.cpp(26) : error C2143: syntax error : missing ';' before '}'
C:\Djgpp\Study\Cpp1.cpp(26) : error C2143: syntax error : missing ';' before '}'
C:\Djgpp\Study\Cpp1.cpp(26) : error C2143: syntax error : missing ';' before '}'
C:\Djgpp\Study\Cpp1.cpp(26) : error C2143: syntax error : missing ';' before '}'
Error executing cl.exe.

Cpp1.exe - 9 error(s), 0 warning(s)

이런 어마어마한 에러가 나오는데요
연산자를 * 등으로 바꿔서 대입이 아닌 계산 내용으로 바꾸게 되면
에러가 나지 않습니다.
'=' 연산자에 대해서만 프렌드로 선언하면 안되는 이유가 무엇인지
궁금합니다.
[/code]

knight2000의 이미지

C:\Djgpp\Study\Cpp1.cpp(22) : error C2801: 'operator =' must be a <Unknown> member

위 문장 그대로... 프렌드 메소드(operator =)가 클래스의 멤버가 아니기 때문입니다.

메소드(operator =)가 클래스에서 적용이 가능하도록 고쳐주어야 합니다.

프렌드 함수의 선언은 클래스 내부에서 가능하지만...
그 내용은 반드시 클래스 외부에서 기술해야 한다고 알고 있습니다.

제 답변이 틀리면 태클 걸어 주십시오.

===== ===== ===== ===== =====
knight2000 of SALM.
SALM stood for SALM Ain't a Life Model.
SALM is not the life model, but SALM is just the life.

knight2000의 이미지

제가 예제로서 찾아본 중첩정의된 operator= 함수는 모두 반환값으로 &(참조형)을 쓰던데요. void가 아니라...
우연일까요?

연산자의 중첩정의는 그 연산자 특유의 성질을 유지하는 쪽으로만 가능합니다.
예를 들면 operator+에 빼기 기능을 넣을 수는 없습니다.
이것도 C++의 특징 가운데 하나라고 합니다.

그러므로 operator= 도 우항을 좌항에 대입하는 식으로만 연산자의 중첩 정의가 가능하다고 생각합니다.

야간 알바 때문에 피곤해서 오늘은 여기까지만 조사했습니다.
좀더 자세한 설명은 계속 찾고 있습니다.
(이곳은 알바하는 PC방입니다.)

===== ===== ===== ===== =====
knight2000 of SALM.
SALM stood for SALM Ain't a Life Model.
SALM is not the life model, but SALM is just the life.

purewell의 이미지

Quote:
연산자의 중첩정의는 그 연산자 특유의 성질을 유지하는 쪽으로만 가능합니다.
예를 들면 operator+에 빼기 기능을 넣을 수는 없습니다.
이것도 C++의 특징 가운데 하나라고 합니다.

ㅡ_-); 컴파일러가 이것이 빼기 기능하는지 더하기 기능하는지
어드렇게 알 수 있는지...

제가 알기로는 C++의 연산자 오버라이딩 내용에 대한
제약은 없는 듯 합니다만... =.=;;;

_____________________________
언제나 맑고픈 샘이가...
http://purewell.biz

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.