vendredi 25 décembre 2015

Getter worked but returns an error when i turn it into a const

So I'm currently developping a so and a dll file to handle the files of my users (basically a filesystem). I have two classes, one is FileData (representing a single file), and a FileList (which as the name suggests is a list of FileDatas). Here is my problem. My code was working, but I needed to implement a sort method based on the metadatas I get on my file and to do that I needed to get my getter to a const method :

I'm compiling with the flags -Wextra -Wall -Werror

FileData.hpp

class FileData
{
 // ...
 std::vector<std::pair<std::string, std::string> > _metadatas;
public:
 FileData(std::string);
 ~FileData();
 FileData(const FileData &);
 std::vector<std::pair<std::string, std::string> > const &getMetadatas() const;
};

FileData.cpp (i tried to both return (_metadatas) and (&_metadatas) both fail)

std::vector<std::pair<std::string, std::string> > const &FileData::getMetadatas() const
 {
   return (_metadatas);
 }

And here is the error message :

 FileData.cpp: In member function ‘std::vector<std::pair<std::basic_string<char>, std::basic_string<char> > >& FileData::getMetadatas() const’:
 FileData.cpp:164:23: error: invalid initialization of reference of type ‘std::vector<std::pair<std::basic_string<char>, std::basic_string<char> > >&’ from expression of type ‘const std::vector<std::pair<std::basic_string<char>, std::basic_string<char> > >’
 return (_metadatas);
                    ^
make: *** [FileData.o] Error 1

And here is the reason in my FileList.cpp i need to get the getMetadatas a const getter (even though i know getters should always be const anyway) :

FileList.cpp

bool                SortMetadata::sortArtist(const FileData &a, const FileData &b)
{
   int i,j;
   std::vector<std::pair<std::string, std::string> > tmp, temp;
   tmp = a.getMetadatas();
   temp = b.getMetadatas();
   std::string first("");
   std::string second("");
   for (i = 0; i < tmp.size(); ++i)
    {
       if (tmp[i].first.compare("artist") == 0)
           first = tmp[i].second;
    }
   for (j = 0; j < temp.size(); ++j)
    {
        if (temp[j].first.compare("artist") == 0)
            second = temp[j].second;
    }
   return (first.compare(second) < 0);
}

Here is the error message i get if i don't make my getMetadatas() a const method :

 FileList.cpp: In function ‘bool SortMetadata::sortArtist(const FileData&, const FileData&)’:
 FileList.cpp:91:26: error: passing ‘const FileData’ as ‘this’ argument of ‘std::vector<std::pair<std::basic_string<char>, std::basic_string<char> > >&  FileData::getMetadatas()’ discards qualifiers [-fpermissive]
 tmp = a.getMetadatas();
                      ^
 FileList.cpp:92:27: error: passing ‘const FileData’ as ‘this’ argument of ‘std::vector<std::pair<std::basic_string<char>, std::basic_string<char> > >& FileData::getMetadatas()’ discards qualifiers [-fpermissive]
 temp = b.getMetadatas();

I'm not sure what's the problem here, since the code was working fine before I changed my getter to const, and I don't understand what it changes to be const, since the method is just a return.. Thanks for the help guys !

Aucun commentaire:

Enregistrer un commentaire