Get And Set Functions Malfunctioning

Ok so I am supposed to make a class that represents car information however it worked up until I put all my Get and Set functions into the header file now I have 117 errors so if someone could point me in the right direction or tell me what exactly I am doing wrong that would be awesome
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
main.ccp

#include <iostream>
#include "car.h"

using namespace std;

int main()
{

	Car myCar;
	Car myDreamCar("Nissan", "350z", "Jet Black", 42,3,1,3,false,false,false);

	cout << "Make Car1:" << myCar.GetMake() << endl;

	cout << "Car2: " << myDreamCar.GetMakeAndModel();
	return 0;
}

car.cpp

#include "car.h"

Car::Car(string make = "Nissan", string model = "350z", string color = "Cherry Red",
		 unsigned int milage = 1000, unsigned int horsepower = 306, unsigned int numberofdoors = 2, unsigned int numberofcylinders = 6,
		 bool fuelinjection = true, bool transmission = true, bool turbocharged = true)
{
	m_Make = make
	m_Model = model
	m_Color = color
	m_Milage = milage
	m_HorsePower = horsepower
	m_NumberOfDoors = numberofdoors
	m_NumberOfCylinders = numberofcylinders
	m_FuelInjection = fuelinjection
	m_Transmission = transmission
	m_TurboCharged = turbocharged

}

string Car::GetMake();
{
	return m_Make;
}
string Car::SetMake()
{
	m_Make = make;
}

string Car::GetModel();
{
	return m_Model;
}
string Car::SetModel()
{
	m_Model = model;
}

string Car::GetColor();
{
	return m_Color;
}
string Car::SetColor()
{
	m_Color = color;
}

string Car::GetMilage();
{
	return m_Mileage;
}
string Car::SetMilage()
{
	m_Milage = milage;
}

string Car::GetHorsePower();
{
	return m_HorsePower;
}
string Car::SetHorsePower()
{
	m_HorsePower = horsepower;
}

string Car::GetNumberOfDoors();
{
	return m_NumberOfDoors;
}
string Car::SetNumberOfDoors()
{
	m_NumberOfDoors = numberofdoors;
}

string Car::GetNumberOfCylinders();
{
	return m_Cylinders;
}
string Car::SetNumberOfCylinders()
{
	m_NumberofCylinders = numberofcylinders ;
}

string Car::GetFuelInjection();
{
	return m_FuelInjection;
}
string Car::SetFuelInjection()
{
	m_FuelInjection = fuelinjection;
}

string Car::GetTransmission();
{
	return m_Transmission;
}
string Car::SetTransmission()
{
	m_Transmission = mtransmission;
}

string Car::GetTurboCharged();
{
	return m_TurboCharged;
}
string Car::SetTurboCharged()
{
	m_TurboCharged = turbocharged;
}

car.h

#pragma once

#include <string>

using namespace std;

class Car
{
private:

	string m_Make;
	bool m_TurboCharged;
	string m_Model;
	string m_ExteriorColor;
	int m_Milage;
	int m_HorsePower;
	int m_NumberOfDoors;
	int m_NumberOfCylinders;
	bool m_AutomaticTransmission;
	bool m_FuelInjected;
	
public:

		Car(string name = "Nissan",
		string model = "350z",
		string color = "Cherry Red",
		unsigned int milage = 1000,
		unsigned int horsepower = 306,
		unsigned int numberofdoors = 2,
		unsigned int numberofcylinders = 6,
		bool transmission = true
		bool FuelInjection = true
		bool turboCharged = true);

	string GetMake();
	void setMake(string make);
	string GetModel();
	void setModel(string model);
	string GetColor();
	void setColor(string color);
	unsigned int GetMilage();
	void setMilage(int milage):
	unsigned int GetHorsePower();
	void setHorsePower(int horsepower);
	unsigned int GetNumberOfDoors();
	void setNumberOfDoors(int numberofdoors);
	unsigned int GetNumberOfCylinders();
	void setNumberOfCylinders(int numberofcylinders);
	bool GetFuelInjection();
	void setFuelInjection(bool fuelinjection);
	bool GetTransmission();
	void setTransmission(bool transmission);
	bool GetTurboCharged();
	void setTurboCharged(bool turbocharged);


};
*/
Last edited on
Put your code in code tags: http://cplusplus.com/articles/firedraco1/

Then try actually reading the compiler errors. You should be able to figure out all of these for yourself.

Don't just give up once the compiler spits an error at you.

EDIT:
Also..

*cringes at the sight of such horrid get/set abuse*
Last edited on
Well thanks for the help but I mean it just says all my Identifiers don't exist which I don't

understand. As well says my Identifiers are undeclared which again I font understand how to fix.

Then its says I need ; where they shouldn't be well I dont think they should be.

And what am I doing that makes my Get/Set abusive?
Last edited on
The code tags are nice... but why on earth did you comment everything? that takes away the syntax highlighting >_>

Well thanks for the help but I mean it just says all my Identifiers don't exist which I don't understand


Look at the lines giving you the error. Line 31 above is one of them

It's saying "m_Color" isn't declared, right? Think about it... where are you declaring m_Color? (hint: you're not. Look more closely at how you named your vars in your class: m_ExteriorColor). You did this with like 4 or 5 of your variables.

Then its says I need ; where they shouldn't be well I dont think they should be.


This error isn't 100% believable. It could be one of any number of problems. When you get this error (or really, any error), look over the erroring line(s) of code and look for anything that's wrong. Don't just look for a missing semicolon.

Take a close look at some of the lines that are giving you this error.

Specifically lines 31, 32, 33, etc. See any missing semicolons there? I sure do.


Line 164 probably gives you a similar error. There, though, it's not a missing semicolon that's a problem, but a missing comma.

----------

some other things:

1) You shouldn't have semicolons before the opening brace for function definitions. See lines 114, 123, etc
2) Make sure your class definitions (in the .cpp file) match the prototype (in the .h file). Notice that all of your Set() functions should return void according to the .h, but you have them returning string in the .cpp
3) Similar problem with your Get functions. They're all returning strings, when some should be returning bool/int/etc
4) For default parameters.... if you have them in the function prototype, don't put them in the function definition. IE: your construtor in the .cpp file should not have the default parameters listed.

And what am I doing that makes my Get/Set abusive?


This isn't "wrong" per se. It's just a style thing.

What's the point of writing a dozen functions to set/get individual member variables? Why not just make the member variables public and access them directly? Saves you tons of time and makes your code way smaller.

There's nothing wrong with public data members in some cases. IMO this is one of those cases.
Last edited on
An upside(good) thing about get/set and particular set functions is that you can do input validation -
rather than have public variables where the values could be set to any old possible incorrect value directly.
Oh, there's definately upsides, don't get me wrong.

I'm not saying you should always make such members public. I'm just saying you shouldn't always make them private. One extreme is just as bad as the other.
Gets and sets only need to be used for data validation and data hiding. Typically if only a few of your variables will be accessed outside the class then it's fine to have them as public but for the most part sets should be private anyway and only the class should be able to change them.
So think if you need them ALL to be public or not. Most scenarios constructors can be used to set them as private setters. if you need to change the data via the setter outside the class then public is ok, but it makes the accessor/mutator kinda pointless.(edit: unless of course your doing validation as guest* suggested)
Last edited on
Topic archived. No new replies allowed.