Why this compile error?

Write here if you have problems with your C++ source code

Moderator: Board moderators

Post Reply
User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Why this compile error?

Post by ImLazy » Wed Aug 31, 2005 2:14 pm

Code: Select all

class A{
public:
  int a;
};

class B{
public:
  void Do(A& a){ }

  A Get(){
    A a;
    return a;
  }
};

int main(){
  B b;
  b.Do(b.Get());
  return 0;
}
This program gets compile error by G++:
"test.cpp: In function `int main()':
test.cpp:18: no matching function for call to `B::Do(A)'
test.cpp:8: candidates are: void B::Do(A&)"

But the following one is accepted by G++:

Code: Select all

int main(){
  B b;
  A a=b.Get();
  b.Do(a);
  return 0;
}
I think in these two programs, "a" and "b.Get()" are both objects of Class A. But why the first one gets compile error?

User avatar
ImLazy
Experienced poster
Posts: 215
Joined: Sat Jul 10, 2004 4:31 pm
Location: Shanghai, China

Post by ImLazy » Wed Aug 31, 2005 2:21 pm

Oh, I see.
b.Get() has not an address. So an reference of it is illegal.
I stay home. Don't call me out.

Post Reply

Return to “C++”