我正在尝试编写一个程序,当用户输入电影的关键字时,程序会在标题中搜索它并返回结果。我坚持如何尝试做到这一点。我不断收到关于头类中没有默认构造函数的错误。我不知道如何解决这个问题。
这是头类
// Movies.h
#ifndef MOVIES_H
#define MOVIES_H
#include "Movie.h" // include Movie class definition
#include <string>
using namespace std;
class Movies {
// data is private by default
static const int MAX_MOVIES = 1000;
Movie movies[MAX_MOVIES];
int movieCnt;
public:
Movies(string);
void Test(string);
const Movie getMovie(int);
private:
void loadMovies(string);
string myToLower(string);
};
#endif
这是头文件的 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);}
const Movie Movies::getMovie(int mc) {
return movies[mc-1];
}
void Movies::loadMovies(string fn) {
ifstream iS(fn);
string s;
getline(iS, s); // skip heading
getline(iS, s);
movieCnt=0;
while(!iS.eof()) {
movies[movieCnt++] = Movie(s);
getline(iS, s);
}
iS.close();
}
void Movies::Test(string key)
{
Movies[1];
}
string Movies::myToLower(string s) {
int n = s.length();
string t(s);
for(int i=0;i<n;i++)
t[i] = tolower(s[i]);
return t;
}
这是我的主要功能
// MovieInfoApp.cpp
#include "Movie.h" // include Movie class definition
#include "Movies.h" // include Movies class definition
#include <iostream>
#include <string>
using namespace std;
void main() {
Movies movies("Box Office Mojo.txt");
string answer, key;
bool set = false;
int movieCode, ant;
cout<< "Would you like to start the Movie search?";
cin>> answer;
while (answer =="y" ||answer =="Y")
{
cout<< "would you like to enter a movie name or a movie number? (press 1 for movie name press 2 for number";
cin>>ant;
if (ant = 2)
{
cout << "Please enter the movie number: ";
cin >> movieCode;
Movie m = movies.getMovie(movieCode);
if(m.getTitle().length() > 0)
{
cout << m.toString() << "\n";
}
else
{
cout << "\n Movie not found!\n\n" << endl;
}
}
else if (ant =1)
{
cout << "Please enter a keyword or title of the movie: ";
cin >> key;
Movies tester; // No default constructor error over here
tester.Test(key);
}
else
{
cout<< "invalid entry please try again";
}
cout<< "Would you like to continute the Movie search?";
cin>> answer;
}
}