简体   繁体   中英

Simple program with a structure. Getting this: invalid operands of types 'void' and '<unresolved overloaded function type>' to binary 'operator

I'm trying to call a void function that displays contents of a structure variable but I get this error when I call the function.

invalid operands of types 'void' and '<unresolved overloaded function type>' to binary 'operator

Honestly I am new to c++ and I don't understand what the error means. How do I fix this?

#include <iostream>

using namespace std;

struct MovieData
{
    string title;
    string director;
    int yearReleased;
    int runningTimeInMinutes;
};

void showMovieData(MovieData movie);

int main()
{
    MovieData apocalypseNow = {"Apocalypse Now", "Francis Ford Coppola", 1979, 153};
    MovieData theWizardOfOz = {"The Wizard of Oz", "Victor Fleming", 1939, 101};

    //error occurs here
    showMovieData(apocalypseNow) << endl;
    showMovieData(theWizardOfOz) << endl;

}


void showMovieData(MovieData movie)
{
    cout << "Title: " << movie.title << endl;
    cout << "Director: " << movie.director << endl;
    cout << "Year Released: " << movie.yearReleased << endl;
    cout << "Running Time (in minutes): " << movie.runningTimeInMinutes << endl;
}

Take a look at this code:

showMovieData(apocalypseNow) << endl;
showMovieData(theWizardOfOz) << endl;

Here, showMovieData is a function that returns void , meaning that it doesn't evaluate to a value. The code you've written is then trying to apply operator << to a nonexistent value and endl , which is impossible because you can't apply any operators to a void value.

To fix this, consider rewriting the code as

 showMovieData(apocalypseNow);
 cout << endl;
 showMovieData(theWizardOfOz);
 cout << endl;

Alternatively, replace showMovieData with a global operator<< operator that can be used to display objects of type MovieData , like this:

ostream& operator<<(ostream& out, const MovieData& movie)
{
    out << "Title: " << movie.title << endl;
    out << "Director: " << movie.director << endl;
    out << "Year Released: " << movie.yearReleased << endl;
    out << "Running Time (in minutes): " << movie.runningTimeInMinutes << endl;
    return out;
}

Then, you can write

cout << apocalypseNow << endl;
cout << theWizardOfOz << endl;

That said, the operator<< definition above isn't ideal because it inserts endl into the stream, flushing the contents, but it should also work. You may want to consider to replace endl inside operator<<(ostream&, const MovieData&) with '\\n' which avoids the flushing.

Hope this helps!

You can't call the function "showMovieData" in the same expression that you call the endl statement.

You should rewrite those two lines giving errors as:

showMovieData(apocalypseNow);
cout << endl;
showMovieData(theWizardOfOz);
cout << endl;

Edit - beaten to it, see answer below which is more thorough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM