修复错误:将'const foo'作为'const size_t foo::method()'的参数传递'this'

Fixing error: passing 'const foo' as 'this' argument of 'const size_t foo::method()'

本文关键字:const foo method this 参数传递 错误 作为 size      更新时间:2023-10-16

我有一个容器类,它存储Movie对象:

MovieBase.h

#include <iostream>
#include <set>
#include <utility>
#include <vector>
#include "Movie.h"
using namespace std; // TODO: Remove this once we separate .h & .cpp files
/////////////////////////////////////////////////////////////
//  Custom comparator for set of movies
//
//  Stores movies by index #, where the lowest index
//  is at root of the tree.
/////////////////////////////////////////////////////////////
struct MovieIndexCmp {
    bool operator ()(Movie a, Movie b) {
        return a.getIndex() < b.getIndex();
    }
};
/////////////////////////////////////////////////////////////
//  MovieBase container class
/////////////////////////////////////////////////////////////
class MovieBase { 
    private: 
        set<Movie, MovieIndexCmp> cont;
    public:
        void add(Movie m) {
            cont.insert(m);
        }
        size_t size() {
            return cont.size();
        }

        void listMovies() {
            set<Movie>::iterator itr;
            itr = cont.begin();
            while(itr != cont.end() ) {
                cout << "movie #: " << (*itr).getIndex() << "   title: " << (*itr).getTitle() << "   year: " << (*itr).getYear()  <<endl;
                ++itr;
            }           
        }
};

我正在尝试使用方法listMovies()来遍历这个容器。然而,我得到以下编译器错误:

MovieBase.h: In member function ‘void MovieBase::listMovies()’:
MovieBase.h:53:44: error: passing ‘const Movie’ as ‘this’ argument of ‘const size_t Movie::getIndex()’ discards qualifiers [-fpermissive]
     cout << "movie #: " << (*itr).getIndex() << "   title: " << (*itr).getTitle() << "   year: " << (*itr).getYear()  <<endl;
                                            ^
MovieBase.h:53:81: error: passing ‘const Movie’ as ‘this’ argument of ‘std::string Movie::getTitle()’ discards qualifiers [-fpermissive]
     cout << "movie #: " << (*itr).getIndex() << "   title: " << (*itr).getTitle() << "   year: " << (*itr).getYear()  <<endl;
                                                                                 ^
MovieBase.h:53:116: error: passing ‘const Movie’ as ‘this’ argument of ‘size_t Movie::getYear()’ discards qualifiers [-fpermissive]
     cout << "movie #: " << (*itr).getIndex() << "   title: " << (*itr).getTitle() << "   year: " << (*itr).getYear()  <<endl;

这就是我的电影.h的样子:

using namespace std; // TODO: Remove this once we separate .h & .cpp files
/////////////////////////////////////////////////////////////
//  Movie container class
/////////////////////////////////////////////////////////////
class Movie { 
    private: 
        const size_t movieIndex;
        const size_t movieYear;
        const string movieTitle;
        vector<pair<size_t,string> > genreList;
    public:
        /*
            Constructor
        */
        explicit Movie(const size_t movieIndex, const string movieTitle, const size_t movieYear) 
            : movieIndex(movieIndex),
              movieTitle(movieTitle),
              movieYear(movieYear)
        {}
        /*
            Tags the movie with a specified genre
        */  
        void addGenre(pair<size_t,string> genre) {
            genreList.push_back(genre);
        }
        /*
            Returns the all of the genre's that a movie belongs to.
        */
        void listGenres() {
            vector<pair<size_t,string> >::const_iterator itr;
            itr = genreList.begin();
            while(itr != genreList.end() ) {
                cout << "genre #: " << (*itr).first << "   genre name: " << (*itr).second << endl;
                ++itr;
            }
        }
        /*
            Returns movie's unique index value
        */
        const size_t getIndex() {
            return movieIndex;
        }
        /*
            Returns movie's title
        */
        string getTitle() {
            return movieTitle;
        }
        /*
            Returns movie's year
        */
        size_t getYear() {
            return movieYear;
        }
};

我已经尝试删除所有出现的const,但没有成功。这个编译器错误说明了什么?如何修复它?

您正试图在const对象上调用一个非const成员函数。您可以将getIndex()设为const:

const size_t getIndex() const { ... }

EDIT:并删除const返回类型。