# Compilation error and refernece to a copiedct'r variable?

This topic is 4849 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have the following code:
		class B {
public:
DWORD Num;
};
class C {
public:
DWORD counter;
B ReturnClass () { B b; this->counter = 0; b.Num=2; return b;};
void UseClass (B & b) { this->counter = b.Num;};
};

C c;
c.UseClass (c.ReturnClass());

I get the following errors: error C2065: 'B' : undeclared identifier E:\Projects\Engine2\V0\Main.cpp(208) : error C2146: syntax error : missing ';' before identifier 'b' E:\Projects\Engine2\V0\Main.cpp(208) : error C2065: 'b' : undeclared identifier E:\Projects\Engine2\V0\Main.cpp(208) : error C2228: left of '.Num' must have class/struct/union type Why do I get these errors? Would the call to UseClass(c.ReturnClass()) compile if I fixed the class error? Is it not good to put in a reference paramter a return by value of a function? (or copyct'r return value). I would appreaciate help. Thanks in advance.

##### Share on other sites
I can't say why that compile-time error would occur, but I do know that you'll get runtime errors with it. In ReturnClass(), the object b is created on the stack for the duration of that function alone, and destructed when you leave the function, so by the time you get back to the call to c.UseClass() the object is no longer valid.

##### Share on other sites
Quote:
 Original post by TDragonI can't say why that compile-time error would occur, but I do know that you'll get runtime errors with it. In ReturnClass(), the object b is created on the stack for the duration of that function alone, and destructed when you leave the function, so by the time you get back to the call to c.UseClass() the object is no longer valid.

When you return a class by value, you use the copy constructor which suppose to create a new class, it is different from the class which you return in the method and that was declared inside the method.

##### Share on other sites
Oops, silly me...I've been thinking too hard today. That would only happen if you returned the address of the object. Sorry 'bout that.

##### Share on other sites
(My terminology might not be exact here as I'm not familiar enough with the relevant portions of the standard).

A fuction call which returns a class by value returns an rvalue. An rvalue may be bound to a const reference*, in which case either the existing rvalue is directly bound or a new temporary is copy-constructed from the rvalue and bound to the const reference. In either case when the const reference is a function parameter the object bound to the const reference persists until the completion of the full expression containing the call to the function for which the const reference is a parameter.

In English, your code would be entirely valid if UseClass took its parameter by const reference.

There is nothing in the code you posted that would cause the errors you're getting. The error must be elsewhere.

Enigma

*At least I think it has to be a const reference. Of my three compilers one issues an error, one a warning and one compiles it with no errors or warnings. If it doesn't have to be a const reference then the code is valid as-is.

1. 1
Rutin
61
2. 2
3. 3
4. 4
5. 5

• 12
• 10
• 28
• 20
• 9
• ### Forum Statistics

• Total Topics
633412
• Total Posts
3011751
• ### Who's Online (See full list)

There are no registered users currently online

×