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

标签 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")

在电影中.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();

有什么帮助吗?谢谢!

最佳答案

// 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 我真的很困惑..

关于C++ 操作数类型不兼容 ("Movie"和 "nullptr"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21899811/

相关文章:

c++ - 将派生对象存储到 void*,然后从中转换基对象是否不安全?

c++ - 使用 GLM 在 OpenGL 中进行轨道旋转和自旋旋转

c++ - 保存 C++ 预处理器宏的原始值

c++ - 当按网络字节顺序放置 double 时,为什么它以 4 字节 block 的形式完成?

c++ - 错误 : invalid initialization for struct in C++

c++ - Ogre RenderWindow 断言失败错误

c++ - 使用 +(一元加)解决 lambda 的函数指针和 std::function 上的不明确重载

c++ - 尝试使用 C++ 图形,我已经包含了头文件,甚至出现错误?

c++ - QRegExp 不匹配任何东西

c++ - 无法声明字段 ‘Executive::history’为抽象类型