不能将字符串数组声明为类成员

Pho*_*nix 1 c++ string declaration

我无法在班上声明一个字符串数组.在我的班级定义下面:

class myclass{

    public:
        int ima,imb,imc;
        string luci_semaf[2]={"Rosso","Giallo","Verde"};
  };
Run Code Online (Sandbox Code Playgroud)

和我的主文件

#include <iostream>
#include <fstream> 
#include "string.h"
#include <string>
using namespace std;
#include "mylib.h"
int main() {

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

为什么我会收到以下警告/错误?

在此输入图像描述

Som*_*ude 5

你有两个问题:第一个是你无法像这样初始化内联数组,你必须使用构造函数初始化列表.第二个问题是您尝试使用三个元素初始化两个元素的数组.

要初始化它,例如

class myclass{
public:
    int ima,imb,imc;
    std::array<std::string, 3> luci_semaf;
    // Without C++11 support needed for `std::array`, use
    // std::string luci_semaf[3];
    // If the size might change during runtime use `std::vector` instead

    myclass()
        : ima(0), imb(0), imc(0), luci_semaf{{"Rosso","Giallo","Verde"}}
    {}
};
Run Code Online (Sandbox Code Playgroud)