2012-07-27 52 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]; 
}