Question

Both are in the operator= in the same class

here is the definition of the function.

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

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

error is below:

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;
}

And above is the function getPhrase

I don't know why I can't do a strcpy for that.

I'm trying to completet the assignment.

EDIT:

This is the type of phrase

char phrase[10][40];
Was it helpful?

Solution

There are two problems. First you have to make getPhrase a const method. The second problem is that strcpy doesn't work with an extra level of indirection. You probably need something like this:

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)); 
    }
}

You should replace the 10 with constant

class segment {
    //other stuff
    static const int kNumPhrases = 10;
    char phrase[kNumPhrases][40];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top