I am trying to create an automatic resource manager that works very much
the same as std::auto_ptr. However, I have a compile error I cannot
figure out. The following program demonstrates the problem. Pretend,
in each case, that -1 means "no resource", and any other number is a
handle to a valid resource. The class releases the resource in the
destructor (similar to a std::auto_ptr calling delete), so any time a
copy of the class is made, ownership of the resource should be transferred.
class A
{
int s ;
public :
A() : s(-1)
{ }
A(int s) : s(s)
{ }
A(A &a) : s(a.s)
{
a.s = -1 ;
}
~A() throw()
{
// Do something to release resource.
s = -1 ;
}
A &operator=(A &a) throw()
{
s = a.s ;
a.s = -1 ;
return *this ;
}
} ;
A f()
{
A a(5) ;
return a ;
}
int main()
{
A a ;
a = f() ;
return 0 ;
}
Compling gives the following problem:
$ g++ -W -Wall -ansi -pedantic test.cpp
test.cpp: In function `int main()':
test.cpp:41: error: no match for 'operator=' in 'a = f()()'
test.cpp:25: error: candidates are: A& A::operator=(A&)
That is, the assignment fails, for some reason I cannot determine. Any
insight would be appreciated.
Thanks,
Alan