Click here to Skip to main content
15,884,905 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
So I have a WindowSize RECT declared as a private variable in my WinClass, class. I tried to use block initialization in the constructor, but alas, it made me initialize each member separately so my question is why doesn't this work?
C++
WindowSize = { 0, 0, 300, 300 };

This works:
C++
RECT WindowSize = {0,0,300,300};

Like I said in the title it is more of a curiosity for me, so if you have the time and patience to explain it to me, allow me to say thanks in advance -- Steve.
WinClass.h
C++
private:
	RECT WindowSize;

WinClass.cpp
C++
WinClass::WinClass(HINSTANCE hInstance){
	WindowSize = { 0, 0, 800, 600 };...}

Errors:
C++
error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
Posted

1 solution

This code declares a new variable with some initial values at compile time, and that is fine, the compiler creates the variable from scratch.
C++
RECT WindowSize = {0,0,300,300};


This code
C++
WindowSize = { 0, 0, 300, 300 };

tries to assign multiple values to an existing variable, at run time, which is not allowed. At run time you must assign each member of the class or structure individually.
 
Share this answer
 
Comments
drkwlf 9-Dec-15 15:49pm    
That makes sense to me, thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900