1

I am doing a college project and one of the things I need to do is a getter for a vector of strings, I was not sure how I could implement this, but them I found this pretty explanatory answer.

But here is the thing : I need to do this using different files, a header and an cpp file. The signature of the getter in the header goes like this :

vector<string> const &getKeyWords() const;

Meanwhile, this is how I implemented the method :

vector<string> Midia:: const &getKeywords() const { return this->keywords; }

but this implementation is giving me the following compilation error :

Midia.cpp:29:24: error: expected unqualified-id before ‘const’
    vector<string> Midia:: const &getKeywords() const { return this->keywords; }

Quick observation : the "const" the compiler is talking about is the first one.

And I just don't know what could be the correct way of implementing this sort of getter. Any help in solving this issue gonna be apreciated.

HaveMercy
  • 65
  • 4

1 Answers1

1

The problem is that the syntax for the out of class definition of the member function is wrong.

The correct syntax would be as shown below. In the modified program, we have the name of the class followed by the name of the member function with the scope resolution operator:: in between them. Also, the first const is part of the return type so it can't follow the name of the class.

//--------------------vvvvv--vvvvvvvvvvv------------>name of class followed by name of member function with scope operator in between
vector<string> const& Media::getKeyWords() const 
//-------------^^^^^-------------------------------->const is part of return type 
{ 
    return this->keywords; 
}
Jason
  • 36,170
  • 5
  • 26
  • 60