带cout的struct输出和函数调用struct参数

时间:2014-04-14 16:25:02

标签: c++ parameters struct

因此,问题是编译时出现了几个错误。

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. 

底部错误在DisplayMovieData()中发生两次,所以为了简单起见我写了一次。

ReadMovieData函数应接受结构指针引用变量,DisplayMovieData函数应接受MovieInfo结构变量。

main函数创建一个包含2个MovieInfo结构变量的数组,其他函数应该在数组的元素上调用。

我完成的代码如下。

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

5 个答案:

答案 0 :(得分:1)

错误非常明确且清晰 - 您的功能需要string*,但您传递的是movieInfo - 不相关的类型不能仅仅将魔法转换为另一种类型。

您可能想要的是传递movieInfo的数据成员:

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

如果参数不是指针会更好 - 使用引用代替。如果您不更改参数,则引用应为const类型。

更好的是,为什么不通过moveInfo

ReadMovieData(movieInfo& movie);

让函数处理类的内部?这样可以更好地封装数据,并且不会导致意大利面条代码非常快。

此外,声明和定义需要匹配(否则你将重载) - 你在某些地方使用指针而在其他地方使用引用/值。

最后,这里是operator<<的重载可能如下:

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

答案 1 :(得分:1)

您的函数原型与其定义之间存在不匹配,因为您可以看到比较两者中的参数类型。

请注意,由于您为影片信息定义了一个结构,因此您可以直接将其传递给读取和显示函数(而不是传递单个结构数据成员字符串)。

您可能需要阅读以下可编辑代码:

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

答案 2 :(得分:0)

您的班级movieInfo没有重载<<运算符,您需要使用iostream,但是,您可以传递movieInfo中包含的字符串}:

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

请致电:

DisplayMovieData(movie.title, movie.director);

答案 3 :(得分:0)

您正在使用此签名声明该功能

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

但您使用

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

这些不匹配!

答案 4 :(得分:0)

代码完全无效。我认为有效代码应该采用以下方式

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