我正在尝试将用户输入的特定数量的电影放入数组。
然后,我将在函数中使用类数组的指针,如下所示

当我编译程序时,它给我一条错误消息,说
“无法将'(MovieData *)(&pMovie)'从'MovieData *'转换为MovieData'

我的 body

int amount = 0;
cout << "How many movies did you watch last month?";
cin >> amount;

MovieData *pMovie[amount];
*pMovie = new MovieData[amount];

cout << "Please enter the information of your Movies!" << endl;
readMovieData(pMovie, amount);         //This line!

cout << "Here are the information with your Movies!" << endl;
MovieDatastats(pMovie, amount);        //This line!

这是我的功能
void readMovieData(MovieData *pMovie, int const size)
{
string title;
string director;
int year;
int time;

for(int i = 0; i < size; i++)
{
    cout << "Please enter the Title of the movie.";
    getline(cin,title);
    pMovie[i].setTitle(title);

    cout << "\nPlease enter the Director name of the movie.";
    getline(cin,director);
    pMovie[i].setDirector(director);

    cout << "\nPlease enter the year it was released.";
    cin >> year;
        if(year >= 1900 && year <= 2004)
            pMovie[i].setYear(year);
        else
        {
            cout << "Please enter a year between 1900 and 2004." << endl;
            year = 0;
        }

    cout << "\nPlease enter the time the movie last.";
    cin >> time;
        if(time > 0 && time < 14400)
            pMovie[i].setTime(time);
        else
        {
            time = 0;
            cout << "Please enter a time between 0 and 14400 in minutes." << endl;
        }
    }
}

这是为了我的功能将信息设置为私有(private)类

另一个功能是处理私有(private)类(class)的信息
void MovieDatastats(MovieData *pMovie, int const size)
{
float sumTime = 0.0;
float averageTime = 0.0;
for (int i = 0; i < size; i++)
{
    sumTime +=pMovie[i].getTime();
}
averageTime = sumTime/size;

cout << "Your average time of all your movie is: " << averageTime << endl;

int oldYear = 0;
int count = 0;
int recentYear = 0;
int counter = 0;

oldYear = pMovie[0].getYear();
recentYear = pMovie[0].getYear();

for (int j = 1; j < size; j++)
{
    if(pMovie[j].getYear() < oldYear)
    {
        oldYear = pMovie[j].getYear();
        count = j;
    }
    else if(pMovie[j].getYear() > recentYear)
    {
        recentYear = pMovie[j].getYear();
        counter = j;
    }
}

cout << "The oldest Movie you have watched is called..." << endl;
cout << "Title: " << pMovie[count].getTitle() << endl;
cout << "Director: " << pMovie[count].getDirector() << endl;
cout << "Release Year: " << pMovie[count].getYear() << endl;

cout << "The most newest Movie you have watched is called..." << endl;
cout << "Title: " << pMovie[counter].getTitle() << endl;
cout << "Director: " << pMovie[counter].getDirector() << endl;
cout << "Release Year: " << pMovie[counter].getYear() << endl;
}

另外,如何删除分配的内存并将指针设置为NULL?

最佳答案

您的问题在这里:

MovieData *pMovie[amount];
*pMovie = new MovieData[amount];

第一行声明电影数据指针数组。您不需要-您想要的是指向MovieData的指针。因此,第二行也是错误的。这是更正:
MovieData *pMovie;
pMovie = new MovieData[amount];

至于何时释放,这是使用上述代码的危险。当不再需要pMovie时可以取消分配。但是当您不再需要该时间时,这就是内存检查器存在的原因-容易出错。这就是 vector 很方便的原因:
#include <vector>
//...
std::vector<MovieData> pMovie(amount);

而已。无需取消任何分配。现在,要将其与您现有的函数接口(interface),请传递 vector 中第一个元素的地址:
readMovieData(&pMovie[0], pMovie.size());

关于c++ - 编译错误:无法转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22309444/

10-10 07:25