Passing structure as argument help
Apr 23, 2012 at 12:54am UTC
I'm extremely new to structures and have a book that has this problem in it. I'm simply trying to pass the structure to a function to output what was assigned to the structure's two variables, movie1 and movie2. Any explanation on how to fix what I'm doing wrong here would be much appreciated, thanks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
void displayMovie(MovieData m)
{
cout << m.movieTitle << endl;
cout << m.director << endl;
cout << m.releaseDate << endl;
cout << m.movieRuntime << endl;
}
struct MovieData
{
string movieTitle;
string director;
int releaseDate;
string movieRuntime;
};
int main()
{
MovieData movie1;
MovieData movie2;
movie1.movieTitle = "test" ;
movie1.director = "test2" ;
movie1.releaseDate = 2000;
movie1.movieRuntime = "120 minutes" ;
movie2.movieTitle = "TEST" ;
movie2.director = "TEST2" ;
movie2.releaseDate = 2000;
movie2.movieRuntime = "118 minutes" ;
displayMovie(movie1);
displayMovie(movie2);
return 0;
}
Last edited on Apr 23, 2012 at 1:25am UTC
Apr 23, 2012 at 12:56am UTC
Please use [co de][/code] tags around your code. And what is the issue you are having here?
Apr 23, 2012 at 1:08am UTC
Well, the compiler does not say I have any syntax errors, but when I run it I get this error message, "term does not evaluate to a function taking 1 arguments"
Apr 23, 2012 at 1:15am UTC
But all this toy program is supposed to do is pass the structure to the function and display what was entered in main()
Apr 23, 2012 at 1:28am UTC
Try putting a forward declaration (basically a prototype for your structure) before the function definition. I can't think of any other issues.
Apr 23, 2012 at 1:45am UTC
And now I am oh so gratefully receiving errors under "<<" in my function. Again, I'm brand spanking new to structures, let alone programming in general
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
void displayMovie(MovieData m);
struct MovieData
{
string movieTitle;
string director;
int releaseDate;
string movieRuntime;
};
void displayMovie(MovieData m)
{
cout << m.movieTitle << endl;
cout << m.director << endl;
cout << m.releaseDate << endl;
cout << m.movieRuntime << endl;
}
int main()
{
MovieData movie1;
MovieData movie2;
movie1.movieTitle = "test" ;
movie1.director = "test2" ;
movie1.releaseDate = 2000;
movie1.movieRuntime = "120 minutes" ;
movie2.movieTitle = "TEST" ;
movie2.director = "TEST2" ;
movie2.releaseDate = 2000;
movie2.movieRuntime = "118 minutes" ;
displayMovie(movie1);
displayMovie(movie2);
return 0;
}
Topic archived. No new replies allowed.