[완료] 연산자 오버로딩을 클래스 내부에서 하는 것과 외부에서 하는것의 차이가 무엇인지요?

goraion의 이미지

원래 클래스 외부에서 연산자 오버로딩을 한 다음 자꾸 에러가 발생해서
아래와 같은 질문을 했습니다.

혹시나 싶어서 전역으로 선언한 오버로딩을 클래스 내부로 선언하고
그에 따라 적절히 아래의 코드를 수정해주었더니 잘 되네요.

도대체 무슨 이유가 있어서

A+B+C와 같은 경우 클래스 내부의 오버로딩을 할 때는 컴파일이 되고, 전역으로 오버로딩을 할 때는 안되는 것일까요?

궁금하네요.

========== 원 질문 ===============

현재 *에 scalar multiplification, matrix multiplification 을 오버로딩했고,
+에 matrix addition을 오버로딩 해 두었고, 계산도 잘 해줍니다.

문제는
Matrix D = A+B+C
와 같이 연산을 한번에 여러번 하게 되면,
다음과 같은 error가 발생하는 군요.

test.cpp:25: error: no match for ‘operator+’ in ‘operator+(Matrix&, Matrix&)(((Matrix&)(& B))) + C’
matrix_ed.h:73: note: candidates are: Matrix operator+(Matrix&, Matrix&)

아무래도 A+B를 하나의 매트릭스로 보지 않고 operator+(Matrix&,Matrix&)로 인식하고 있어서 일어나는 문제인 것 같습니다만.

어떻게 해결하는 방법이 없을까요?

구글링해도 딱히 나오는 답변이 없네요.

부탁드리겠습니다.

============== 혹시나 싶어서 오버로딩 부분 소스를 올려두었습니다 ================
이상하게 게시판에선 인덴트가 안되네요.
// #### Unary Operator
Matrix operator-(Matrix &A)
{
double x = -1.0;
Matrix C = x*A;
return C;
}

// #### Binary Operator
// Matrix Addition
Matrix operator+(Matrix &A, Matrix &B)
{
Matrix C;
if(!(A.rows == B.rows && A.cols == B.cols))
{
std::cout }
else
{
C.rows = A.rows;
C.cols = A.cols;
C.initial();
for(int i=0; i {
C.data[i] = A.data[i] + B.data[i];
} // i
}
return C;
}

// Scalar Multiplification
Matrix operator*(double a, Matrix &A)
{
Matrix C = A;
for(int i=0; i {
C.data[i] *= a;
}
return C;
}

// Matrix Multiplification
Matrix operator*(Matrix &A, Matrix &B)
{
Matrix C(A.rows, B.cols, 0.0);
for(int i=0; i {
for(int j=0; j {
for(int k=0; k {
C(i,j) += A(i,k)*B(k,j);
} // k
}// j
} // i
return C;
}

kwchun의 이미지

const 키워드로 해결되는군요.

#include <cstdlib>
 
struct Matrix
{
};
 
const Matrix operator+(const Matrix &A, const Matrix &B)
{
  return Matrix();
}
 
int main(int argc, char* argv[])
{
  Matrix a, b, c, d;
 
  d = a + b + c;
  return EXIT_SUCCESS;
}
goraion의 이미지

일단 저는 class로 했습니다. const 키워드를 추가했는데도 같은 에러가 뜹니다.

goraion의 이미지

아까는 const Matrix가 중요하다 생각해서 받아오는 인자는 손대지 않았는데,

이번에는 Matrix operator+(const Matrix &A, const Matrix &B) 와 같이 하니까 해결되네요.

지식이 부족해서 왜 이런 차이를 보여주는지 모르겠습니다. 책을 열심히 읽어봐야겠어요.

감사합니다. ^^

goraion의 이미지

클래스 내부에서 연산자 오버로딩을 한 경우에 A+B+C와 같은 경우와 A*B + C와 같은경우 잘 작동하였으나
A + B*C 와 같은 연산자에서는 오류를 발생시키네요.

결국 클래스 내부에서 연산자 오버로딩을 하는것도 근본적인 해결책이 되지 않는 것 같습니다.

댓글 달기

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