简体   繁体   中英

struct output with cout, and function call struct parameters

So, the problem is several errors at compile time.

ReadMovieData(string* title, string* director) cannot convert from movieInfo to string*

DisplayMovieData(string title, string director) cannot convert from movieInfo to string

No operator found which takes a right-hand operand of type 'movieInfo' (or there is no acceptable conversion. 

The bottom error happens twice in DisplayMovieData() so I wrote it once for simplicity sake.

The ReadMovieData function should accept a structure pointer reference variable and the DisplayMovieData function should accept a MovieInfo structure variable.

The main function creates an array of 2 MovieInfo struct variables and the other functions should be called on an element of the array.

The code I have finished is below.

#include <stdafx.h>
#include <string>
#include <iomanip>
#include <iostream>
using namespace std;

//prototypes
int ReadMovieData(string* title, string* director);
int DisplayMovieData(string title, string director);

struct movieInfo {
    string title, director;
};

int main(){

    const int SIZE = 2;
    movieInfo movieList[SIZE];
    movieInfo movie;

    //supposed to assign data to movieList[i] at some point
    for (int i = 0; i < SIZE; i++){
        ReadMovieData(movie, movie);
        DisplayMovieData(movie, movie);
    }
    return 0;
}

int ReadMovieData(movieInfo &title, movieInfo &director){

    movieInfo movie;

    //get the movie name
    cout << "What is the movie? ";
    cin.ignore();
    cin >> movie.title;

    //get the movie director
    cout << "What is the director of " << movie.title << "?";
    cin.ignore();
    cin >> movie.director;

    return 0;
}

int DisplayMovieData(movieInfo title, movieInfo director){

    cout << "The movie name is: " << title << endl;
    cout << "The director of " << title << " is: " << director << endl;

    return 0;
}

The errors are pretty explanatory and clear - your function takes string* but you're passing movieInfo - unrelated types can't just magicaly convert one to another.

What you probably want is pass the data members of movieInfo :

ReadMovieData(&movie.title, &movie.director);

It would be better if arguments were not pointers - use references instead. Where you won't be changing the arguments, the references should be to const type.

Even better, why not just pass moveInfo

ReadMovieData(movieInfo& movie);

and let the function deal with the internals of the class? This better encapsulates data and doesn't lead to spaghetti code quite so fast.

Also, the declarations and definitions need to match (otherwise you'd be overloading ) - you're using pointers in some places and references/values in others.

Finally, here's how an overload of operator<< might look like:

std::ostream& operator<<(std::ostream& os, const movieInfo& m)
{
    return os << "Title: " <<  m.title << ", Director: " << m.director; 
}

There are mismatches between your function prototypes and their definitions, as you can see comparing the parameter types in both.

Note that since you defined a structure for the movie info, you can directly pass it to the reading and displaying functions (instead of passing the single structure data member strings).

You may want to read the following compilable code:

#include <iostream>
#include <string>
using namespace std;

struct MovieInfo {
    string title;
    string director;
};

void ReadMovieData(MovieInfo& movie);
void DisplayMovieData(const MovieInfo& movie);

int main() {
    const int SIZE = 2;
    MovieInfo movieList[SIZE];

    for (int i = 0; i < SIZE; i++) {
        ReadMovieData(movieList[i]);
        DisplayMovieData(movieList[i]);
    }
}

// Since movie is an output parameter in this case, pass by non-const reference.
void ReadMovieData(MovieInfo& movie) {
    //get the movie name
    cout << "What is the movie? ";
    cin >> movie.title;

    //get the movie director
    cout << "What is the director of " << movie.title << "?";
    cin >> movie.director;
}

// Since movie is an input parameter in this case, pass by reference to const.
void DisplayMovieData(const MovieInfo& movie) {
    cout << "The movie name is: " << movie.title << endl;
    cout << "The director of " << movie.title 
         << " is: " << movie.director << endl;
}

Your class movieInfo does not have an overloaded << operator, which is necessary is you want to work with iostream , however, you can pass the strings contained in movieInfo :

int DisplayMovieData(string &title, string &director) { }

Call like:

DisplayMovieData(movie.title, movie.director);

You are declaring the function with this signature

 int ReadMovieData(string* title, string* director);

but you're defining it using

 int ReadMovieData(movieInfo &title, movieInfo &director) {
     // ...
 }

These don't match!

The code is totally invalid. I suppose the valid code should look the following way

#include <stdafx.h>
#include <string>
#include <iomanip>
#include <iostream>

using namespace std;

struct movieInfo 
{
    string title, director;
};

//prototypes
movieInfo ReadMovieData();
void DisplayMovieData( const movieInfo & );

int main()
{
    const int SIZE = 2;
    movieInfo movieList[SIZE];

    //supposed to assign data to movieList[i] at some point
    for ( int i = 0; i < SIZE; i++ )
    {
        movieList[i] = ReadMovieData();
        DisplayMovieData( movieList[i] );
    }

    return 0;
}

movieInfo ReadMovieData()
{
    movieInfo movie;

    //get the movie name
    cout << "What is the movie? ";
    cin.ignore();
    cin >> movie.title;

    //get the movie director
    cout << "What is the director of " << movie.title << "?";
    cin.ignore();
    cin >> movie.director;

    return movie;
}

void DisplayMovieData( const movieInfo &movie )
{
    cout << "The movie name is: " << movie.title << endl;
    cout << "The director of " << movie.title << " is: " << movie.director << endl;
}

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