gpt4 book ai didi

c++ - vector 空 Push_back 在 '{' 标记之前调用预期的主表达式

转载 作者:搜寻专家 更新时间:2023-10-31 01:27:06 25 4
gpt4 key购买 nike

我的编译器有问题

我在 gnugcc 中使用代码块

我想做的是:

table.push_back({""});

它说

main.cpp|22|error: expected primary-expression before '{' token|

代码在 VS 和其他编译器中工作......到底是什么?

vector < car > AddCar()
{
int i;
vector < car > table;

string check;
table.push_back({""});

for (i = 0; i < table.size(); i++)
{
cout << "marka: ";
cin >> table[i].mark;
cout << "model: ";
cin >> table[i].model;
cout << "cena: ";
cin >> table[i].price;
cout << endl;
table.push_back(car());
...

是的,我想要一个空洞的回击

最佳答案

出现此错误是因为您可能没有启用 C++11。例如,考虑 this program :

#include <iostream>
#include <string>
#include <vector>

struct Car {
std::string a;
};

int main() {
std::vector<Car> example;

example.push_back({""});
}

在 GCC 8.2.0 中使用 C++98 运行时,显示的错误是:

prog.cc: In function 'int main()':
prog.cc:12:23: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
example.push_back({""});
^
prog.cc:12:27: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
example.push_back({""});
^

使用 C++11 运行它可以修复错误。

关于c++ - vector 空 Push_back 在 '{' 标记之前调用预期的主表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54046064/

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