预期 不允许使用表达式/类型名称
Expected An Expression / Type Name Not Allowed
Brixpath::Brixpath(){
{ _animationOptions = (AnimationOptions){5, 3, 40, 30};
};
当我运行此代码块时,VS给出错误
动画选项上不允许的类型名称。
当我删除类型名称时
Brixpath::Brixpath(){
{ _animationOptions = {5, 3, 40, 30};
};
VS2010 在第 2 行的第一个"{"处给出错误
错误:预期的表达式
动画选项的定义是-
struct AnimationOptions {
int maxClicks; //how many clicks animation on screen to support
int step; // animation speed, 3 pixels per time
int limit; //width of animation rectangle. if more, rectangle dissapears
int distance; //minimum distance between previous click and current
};
如何解决此错误?请帮忙。
给定VS 2010的用户(即不能使用C++11统一初始化(,您可能希望向结构中添加构造函数,然后使用它来初始化结构:
struct AnimationOptions {
int maxClicks; //how many clicks animation on screen to support
int step; // animation speed, 3 pixels per time
int limit; //width of animation rectangle. if more, rectangle dissapears
int distance; //minimum distance between previous click and current
AnimationOptions(int maxClicks, int step, int limit, int distance) :
maxClicks(maxClicks), step(step), limit(limit), distance(distance) {}
};
Brixpath::Brixpath() : _animationOptions(5, 3, 40, 30) {}
如果您需要将动画选项维护为 POD,我相信您可以使用支撑初始化而不是成员初始化来简化代码:
AnimationOptions make_ao(int clicks, int step, int limit, int distance)
{
AnimationOptions ao = {clicks, step, limit, distance};
return ao;
};
这将起作用,并且是首选选项(需要 C++11(:
Brixpath::Brixpath() : _animationOptions{5, 3, 40, 30}
{
};
在这里,您在构造函数初始化列表中初始化_animationOptions
,而不是在构造函数的主体中为其赋值。
在没有 C++11 支持的情况下,您可以AnimationOptions
提供一个构造函数,在这种情况下它将不再是 POD,或者逐个元素设置。如果这是一个问题,您还可以创建一个初始值设定项函数:
AnimationOptions make_ao(int clicks, int step, int limit, int distance)
{
AnimationOptions ao;
ao.maxClicks = clicks;
ao.step = step;
....
return ao;
};
然后
Brixpath::Brixpath() : _animationOptions(make_ao(5, 3, 40, 30))
{
};
这会AnimationOptions
保留为 POD,并将初始化与构造函数代码分离。
如何解决此错误?
使用 c++11 标准选项编译代码,或按成员方式初始化结构:
Brixpath::Brixpath()
{
_animationOptions.maxClicks = 5;
_animationOptions.step = 3;
_animationOptions.limit = 40
_animationOptions.distance = 30;
};
相关文章:
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 如何计算具有指定类型的表达式的相对精度和绝对精度
- 来自 DLL 的函数调用 [表观调用的括号前面的表达式必须具有(指向-)函数类型]
- 表达式 SFINAE:如何根据类型是否包含具有一个或多个参数的函数来选择模板版本
- 有没有办法一次声明相同类型的多个对象,并通过一个表达式立即使用相同的右值初始化它们?
- 表观调用前面的表达式必须具有指向 func 类型的指针
- 数组类型 int[n][n] 不可赋值,因为表达式必须具有常量值
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- 表达式必须具有类类型 vs.
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- 非类类型表达式的静态类型与动态类型之间的差异
- 如何确定涉及 C++ 中除法的算术表达式的数据类型
- 结构化绑定初始值设定项表单 { 赋值表达式 } 对于 clang 上的数组类型失败
- 下标需要数组或指针类型表达式必须具有指针对象类型
- 带有数组类型表达式的错误分配
- 表示函数参数的元组的类型表达式
- 如何确定撤销类型表达式的更大类型
- c++ 11中有新的函数类型表达式格式吗?