gpt4 book ai didi

c++ - 在 C++ 中插入结构内的 vector 时出错

转载 作者:行者123 更新时间:2023-11-30 00:41:23 24 4
gpt4 key购买 nike

如何插入到结构体内部赋值的 vector 中?代码是这样写的

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’

最佳答案

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);

关于c++ - 在 C++ 中插入结构内的 vector 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3769020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com