假设我在C++11中有一个结构:
typedef struct
{
int val1;
int val2;
} Mystruct;我可以像这样初始化它,它可以工作:
Mystruct a = {1,2};但是,我如何以同样的方式重新分配值:
Mystruct a = {1,2};
...................
a = {3,4}; //this line doesn't work更新1:
这是我从visual studio复制出来的确切代码,也是我编写的唯一代码:
#include "stdafx.h"
struct Mystruct
{
int val1;
int val2;
};
int _tmain(int argc, _TCHAR* argv[])
{
Mystruct a = {1,2};
a = {3,4}; //this line doesn't work
return 0;
}这些错误是:
错误C2059:语法错误:“{”c:\workspace\trycplus\trycplus\trycplus.cpp
错误C2143:语法错误:“缺失”;“{”c:\workspace\trycplus\trycplus\trycplus.cpp之前的“
错误C2143:语法错误:缺少';‘}’之前的c:\workspace\trycplus\trycplus\trycplus.cpp
更新1:
我将Visual 2012更新为2012年11月CTP,并将平台工具集更改为:
微软视觉C++编译器2012年11月CTP (v120_CTP_Nov2012),任务仍然不起作用
发布于 2013-12-02 18:57:04
从初始化程序列表中分配在C++11中工作得很好:
a = {3,4};见现场演示。
顺便说一句,您不需要cumbersone typedef语法,您需要struct而不是strut
struct Mystruct
{
int val1;
int val2;
};发布于 2014-02-25 19:54:25
如果您正在使用C++11,那么您不应该需要#include "stdafx.h"或int t_main(int argc, _TCHAR* argv[])。t_main带有命令行参数,而您的程序并不需要这些参数。
https://stackoverflow.com/questions/20335721
复制相似问题