program stops working

i decided to practice classes,inheritance,keyword this and friendship and made myself a project "Synth constructor" @inspired_by_fallout the project's mission is to give an option to create a detailed description of the synth's body parts and than view the description as a whole.
The code i made seems to have no syntax errors and compiles with 0 errors but the problem is the second i run it it throws out a "program stopped working" and i don't think i have the skill to understand why yet.

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
  #include<iostream>

class Head {
protected:
	std::string Hair_Info;
	std::string Nose_Info;
	std::string Ears_Info;
	std::string Mouth_Info;
	std::string Eyes_Info;
	int Head_Fat_Info;
	int Head_Length_Info;
public:
	void Construct_Hair(std::string Hair_Info) { this->Hair_Info = Hair_Info; }
	void Construct_Nose(std::string Nose_Info) { this->Nose_Info = Nose_Info; }
	void Construct_Ears(std::string Ears_Info) { this->Ears_Info = Ears_Info; }
	void Construct_Mouth(std::string Mouth_Info) { this->Mouth_Info = Mouth_Info; }
	void Construct_Eyes(std::string Eyes_Info) { this->Eyes_Info = Eyes_Info; }
	void Construct_Head_Fat(int Head_Fat_Info) { this->Head_Fat_Info = Head_Fat_Info; }
	void Construct_Head_Length(int Head_Length_Info) { this->Head_Length_Info = Head_Length_Info; }
	friend std::ostream& operator<<(std::ostream& Out, std::string Data);
	void Head_Info() {
		std::cout << "Synth model head information:\n";
		std::cout << "Hair_Info: " << Hair_Info << "\n";
		std::cout << "Nose_Info: " << Nose_Info << "\n";
		std::cout << "Ears_Info: " << Ears_Info << "\n";
		std::cout << "Mouth_Info: " << Mouth_Info << "\n";
		std::cout << "Eyes_Info: " << Eyes_Info << "\n";
		std::cout << "Fat_Info: " << Head_Fat_Info << "\n";
		std::cout << "Length_Info: " << Head_Length_Info << "\n";
	}
};

class Torso {
protected:
	int Torso_Muscle_Info;
	int Torso_Fat_Info;
	int Torso_Length_Info;
public:
	void Construct_Torso_Muscle(int Torso_Muscle_Info) { this->Torso_Muscle_Info = Torso_Muscle_Info; }
	void Construct_Torso_Fat(int Torso_Fat_Info) { this->Torso_Fat_Info = Torso_Fat_Info; }
	void Construct_Torso_Length(int Torso_Length_Info) { this->Torso_Length_Info = Torso_Length_Info; }
	friend std::ostream& operator<<(std::ostream& Out, std::string Data);
	void Torso_Info() {
		std::cout << "Synth model torso information:\n";
		std::cout << "Muscle_Info: " << Torso_Muscle_Info << "\n";
		std::cout << "Fat_Info: " << Torso_Fat_Info << "\n";
		std::cout << "Length_Info: " << Torso_Length_Info << "\n";
	}
};

class Arms {
protected:
	std::string Hands_Info;
	int Arm_Muscle_Info;
	int Arm_Fat_Info;
	int Arm_Length_Info;
public:
	void Construct_Hands(std::string Hands_Info) { this->Hands_Info = Hands_Info; }
	void Construct_Arms_Muscle(int Arm_Muscle_Info) { this->Arm_Muscle_Info = Arm_Muscle_Info; }
	void Construct_Arms_Fat(int Arm_Fat_Info) { this->Arm_Fat_Info = Arm_Fat_Info; }
	void Construct_Arms_Length(int Arm_Length_Info) { this->Arm_Length_Info = Arm_Length_Info; }
	friend std::ostream& operator<<(std::ostream& Out, std::string Data);
	void Arms_Info() {
		std::cout << "Synth model arms information:\n";
		std::cout << "Hands_Info: " << Hands_Info << "\n";
		std::cout << "Muscle_Info: " << Arm_Muscle_Info << "\n";
		std::cout << "Fat_Info: " << Arm_Fat_Info << "\n";
		std::cout << "Length_Info: " << Arm_Length_Info << "\n";
	}
};

class Legs {
protected:
	std::string Feet_Info;
	int Legs_Muscle_Info;
	int Legs_Fat_Info;
	int Legs_Length_Info;
public:
	void Construct_Feet(std::string Feet_Info) { this->Feet_Info = Feet_Info; }
	void Construct_Legs_Muscle(int Legs_Muscle_Info) { this->Legs_Muscle_Info = Legs_Muscle_Info; }
	void Construct_Legs_Fat(int Legs_Fat_Info) { this->Legs_Fat_Info = Legs_Fat_Info; }
	void Construct_Legs_Length(int Legs_Length_Info) { this->Legs_Length_Info = Legs_Length_Info; }
	friend std::ostream& operator<<(std::ostream& Out, std::string Data);
	void Legs_Info() {
		std::cout << "Synth model legs information:\n";
		std::cout << "Feet_Info: " << Feet_Info << "\n";
		std::cout << "Muscle_Info: " << Legs_Muscle_Info << "\n";
		std::cout << "Fat_Info: " << Legs_Fat_Info << "\n";
		std::cout << "Length_Info: " << Legs_Length_Info << "\n";
	}
};

class Personality {
protected:
	std::string Disorders_Info;
	std::string Feelings_Info;
public:
	void Construct_Disorders(std::string Disorder_Info) { this->Disorders_Info = Disorder_Info; }
	void Construct_Feelings(std::string Feelings_Info) { this->Feelings_Info = Feelings_Info; }
	friend std::ostream& operator<<(std::ostream& Out, std::string Data);
	void Personality_Info() {
		std::cout << "Synth model personality information:\n";
		std::cout << "Disorders_Info: " << Disorders_Info << "\n";
		std::cout << "Feelings_Info: " << Feelings_Info << "\n";
	}
};

class Synth_Model : public Head, public Torso, public Arms, public Legs, public Personality {
private:
	int Overall_Length;
	std::string Race_Info;
	std::string Skin_Info;
public:
	void Construct_Race(std::string Race_Info) { this->Race_Info = Race_Info; }
	void Construct_Skin(std::string Skin_Info) { this->Skin_Info = Skin_Info; }
	friend std::ostream& operator<<(std::ostream& Out, std::string Data);
	void Synth_Model_Informatio() {
		Head_Info();
		std::cout << "\n";
		Torso_Info();
		std::cout << "\n";
		Arms_Info();
		std::cout << "\n";
		Legs_Info();
		std::cout << "\n";
		Personality_Info();
		std::cout << "\n";
		std::cout << "Race_Info: " << Race_Info << "\n";
		std::cout << "Skin_info: " << Skin_Info << "\n";
		Overall_Length = Head_Length_Info + Torso_Length_Info + Legs_Length_Info;
		std::cout << "Overala_Length: " << Overall_Length << " cm\n";
	}
};

std::ostream& operator<<(std::ostream& Out, std::string Data) {
	Out << Data;
	return (Out);
}

int main() {
	Synth_Model Model_A;

	//Head constructor
	Model_A.Construct_Hair("Black");
	Model_A.Construct_Nose("Small");
	Model_A.Construct_Ears("Small");
	Model_A.Construct_Mouth("Small");
	Model_A.Construct_Eyes("Asian Eyes");
	Model_A.Construct_Head_Fat(3); // Fat in percentages
	Model_A.Construct_Head_Length(20); // Length in centimeter

        //Torso constructor
	Model_A.Construct_Torso_Muscle(97); // Muscle in percentages
	Model_A.Construct_Torso_Fat(3); // Fat in percentages
	Model_A.Construct_Torso_Length(40); // Length in centimeters

	//Arms constructor
	Model_A.Construct_Hands("Medium");
	Model_A.Construct_Arms_Muscle(95); // Muscle in percentages
	Model_A.Construct_Arms_Fat(5); // Fat in percentages
	Model_A.Construct_Arms_Length(65); // Each arm, length in centimeters

	//Legs constructor
	Model_A.Construct_Feet("Medium");
	Model_A.Construct_Legs_Muscle(90); // Muscle in percentages
	Model_A.Construct_Legs_Fat(10); //Fat in percentages
	Model_A.Construct_Legs_Length(80); // Length in centimeters

	//Personality constructor
	Model_A.Construct_Disorders("Acute stress disorder");
	Model_A.Construct_Feelings("Mostly peaceful");

	//Synth_Model constructor
	Model_A.Construct_Race("Asian");
	Model_A.Construct_Skin("Tan");
	Model_A.Synth_Model_Informatio();
	return 0;
}


detailed problem:
"ConsoleApplication1.exe has stopped working"
"A problem caused the program to stop working correctly.
Windows will close the program and notify you if a solution is available."

and it gives two opetions: "Debug" does nothing and "Close program" that does as said..

IDE:visual studio 2015 community edition.
Last edited on
+ If anyone has a recommendation on what i should avoid ,which style to stick or how to make my code cleaner in the future it will be more than welcome.
closed account (48T7M4Gy)
It runs on the shell here OK. Have you tried a reboot? Might be a quick fix.
nope still doesn't work
Have you tried stepping through it in, a debugger to find out exactly where it crashes, and to examine the state of the memory at that point? I strongly recommend you do.

I know this is only an exercise for you to experiment with inheritance, but, from the names of the classes, it seems as though you've misunderstood what inheritance used for. At its most basic, it should be used to model "is-a" relationships; that is a relationship where the derived class is a specialised type of the base class.

For example, a dog is an animal, a cat is an animal, and a bird is an animal, so if you were creating classes for these, you might want to create an Animal base class, for all the behaviour and properties that are common to all animals. Then your Dog, Cat and Bird classes would inherit from Animal.

That isn't true of any of the base classes in your example. It would be wrong to say a synth is a head, or that a synth is a torso, or whatever. A synth has a head. A synth has a torso. This is composition, not inheritance.

Consider the following: what if you decided to make a synth with 2 heads? How would your design cope with that?
closed account (48T7M4Gy)
Memory was the next step in the fault finding and an additional aspect to debugging above is to comment out the classes and progressively reintroduce them to find the spot where memory or whatever is causing trouble. There might be a spot (surely) where it doesn't crash or was this all typed up in one slab of code?

A couple of aspects of the code catch my intuitive eye.

The friends syntax can be a notorious causes of problems, the use of 'this' in the constructors or whatever they are and elsewhere is interesting. construct_whatever(etc) returning void seems strange, are they meant to be constructors, where are the destructors?

Why this didn't show in running it in the shell is anyone's guess.
@kemort Those "constructors or whatever they are" are just oddly-named setter methods. I don't see anything troublesome about the use of this in them - it's just being used to distinguish between the object's data members, and method arguments of the same name.

Personally, I would make sure I didn't use the same names for my method args and my data members, but that's just stylistic issue.

Could you elaborate on why you think the use of this might be causing problems?
globaltourist,

While playing with your original code, I tried commenting out all the friend forward declarations and the code at 135 - 138. once I did that the code compiled and ran with no problem.

Unless you are trying to overload the << operator and even then I see no use for it unless I have missed something. The program works with out it.

Andy
@MikeyBoy: if i would name synth as a "Body_Part" than would it be right to say a Body_Part is a head or a Body_Part is a Torso ?
+ i wanted to test how the program behaves if i make multiple base class with a derived class Synth_Model that has access to all of them so that in main i would need only one type (Synth_Model) to access all of the base members.
About the two heads i didn't mean to make the program that any user can access i wanted to create a test exercise in which i manually set everything and see if it works so i wont set 2 heads and it wouldn't be a problem.
+Question:can the problem be my IDE(microsoft visual studio) ? since it wont let me print std::string values i decided to overload operator<< and it fix the problem of "no operator << matches something something":
 
friend std::ostream& operator<<(std::ostream& Out, std::string Data);


and than:
1
2
3
4
std::ostream& operator<<(std::ostream& Out, std::string Data) {
	Out << Data;
	return (Out);
}


+On the topic of keyword "this" i mentioned at the top that i wanted to exercise this keyword it is not my style and personally i would avoid it as well.
Last edited on
@kemort as MikeyBoy said these are oddly-named setters and not constructors that is why there aren't any destructors .
+if you can run this program perfectly and i cant, can it be that my IDE (microsoft visual studio) isn't good ?
@Handy Andy: the use for overloading << is that without it i cant print out std::string values as it gives me "no operator << matches something something" error and overloading << seems to fix the error popup so i cant comment out the friend declarations as it wont let me go past compilation.
UPDATE:i downloaded code::blocks and ran the same code with no changes and it runs perfectly i think that microsoft visual studio just doesn't work properly.
+shell runs the code perfectly for me aswell
I don't want to belabour the point about design. I appreciate that this is an exercise for you to get familiar with the technique of inheritance in C++, not production code. But since you asked...

if i would name synth as a "Body_Part" than would it be right to say a Body_Part is a head or a Body_Part is a Torso ?

No. A head is a type of body part. A torso is a type of body part. Both of those classes would inherit from Body_Part - which is the opposite way around from the way you have Synth inheriting from all those classes.

i wanted to test how the program behaves if i make multiple base class with a derived class Synth_Model that has access to all of them so that in main i would need only one type (Synth_Model) to access all of the base members.

Yes, but in doing so, you're making the Synth object behave as if it were Torso, and a Head, and Legs, etc. You're giving that class the same interfaces of all the classes it derives from, when that probably isn't appropriate. Your Synth class should have the interface appropriate for the way calling code would interact with a Synth object - which may well not include the complete interfaces for all the components.

About the two heads i didn't mean to make the program that any user can access i wanted to create a test exercise in which i manually set everything and see if it works so i wont set 2 heads and it wouldn't be a problem.

Sure. I understand that. The purpose of my question was to illustrate just one reason why your design probably isn't appropriate for what it is you're trying to model.

if you can run this program perfectly and i cant, can it be that my IDE (microsoft visual studio) isn't good ?

If your code has undefined behaviour, then the fact that the MSVS build crashes makes it better, not worse. You want your bugs to be nice and obvious.
Last edited on
@OP: If you want to use std::string, then #include <string>


> and it fix the problem of "no operator << matches something something":
If you've got a compiler error, then post it.

1
2
3
4
std::ostream& operator<<(std::ostream& Out, std::string Data) {
	Out << Data;
	return (Out);
}
that's infinite recursion (stack overflow), ¿what function do you think `Out<<Data;' will call?

This would be fixed by #include <string> which provides the prototype and implementation to print strings to the screen.


As for why it works with GCC. `<string>' gets included by `<iostream>' (this is not required behaviour and you should not count on it). Then it uses std::ostream& operator<<(std::ostream& Out, const std::string &Data); instead of yours.
Last edited on
@ne555 thanks for the tip! i removed all of the friend calls and the friend function and added #include<string> and now it works perfectly in microsoft visual studio.
@MikeyBoy thanks for the explanation! i will use your tips further on and have a question do you have any source or tutorial by chance that will illustrate how to properly use inheritance?
(i know the basics but this sites tutorial doesn't seem to explain inheritance that in depth).
globaltourist

Check out http://www.learncpp.com/ chapter 8 and 9. I have found it to be very useful and easy to understand. I do not remember at the moment which part of chapter 8 covered inheritance

Ne55 found the part I missed, #include <string> . When I setup my main file I include one header file that includes <iostream> and <string> along with a couple of others. So, that’s why your program ran for me in Visual Studio 2015 and not for you. Since my file had the #include <string> everything worked, but the code for the operator ]code]>>[/code] overload was the only problem I had.

Something to think about, hope that helps.

Andy
Topic archived. No new replies allowed.