2014-03-25 164 views
-3

Student有一個靜態成員Year這是一個指向std::string的指針,指向動態分配的數組。指向靜態的指針

ERROR: Cannot convert 'const char*' to 'std::string*' in initialization 

爲什麼我得到錯誤:

class Student 
{ 
private: 
    string name; 
    int year; 
    string semester; 
    int AmtClass; 
    static string *Year[4]; 

    //Skipping the public members 
}; 

string *Student::Year[4] = { "Freshman", "Sophomore", "Junior", "senior" }; 

問題時試圖初始化Year發生?

+0

你真的需要澄清你問這裏。就目前而言,這並沒有多大意義 – mathematician1975

+1

你不明白錯誤信息的哪一部分? – SomeWittyUsername

回答

3
struct A 
{ 
    static std::string *cohort; 
}; 

std::string * A::cohort = new std::string[4]; 

目前尚不清楚爲什麼要動態分配數組。你可以使用 std::array<std::string, 4>std::dynarray<std::string>

當你更新你的帖子,然後任你應該寫

std::string * Student::Year = new std::string[4] { "Freshman", "Sophomore", "Junior", "senior" }; 

或者

struct Student 
{ 
    static std::string Year[]; 
}; 


std::string Student::Year[4] = { "Freshman", "Sophomore", "Junior", "senior" };