编译错误:无法转换

时间:2014-03-10 19:26:46

标签: c++ class pointers

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

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

我的身体

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;
        }
    }
}

这是我的函数将信息设置为私有类

和另一个用来处理私人班级信息的功能

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?

2 个答案:

答案 0 :(得分:2)

你的问题在这里:

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

第一行声明了一个Movie数据指针数组。你不想要 - 你想要的是一个指向MovieData的指针。因此,第二行也是错误的。这是更正:

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

关于何时解除分配,这是使用上述代码的危险。当您不再需要pMovie时,您将解除分配。但是当那个时候你不再需要它的时候就是为什么存在记忆检查器 - 它很容易犯错误。这就是为什么矢量如此方便:

#include <vector>
//...
std::vector<MovieData> pMovie(amount);

就是这样。无需解除任何分配。现在,要将其与现有函数接口,请传递向量中第一个元素的地址:

readMovieData(&pMovie[0], pMovie.size());

答案 1 :(得分:0)

MovieData *pMovie[amount];

MovieData *pMovie

是两种不同的类型。第一个是包含MovieData *类型元素的数组,而第二个是标量类型,它是指向MovieData的指针。

您按以下方式声明了函数readMovieData和MovieDatastats

void readMovieData(MovieData *pMovie, int const size);
void MovieDatastats(MovieData *pMovie, int const size);

使用类型

的第一个参数
MovieData *pMovie

但是,当你调用这些函数时

readMovieData(pMovie, amount);

MovieDatastats(pMovie, amount);

你传递参数

MovieData *pMovie[amount];

编译器无法隐式转换MovieData * pMovie [amount];到MovieData * pMovie。

函数应声明为

void readMovieData(MovieData **pMovie, int const size);
void MovieDatastats(MovieData **pMovie, int const size);

相应地,您需要更改函数的主体。

或者您可以拥有声明它们的函数,但将其作为参数传递给它们

*pMovie

这是数组的第一个元素。

例如

void readMovieData(MovieData *pMovie, int const size);
void MovieDatastats(MovieData *pMovie, int const size);
//...

readMovieData(*pMovie, amount);

MovieDatastats(*pMovie, amount);