C ++操作数类型不兼容(“Movie”和“nullptr”)

时间:2014-02-20 06:21:16

标签: c++

  const Movie* Movies::getMovie(string mc, int& mn) const {
    if(mc.length()==0)
        return nullptr; // not found
    else {
        mc = myToLower(mc);
        int ndx=0;
        for(;ndx<movieCnt &&
            (myToLower(movies[ndx].getTitle()).find(mc)==
            string::npos);ndx++);
        mn = ndx<movieCnt?ndx+1:0;
        return ndx<movieCnt?&movies[ndx]:nullptr;
    }
}

//const Movie* Movies::getMovie(int mc) const {
const Movie* Movies::operator[](int mc) const{
    return (mc > 0 && mc <= movieCnt)?movies[mc-1]:nullptr;
}

我收到此错误消息error: Operand types are incompatible("Movie" and "nullptr")

在Movies.h中

public:
    Movies(string);
    int getMovieCount() const;
    const Movie* getMovie(string, int&) const;
    const Movie* getMovie(int) const;
    const Movie* Movies::operator[](int mc) const;
    ~Movies();

有任何帮助吗?谢谢!

1 个答案:

答案 0 :(得分:0)

// Movie.cpp
#include "Movie.h" // include Movie class definition
#include <sstream>
using namespace std;

Movie::Movie() {
    title = studio = "";
    boxOffice[WORLD] = boxOffice[US] = boxOffice[NON_US] = 
        rank[WORLD] = rank[US] = rank[NON_US] = releaseYear = 0;
}

Movie::Movie(string temp) {
    istringstream iS(temp);
    getline(iS, title, '\t');
    getline(iS, studio, '\t');
    iS >> releaseYear >> rank[US] >> boxOffice[US] >> rank[NON_US]
        >> boxOffice[NON_US] >> rank[WORLD] >> boxOffice[WORLD];
}

string Movie::getTitle() const {return title;}
string Movie::getStudio() const {return studio;}
int Movie::getReleaseYear() const {return releaseYear;}
int Movie::getUSRank() const {return rank[US];}
int Movie::getNonUSRank() const {return rank[NON_US];}
int Movie::getWorldRank() const {return rank[WORLD];}
long long Movie::getUSBoxOffice() const {return boxOffice[US];}
long long Movie::getNonUSBoxOffice() const {return boxOffice[NON_US];}
long long Movie::getWorldBoxOffice() const {return boxOffice[WORLD];}

std::ostream& operator << (std::ostream& os, const Movie &movie) 
{
os << "\n\n====================== Movie Information\n"
<< "\n             Movie Title:\t" << movie.getTitle()
<< "  (" << movie.getReleaseYear() << ")  " << movie.getStudio()
<< "\n    US Rank & Box Office:\t" <<  movie.getUSRank() << "\t$" <<  movie.getUSBoxOffice()
<< "\nNon-US Rank & Box Office:\t" <<  movie.getNonUSRank() << "\t$" <<  movie.getNonUSBoxOffice()
<< "\n World Rank & Box Office:\t" <<  movie.getWorldRank()<< "\t$" <<  movie.getWorldBoxOffice()
<< "\n";
return os;
}


Movie::Movie(const Movie &mP) { // copy constructor
    this->title = mP.title;
    this->studio = mP.studio;
    this->releaseYear = mP.releaseYear;
    this->rank[US] = mP.rank[US];
    this->rank[NON_US] = mP.rank[NON_US];
    this->rank[WORLD] = mP.rank[WORLD];
    this->boxOffice[US] = mP.boxOffice[US];
    this->boxOffice[NON_US] = mP.boxOffice[NON_US];
    this->boxOffice[WORLD] = mP.boxOffice[WORLD];
}

这是Movie.cpp,下面是Movies.cpp。

// Movies.cpp
#include "Movie.h" // include Movie class definition
#include "Movies.h" // include Movies class definition
#include <fstream>
using namespace std;

Movies::Movies(string fn){loadMovies(fn);}

int Movies::getMovieCount() const {return movieCnt;}

const Movie* Movies::getMovie(string mc, int& mn) const {
    if(mc.length()==0)
        return nullptr; // not found
    else {
        mc = myToLower(mc);
        int ndx=0;
        for(;ndx<movieCnt &&
            (myToLower(movies[ndx].getTitle()).find(mc)==
            string::npos);ndx++);
        mn = ndx<movieCnt?ndx+1:0;
        return ndx<movieCnt?&movies[ndx]:nullptr;
    }
}

//const Movie* Movies::getMovie(int mc) const {
const Movie* Movies::operator[](int mc) const{
    return (mc > 0 && mc <= movieCnt)?&movies[mc-1]:nullptr;
}

Movies::~Movies() {     // not needed - invoked at end
    delete[] movies;
    movies = nullptr;
}

void Movies::loadMovies(string fn) {
    ifstream iS(fn);
    string s;
    getline(iS, s); // skip heading
    getline(iS, s);
    movieCnt=0;
    movies = new Movie[MAX_MOVIES];
    while(!iS.eof()) {
        movies[movieCnt++] = Movie(s);
        getline(iS, s);
    }
    iS.close();
    reSize();
}

void Movies::reSize() {
    Movie * m = movies;
    movies = new Movie[movieCnt];
    for(int i=0;i<movieCnt;i++)
        movies[i] = m[i];
    delete[] m; // null assignment not needed; end of method
}

string Movies::myToLower(string s) const {
    string t; // initialized as ""
    for(char c : s)
        t += tolower(c);
    return t;
}

Movies.h的一部分:

public:
    Movies(string);
    int getMovieCount() const;
    const Movie* getMovie(string, int&) const;
    const Movie* getMovie(int) const;
    const Movie* Movies::operator[](int) const;
    ~Movies();

@WhozCraig谢谢你回答我!我上传了Movie.cpp,Movies.cpp和Movies.cpp的一部分我真的很好看..