是否可以创建一个字符串数组,如果是的话,为什么我会遇到此错误:错误:预期表达式

Is it possible to create an array of strings, if so, why am I getting this error: error: expected expression?

本文关键字:错误 为什么 表达式 遇到 如果 创建 是否 数组 字符串 一个      更新时间:2023-10-16
#include <iostream>
#include <array>
#include <string>
using namespace std;
typedef array<string,3> TString;
int main(){
    TString str;
    str = {"Hi","Example", "Error"};
    cout << str[0] <<endl;
    return 0;
}

我得到的原始错误代码:

pruebastr.cpp:10:8:错误:预期表达式 str = {" hi","示例","错误"}; ^生成1个错误。

我是以错误的方式定义数组或不当初始化吗?

确保您的编译器标准标志(STD)设置为至少C 11

您正在使用什么编译器?

clang 3.9.0 -std = C 11:https://godbolt.org/g/g/31ayx7

gcc6:https://godbolt.org/g/0j52zy

编译和工作正常,输出相同:" HI"