2012-07-27 50 views
0

兩者都在運營商=在同一類不能從2D陣列到另一個2D陣列做一個strcpy

這裏是函數的定義。下面

void segment::operator=(const segment& w) { 

     strcpy(this->phrase, w.getPhrase()); //this line creates a problem. 

錯誤是:

segment.cpp: In member function ‘void segment::operator=(const segment&)’: 
segment.cpp:186: error: passing ‘const segment’ as ‘this’ argument of ‘const char* 
segment::getPhrase()’ discards qualifiers 
segment.cpp:186: error: cannot convert ‘char (*)[40]’ to ‘char*’ for argument ‘1’ to ‘char* strcpy(char*, const char*)’ 

const char* segment::getPhrase(){ 
     return *phrase; 
} 

及以上功能getPhrase

我不知道爲什麼我不能爲做一個strcpy的。

我正在嘗試完成作業。

編輯:

這是phrase

char phrase[10][40]; 
+0

什麼是變量「短語」的確切類型更換10? – Itaypk 2012-07-27 20:06:24

+0

問題更新@Itaypk謝謝! – Ali 2012-07-27 20:07:24

回答

4

類型有兩個問題。首先,你必須使getPhrase成爲const方法。第二個問題是strcpy不能用於額外的間接級別。你可能需要的東西是這樣的:

const char* segment::getPhrase(int index) const { 
    return phrase[index]; 
} 

void segment::operator=(const segment& w) { 
    int index; 
    for (index = 0; index < 10; ++index) { 
     strcpy(this->phrase[index], w.getPhrase(index)); 
    } 
} 

你應該不斷

class segment { 
    //other stuff 
    static const int kNumPhrases = 10; 
    char phrase[kNumPhrases][40]; 
}