如何插入结构内分配的向量? 代码是我写的
struct state{
int time_taken;
vector<int>time_live(1);
string loc_name;
vector<string>loc(1);
};
state A[100];
A[flag1]->loc.insert(flag2);
A[flag1]->time_live.insert(time);
A[flag2]->loc.insert(flag1);
A[flag2]->loc.insert(time);
我得到的错误就是这个
kris_UCS.cpp:11: error: expected identifier before numeric constant
kris_UCS.cpp:11: error: expected ‘,’ or ‘...’ before numeric constant
kris_UCS.cpp:13: error: expected identifier before numeric constant
kris_UCS.cpp:13: error: expected ‘,’ or ‘...’ before numeric constant
kris_UCS.cpp: In function ‘int main()’:
kris_UCS.cpp:60: error: base operand of ‘->’ has non-pointer type ‘state’
kris_UCS.cpp:61: error: base operand of ‘->’ has non-pointer type ‘state’
kris_UCS.cpp:63: error: base operand of ‘->’ has non-pointer type ‘state’
kris_UCS.cpp:64: error: base operand of ‘->’ has non-pointer type ‘state’
答案 0 :(得分:7)
struct state
{
int time_taken;
vector<int> time_live; // no parenthesis
string loc_name;
vector<string> loc; // no parenthesis
};
state A[100];
A[flag1].loc.push_back(flag2);
A[flag1].time_live.push_back(time);
A[flag2].loc.push_back(flag1);
A[flag2].loc.push_back(time);