Error C2365

Goal:
Use a struct that has 4 fields, input to those fields, and pass to function to display.

Problem:
(38) : error C2365: 'displaySData' : redefinition; previous definition was 'data variable'


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
39
40
41
42
43
#include <iostream>
#include <string>
using namespace std;

//prototypes
void displaySData(MovieData, MovieData);

struct MovieData
{
	string Title;
	string Director;
	string YearReleased;
	string RunTime;
};

int main()
{
	MovieData data1;
	MovieData data2;

	// Get input for struct var 1
	cout<<"Enter the title of the movie:\n";
	cin>>data1.Title;
	cout<<"Enter the director of the movie:\n";
	cin>>data1.Director;
	// Get input for struct var 2
	cout<<"Enter the release date (year) of the movie:\n";
	cin>>data2.YearReleased;
	cout<<"Enter the run time (minutes) of the movie:\n";
	cin>>data2.RunTime;
	// Function for display
	displaySData(data1, data2);
	system("Pause");
	return 0;
}

void displaySData(MovieData d1, MovieData d2)
{
	cout<<d1.Title<<"\n";
	cout<<d1.Director<<"\n";
	cout<<d2.YearReleased<<"\n";
	cout<<d2.RunTime<<"\n";
}


C++ Masters.. Please help me..
The compiler didn't know what to do with the prototype because MovieData is defined after the prototype. Switch the order it will compile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

struct MovieData
{
	string Title;
	string Director;
	string YearReleased;
	string RunTime;
};

//prototypes
void displaySData(MovieData, MovieData);
Last edited on
Hope this helps.

I modified your code a little bit. Instead of cin, I am using getline(). That way it reads an entire line and not just the first string. Also I am not sure why you using data2 to store year and length, so I removed it completely from the code and swapped the struct with function header. It compiles and runs as expected.

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
39
40
41
42
43
44
#include <iostream>
#include <string>
using namespace std;

//prototypes


struct MovieData
{
	string Title;
	string Director;
	string YearReleased;
	string RunTime;
};

void displaySData(MovieData);

int main()
{
	MovieData data1;
	

	// Get input for struct var 1
	cout << "Enter the title of the movie:\n";
	getline(cin, data1.Title);
	cout << "Enter the director of the movie:\n";
	getline(cin, data1.Director);
	cout << "Enter the release date (year) of the movie:\n";
	getline(cin, data1.YearReleased);
	cout << "Enter the run time (minutes) of the movie:\n";
	getline(cin, data1.RunTime);
	// Function for display
	displaySData(data1);
	system("Pause");
	return 0;
}

void displaySData(MovieData d1)
{
	cout << d1.Title << "\n";
	cout << d1.Director << "\n";
	cout << d1.YearReleased << "\n";
	cout << d1.RunTime << "\n";
}
@ mobotus
Why couldn't it just tell me that! LOL

@ art1986
Yeah, my instructions are to use 2 variables. Also, what is with the getline()? My book would basically tell me to use after each input (after the first):

cin.ignore();
getline(cin, data1.Director);

I literally started learning this chapter 3 hours ago so forgive my ignorance
Last edited on
Lets say the movie title is Harry Potter. When you first cin, it will assign only Harry because it reads until it encounters whitespace, Potter will remain in buffer until next cin. getline() on the other hand will read an entire line.

cin.ignore() will clear out your cin buffer of any left over user input.

cin.ignore(1000, '\n') - will clear out 1000 characters or until it reaches whitespace.

Hope this helps.

Topic archived. No new replies allowed.