修复错误:传递' const foo'作为'这个'参数' const size_t foo :: method()'

时间:2014-04-30 00:56:59

标签: c++

我有一个容器类,用于存储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;

这就是我的Movie.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,但这不起作用。这个编译器错误说什么,如何修复?

1 个答案:

答案 0 :(得分:0)

您正在尝试在const对象上调用非const成员函数。你可以使getIndex()const:

const size_t getIndex() const { ... }

编辑:并删除co​​nst返回类型。