Skip to content

Update C2534 #4756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 16, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions docs/error-messages/compiler-errors-2/compiler-error-c2534.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,29 @@ ms.assetid: 481f9f54-5b51-4aa0-8eea-218f10807705

'identifier' : constructor cannot return a value

A constructor cannot return a value or have a return type (not even a **`void`** return type).
A constructor cannot contain a **`return`** statement with an expression (even if the expression has type **`void`**). This differs from regular void-returning function where a return expression of type **`void`** is allowed. However, using the **`return`** statement without an expression is allowed for early returns in the constructor.

This error may be fixed by removing the **`return`** statement from the constructor definition.
## Example

The following sample generates C2534:

```cpp
// C2534.cpp
// compile with: /c
void void_func() {}

class A {
public:
int i;
A() { return i; } // C2534
A() {
return i; // C2534
return 123; // C2534
return (void)0; // C2534
return void_func(); // C2534

return; // OK
}
};
```

The preceding errors may be fixed by removing the entire **`return`** statement or omitting the return expression if an early return is desired.