Brixpath::Brixpath(){
{ _animationOptions = (AnimationOptions){5, 3, 40, 30};
};
当我运行此代码块时,VS给出错误
AnimationOptions上不允许使用typename。
当我删除类型名称时
Brixpath::Brixpath(){
{ _animationOptions = {5, 3, 40, 30};
};
VS2010在第二行的第一行“ {”给出错误
错误:需要一个表达式
动画选项的定义是-
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) {}
如果您需要将AnimationOptions保持为POD,我相信您可以使用支撑初始化而不是成员初始化来简化代码:
AnimationOptions make_ao(int clicks, int step, int limit, int distance)
{
AnimationOptions ao = {clicks, step, limit, distance};
return ao;
};