class/struct/union

I am writing a program, probably the longest one I have had to do so far, and i'm just trying to get the first part running and I am having trouble, I want to take care of this before continuing with the rest since it is all very similar, I don't want to keep makign the same error. here is what i have.

struct definitions in one header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
using namespace std;

struct Customer {
	int CustId;
	string CustName;
};

struct Movie {
	int MovieId;
	string MovieName;
};

struct Rental {
	int CustId;
	int MovieId;
};

struct CustomerData {
	int count;
	Customer Customers[20];
};


function definitions (prototypes are in functions.h)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "movies.h"
#include "functions.h"

Customer CreateCustomer (int& custid, string& custname)
{
	Customer cust1;
	cust1.CustId = custid;
	cust1.CustName = custname;
	return cust1;
}

void AddCustomer (CustomerData& customer, Customer& customer1)
{
	customer.Customers[customer.count]=customer1;
	customer.count++;
}


and my main cpp:
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
#include "stdafx.h"
#include "movies.h"
#include "functions.h"
#include <fstream>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int id;
	string name;
	ofstream p;
	p.open("output.txt");
	CustomerData cust;
	cust.count=0;
	cout<<"Please enter a Customer ID: ";
	cin>>id;
	cout<<endl<<"Please enter a Customer Name: ";
	cin>>name;
	Customer cust1;
	cust1.CustId =id;
	cust1.CustName = name;
	AddCustomer(cust, cust1);

	cout<<cust.count;

	return 0;
}

i get: error C2228: left of '.count' must have class/struct/union type is 'int'

i get a few of those errors, so i am thinking that it is not able to see the structs even though i link the header file in the cpp. any help is greatly appreciated.
i do know i assigned cust1.CustId and CustName in both main and in the create customer function, i was trying out different things and neither one worked.
closed account (S6k9GNh0)
I can't tell where you're getting the errors exactly. Would you mind showing the relevant lines provided by the error message?
1
2

	customer.Customers[customer.count]=customer1;


Have you defined an overloaded operator for '='?
here is the entire error output:

1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(1): warning C4627: '#include "movies.h"': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(2): warning C4627: '#include "functions.h"': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(17): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source?
1> Assignment 2.cpp
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(4): error C2011: 'Customer' : 'struct' type redefinition
1> c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(4) : see declaration of 'Customer'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(9): error C2011: 'Movie' : 'struct' type redefinition
1> c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(9) : see declaration of 'Movie'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(14): error C2011: 'Rental' : 'struct' type redefinition
1> c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(14) : see declaration of 'Rental'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(19): error C2011: 'CustomerData' : 'struct' type redefinition
1> c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(19) : see declaration of 'CustomerData'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(24): error C2011: 'MovieData' : 'struct' type redefinition
1> c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(24) : see declaration of 'MovieData'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(29): error C2011: 'RentalData' : 'struct' type redefinition
1> c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(29) : see declaration of 'RentalData'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\assignment 2.cpp(17): error C2079: 'cust' uses undefined struct 'CustomerData'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\assignment 2.cpp(18): error C2228: left of '.count' must have class/struct/union
1> type is 'int'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\assignment 2.cpp(56): error C2079: 'cust1' uses undefined struct 'Customer'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\assignment 2.cpp(57): error C2228: left of '.CustId' must have class/struct/union
1> type is 'int'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\assignment 2.cpp(58): error C2228: left of '.CustName' must have class/struct/union
1> type is 'int'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\assignment 2.cpp(59): error C2664: 'AddCustomer' : cannot convert parameter 1 from 'int' to 'CustomerData'
1> Source or target has incomplete type
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\assignment 2.cpp(61): error C2228: left of '.count' must have class/struct/union
1> type is 'int'


and i don't believe that i defined an overloaded operator for '='?, tbh, not 100% sure what that means. i REALLY appreciate the help and look forward to paying it forward when i am able to. i am a beginner but really enjoy coding and i see it being a profession i would enjoy.
From looking at the original post, I can see various problems right off the bat:

1) Using directives in a header file are a bad idea. Not verboten, per se, but not recommended.
2) You have no dynamically allocated customers. This is fine, really, except that you'll only have 1 customer...ever.
3) You're not using include guards, so I can only assume that your header files are being included twice. This could cause the compiler to think that you're redefining your structures at compile time.
Ok, put fstream, iostream and string in your stdafx header, toward the bottom. That will clear up the stdafx problems.
Last edited on
I am not supposed to edit the stdafx header, if i have #include <string> only in the main cpp, then i can't use strings in my struct header file. i am convinced now my problem is with all these includes and headers but i am required to split up all the structs in one header, function prototypes in another header, fucntion definitions in a cpp file and then main in another cpp file.
Include guards:

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
#ifndef __STRUCT_H__
#define __STRUCT_H__

#include <string>
using namespace std;

struct Customer {
	int CustId;
	string CustName;
};

struct Movie {
	int MovieId;
	string MovieName;
};

struct Rental {
	int CustId;
	int MovieId;
};

struct CustomerData {
	int count;
	Customer Customers[20];
};

#endif 
thanks for the help, but haven't learned guards yet, so i shouldn't be using them
In your case, I would start with an empty project and turn off precompiled header usage.
and how would i solve this:
2) You have no dynamically allocated customers. This is fine, really, except that you'll only have 1 customer...ever.
i have to initialize the array of customers? i thought that happened when i created an instance of the struct.
we are told to use precompiled headers.
closed account (S6k9GNh0)
You can't use header guards but you're required to use precompiled-headers. You must have had the same professor I did.

Anyone care to point some issues there?

@ OP: Do this. In the main file, remove the include for #include "movies.h" . Because you don't have a header guard, it's being included twice because it's also included in the function prototypes. Generally, this would be fine and *even considered good practice to explicitly include the header defining types* but whatever. This should fix issues with redefinitions. After that, your structs should be properly defined. Give us the error list after that, if any.
Last edited on
Just for giggles:
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
#ifndef __FLUFFY_PINK_BUNNIES__
#define __FLUFFY_PINK_BUNNIES__

#include <string>
using namespace std;

struct Customer {
	int CustId;
	string CustName;
};

struct Movie {
	int MovieId;
	string MovieName;
};

struct Rental {
	int CustId;
	int MovieId;
};

struct CustomerData {
	int count;
	Customer Customers[20];
};

#endif 
have some new problems now, i'm sure it is coming from a similar issue with me including something that i shouldnt' be. i also have been adding to the project while trying to debug. here is everything i have now.
function headers:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "movies.h"

void AddCustomer (CustomerData& customerdata, Customer& customer);

Customer CreateCustomer (int custid, string custname);

void AddMovie (MovieData& moviedata, Movie& movie);

void AddRental (RentalData& rentaldata, Rental& rental);

void WriteCustomerData (ofstream& q, const CustomerData custdata);

void WriteMovieData (ofstream& q, const MovieData moviedata);

void WriteRentalData (ofstream&q, const RentalData rentaldata);

Rental CreateRental (int custid, int movieid);

Movie CreateMovie (int movieid, string movie);


structs header:
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
struct Customer {
	int CustId;
	string CustName;
};

struct Movie {
	int MovieId;
	string MovieName;
};

struct Rental {
	int CustId;
	int MovieId;
};

struct CustomerData {
	int count;
	Customer Customers[20];
};

struct MovieData {
	int count;
	Movie Movies[20];
};

struct RentalData {
	int count;
	Rental Rentals[20];
};


fucntions source:
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
#include "movies.h"
#include "functions.h"
#include "stdafx.h"

Customer CreateCustomer (int& custid, string& custname)
{
	Customer cust1;
	cust1.CustId = custid;
	cust1.CustName = custname;
	return cust1;
}

Rental CreateRental (int custid, int movieid)
{
	Rental rental;
	return rental;
}

Movie CreateMovie (int movieid, string movie)

void AddCustomer (CustomerData& customer, Customer& customer1)
{
	customer.Customers[customer.count]=customer1;
	customer.count++;
}

void AddMovie (MovieData& moviedata, Movie& movie)
{
	moviedata.Movies[moviedata.count] = movie;
	moviedata.count++;

void AddRental (RentalData& rentaldata, Rental& rental)
{
}

void WriteCustomerData (ofstream& q, const CustomerData custdata)

void WriteMovieData (ofstream& q, const MovieData moviedata)

void WriteRentalData (ofstream&q, const RentalData rentaldata)


and main source:
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "stdafx.h"
#include "functions.h"
#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int id, n;
	string name;
	ofstream p;
	p.open("output.txt");
	CustomerData cust;
	cust.count=0;

	do {
		cout<<"Movie Rental Program"<<endl;
		cout<<"1. Add customer"<<endl;
		cout<<"2. Add movie"<<endl;
		cout<<"3. Add rental"<<endl;
		cout<<"4. Write customer data"<<endl;
		cout<<"5. Write movie data"<<endl;
		cout<<"6. Write rental data"<<endl;
		cout<<"7. Exit"<<endl;
		
		cin>>n;
		switch (n) {
		case 1:
			{
			cout<<"Please enter a Customer ID: ";
			cin>>id;
			cout<<endl<<"Please enter a Customer Name: ";
			cin>>name;
			Customer cust1;
			cust1.CustId =id;
			cust1.CustName = name;
			AddCustomer(cust, cust1);
			break;
			}
		case 2:
			break;
		case 3:
			break;
		case 4:
			break;
		case 5:
			break;
		case 6:
			break;
		case 7:
			break;
		default:
			cout<<"Invalid entry, try again."<<endl;
			break;
		}
	} while (n != 7);



	cout<<cust.count;



	return 0;
}
and the errors:
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(1): warning C4627: '#include "movies.h"': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(2): warning C4627: '#include "functions.h"': skipped when looking for precompiled header use
1> Add directive to 'StdAfx.h' or rebuild precompiled header
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(5): error C2146: syntax error : missing ';' before identifier 'CreateCustomer'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(5): error C2061: syntax error : identifier 'string'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(7): error C2146: syntax error : missing ';' before identifier 'cust1'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(7): error C2065: 'cust1' : undeclared identifier
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(8): error C2065: 'cust1' : undeclared identifier
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(8): error C2228: left of '.CustId' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(9): error C2065: 'cust1' : undeclared identifier
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(9): error C2228: left of '.CustName' must have class/struct/union
1> type is ''unknown-type''
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(9): error C2065: 'custname' : undeclared identifier
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(10): error C2065: 'cust1' : undeclared identifier
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(13): error C2146: syntax error : missing ';' before identifier 'CreateRental'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(14): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.cpp(42): fatal error C1004: unexpected end-of-file found
1> Assignment 2.cpp
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(7): error C2146: syntax error : missing ';' before identifier 'CustName'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(12): error C2146: syntax error : missing ';' before identifier 'MovieName'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.h(6): error C2061: syntax error : identifier 'string'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.h(12): error C2065: 'ofstream' : undeclared identifier
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.h(12): error C2065: 'q' : undeclared identifier
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.h(12): error C2059: syntax error : 'const'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.h(14): error C2065: 'ofstream' : undeclared identifier
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.h(14): error C2065: 'q' : undeclared identifier
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.h(14): error C2059: syntax error : 'const'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.h(16): error C2065: 'ofstream' : undeclared identifier
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.h(16): error C2065: 'q' : undeclared identifier
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.h(16): error C2059: syntax error : 'const'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\functions.h(20): error C2061: syntax error : identifier 'string'
1>c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\assignment 2.cpp(40): error C2039: 'CustName' : is not a member of 'Customer'
1> c:\users\josh\documents\visual studio 2010\projects\assignment 2\assignment 2\movies.h(5) : see declaration of 'Customer'
Just as a general rule, I would advise against adding new functions that invariably will have the same erros until you've actually resolved the errors that you already have.
closed account (S6k9GNh0)
Well it has something to do with the precompiled header. Since I think precompiled headers are slightly retarded and I've used it only once with GCC (which was not even fucking CLOSE to worth the amount of trouble I put into it), I have no idea what the issue is.

we are told to use precompiled headers.


You're required to use a PC header, but you're not allowed to edit it? That's getting terrible.
Last edited on
Topic archived. No new replies allowed.