在c ++中的派生类中初始化字符串数组或2d char数组

时间:2014-11-02 05:32:02

标签: c++ inheritance arrays

这是一个片段:

class student
   {
    protected:
     string name, subject[6];
      ....
    };
class ugstudent : public student
{
   ...
};
class ugfirst_yr : public ugstudent
{
subject[6] = {"phy", "chem", "math", "electrical", "civil", "comp"};  //error*
 public:
  ...
};

*我为上述初始化得到的错误:“此声明没有存储类或类型说明符”,  对于数组中的内容:类型为“const char *”的值不能用于初始化“int”类型的实体

我想要的是字符串数组subject []是常见的,但是数组的内容应该在从类ugstudent派生的每个类中有所不同。我应该改变什么?

1 个答案:

答案 0 :(得分:-1)

尝试这样的事情

class student
{
public:
    vector<string> subject;

    student()
    : subject{ "a", "b", "c" }
    {
    }
};
相关问题