Click here to Skip to main content
14,691,039 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I'm trying to define a structure (Brace initialization, has constructor) for a Vector function, but it failed to be compiled and showed these errors.
error C2059: syntax error : ','
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
error C2143: syntax error : missing ';' before '}'
I tried to run on Visual Studio 2019, it works fine. But for some reason, I have to run it on Visual Studio 2008. I'm using Visual C++ MFC Application.

It seems like need to do some conversion? Any idea?


Brace initialization for classes, structs, and unions | Microsoft Docs[^]

What I have tried:

struct LISTDATA 
	std::string name_;
	RECT rect_;

	LISTDATA(const std::string& name, RECT rect): name_{name}, rect_{rect} {}

std::vector<LISTDATA> vecListData;
Updated 16-Oct-20 0:14am

1 solution

That syntax is in a later version of the compiler than the one provided by VS2008. You should upgrade to VS2019.
Josh_0101 16-Oct-20 5:18am
Hi, is there any way to define a similar struct in VS2008?
Richard MacCutchan 16-Oct-20 5:34am
The struct is fine, it is the constructor that needs changing. Try the following, using parentheses rather than curly braces round the initialiser values.
LISTDATA(const std::string& name, RECT rect): name_(name), rect_(rect) {}
Josh_0101 16-Oct-20 5:57am
I have tried this, the syntax errors will be gone. But it will caused other errors for my CALLBACK.

error C2601: 'rect' : local function definitions are illegal
error C2143: syntax error : missing ';' before '}'
error C2065: 'name' : undeclared identifier
error ....

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
    char name[100];
    RECT rect{0};

    GetClassNameA(hwnd,name, sizeof(name));
    ::GetWindowRect(hwnd, &rect);

    std::vector<listdata>* myVec = reinterpret_cast<std::vector<listdata>*>(lParam);

    if (myVec) {
      myVec->emplace_back(LISTDATA(std::string(name), rect));
    } else {
     return FALSE;

    return TRUE;
Richard MacCutchan 16-Oct-20 6:03am
Look at the line number on the error message and it will tell you where to check. And once again you are using syntax that is not valid in your version of the compiler.
RECT rect{0};
Josh_0101 18-Oct-20 6:44am
Yes, this is 1 of the issue that I'm not sure how to define an array for `RECT` in other way to make it valid in my compiler version..
Richard MacCutchan 18-Oct-20 6:56am
You are not declaring an array, you are just declaring a single RECT variable. But you do not need to initialise it to zero, since the first reference is at GetWindowRect(hwnd, &rect); which will overwrite it.

If you must use the modern features of C/C++ then you need to move to VS2019.
Josh_0101 18-Oct-20 9:51am
I see. I thought it was an array same as the char and keep searching the solution for this..

How can I declare/initialize RECT in this CALLBACK? So far I haven't found the example for this issue and definitely I have to use VS2008 to run it.
Richard MacCutchan 18-Oct-20 10:16am
As I said above it will be initialised by the call to GetWindowRect(), so it does not need any pre-initialisation. All you need in the code is
RECT rect;

I had assumed that you wrote this code but your comments suggest that you copied it from somewhere else. If I am correct then it may be an idea to make that clear in your question.
Josh_0101 18-Oct-20 21:18pm
Thanks and it works! I get the error because of some typo when I'm making the changes. Just need RECT rect; instead of RECT rect {0};

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

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