Click here to Skip to main content
15,939,776 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?

Thanks.

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

What I have tried:

C++
struct LISTDATA 
{
	std::string name_;
	RECT rect_;

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

std::vector<LISTDATA> vecListData;
Posted
Updated 15-Oct-20 23:14pm
v2

1 solution

That syntax is in a later version of the compiler than the one provided by VS2008. You should upgrade to VS2019.
 
Share this answer
 
Comments
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;
}
<\pre>
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..

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