将结构作为数组传递并存储

时间:2016-11-12 16:49:36

标签: arrays c++11 struct arguments

我的阶段看起来像这样:

enum Gait_cycle_states {
HEEL_STRIKE,
FLAT_FOOT,
MID_STANCE,
HEEL_OFF,
TOE_OFF,
MID_SWING
};

我的结构看起来像这样:

struct State {
Gait_cycle_states current_state;
uint8_t next_state; //index of the next state

uint8_t ay_num_criteria;
State_criterion *ay_criteria_for_state;

unsigned int gy_num_criteria;
State_criterion *gy_criteria_for_state;
};

我的功能如下:

void initialize_FSM(State states_array[]){
//Heel strike----------------------------------------------------------

State heel_strike = {
    .current_state = HEEL_STRIKE,
    .next_state = 1, //the index of the next state

    .ay_num_criteria = 1,
    .ay_criteria_for_state = new State_criterion[1],

    .gy_num_criteria = 2,
    .gy_criteria_for_state = new State_criterion[2]
};

//The features to look for in accel_y and gyro_y, and how old they can be to count (in ms)
heel_strike.ay_criteria_for_state[0] = { NO_FEATURE, 150 };
heel_strike.gy_criteria_for_state[0] = { NEGATIVE_TROUGH, 150 };
heel_strike.gy_criteria_for_state[1] = { BREACHED_HIGH_THRESHOLD, 30 };

//putting the state we just configured into the state array
states_array[0] = heel_strike;
};

似乎Visual Studio 2015不允许我使用“。”将值分配给我的类型。或者说,我正在做。

这给我一堆或错误和警告:

1>c:\users\arunava nag\documents\visual studio   2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(123): error C2059: syntax error: '.'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(131): error C2143: syntax error: missing ';' before '}'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C3927: '->': trailing return type is not allowed after a non-function declarator
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C3484: syntax error: expected '->' before the return type
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C3613: missing return type after '->' ('int' assumed)
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C2146: syntax error: missing ';' before identifier 'ay_criteria_for_state'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C2143: syntax error: missing ';' before '{'
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): error C2447: '{': missing function header (old-style formal list?)
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C3927: '->': trailing return type is not allowed after a non-function declarator
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C3484: syntax error: expected '->' before the return type
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C3613: missing return type after '->' ('int' assumed)
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C2086: 'int heel_strike': redefinition
 1>  c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): note: see declaration of 'heel_strike'
 1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C2146: syntax error: missing ';' before identifier 'gy_criteria_for_state'
 1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C2143: syntax error: missing ';' before '{'
 1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(135): error C2447: '{': missing function header (old-style formal list?)
 1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C3927: '->': trailing return type is not allowed after a non-function declarator
 1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C3484: syntax error: expected '->' before the return type
 1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C3613: missing return type after '->' ('int' assumed)
 1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
 1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C2086: 'int heel_strike': redefinition
 1>  c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(134): note: see declaration of 'heel_strike'
 1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C2146: syntax error: missing ';' before identifier 'gy_criteria_for_state'
 1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C2143: syntax error: missing ';' before '{'
 1>c:\users\arunava nag\documents\visual studio 2015\projects\gaitdetector\gaitdetector\gait_detector.cpp(136): error C2447: '{': missing function header (old-style formal list?)

有人可以建议我做些什么吗?

1 个答案:

答案 0 :(得分:1)

State heel_strike = {
    HEEL_STRIKE,
    1, //the index of the next state

    1,
    new State_criterion[1],

    2,
    new State_criterion[2]
};

您尝试使用的内容称为“指定初始值设定项”。它们是C语言的一个特性(自C99起),而不是标准C ++的一部分。有些编译器支持C ++作为扩展,但MSVC不支持。