Error on compiling functions from header and cpp files

I was a given a question which includes a Dress class, a Shoes class and an Outfit Class. I've written all the mentioned classes above but I cant compile them in the main file after I prompt the user for the details. Can anyone see where I went wrong?

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

class Dress
{
	private:
		string material;
		int size;
		string style;
		double price;
	public:
		Dress(string = "", int = 0, string = "", double = 0.0);
		void displayDress();
		double getPrice();
};


dress 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
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include "dress.h"
using namespace std;

Dress::Dress(string md, int szd, string std, double pd)
{
	material = md;
	size = szd;
	style = std;
	price = pd;
}

double Dress::getPrice() 
{ 
	if (material == "silk")
	{
		price = price + 20;
	}
	if (style == "evening")
	{
		price = price + 40;
	}

	return price;
}

void Dress::displayDress()
{
	Dress::getPrice();
	cout << "Material: " << material << endl;
	cout << "Style: " << style << endl;
	cout << "Size: " << size << endl;
	cout << "Price: " << price << endl;
}


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

class Shoes
{
	private:
		string material;
		int size;
		string style;
		double price;
	public:
		Shoes(string = "", int = 0, string = "", double = 0.0);
		void displayShoes();
		double getPrice();
};


shoes 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
29
30
31
32
33
34
35
#include <iostream>
#include <string>
#include "shoes.h"
using namespace std;

Shoes::Shoes(string ms, int szs, string sts, double ps)
{
	material = ms;
	size = szs;
	style = sts;
	price = ps;
}

double Shoes::getPrice()
{
	if (material == "suede")
	{
		price = price + 20;
	}
	if (style == "formal")
	{
		price = price + 40;
	}

	return price;
}

void Shoes::displayShoes()
{
	Shoes::getPrice();
	cout << "Material: " << material << endl;
	cout << "Style: " << style << endl;
	cout << "Size: " << size << endl;
	cout << "Price: " << price << endl;
}


outfit header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#pragma once
#include <iostream>
#include <string>
#include "dress.h"
#include "shoes.h"
using namespace std;

class Outfit :public Dress, public Shoes
{
	private:
		Dress dress;
		Shoes shoes;
		double price;
	public:
		Outfit(string, int, string, double, string, int, string, double);
		void displayOutfit();
};


outfit cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include "dress.h"
#include "shoes.h"
#include "outfit.h"
using namespace std;

Outfit::Outfit(string md, int szd, string std, double pd, string ms, int szs, string sts, double ps) : Dress(md, szd, std, pd), Shoes(ms, szs, sts, ps)
{
	price = 0;
}

void Outfit::displayOutfit()
{
	Dress::displayDress();
	Shoes::displayShoes();
}


test 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
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
#include <iostream>
#include <string>
#include "dress.h"
#include "shoes.h"
#include "outfit.h"
using namespace std;

void displayOutfit();
int main()
{
	//Outfit o("silk", 10, "evening", 40, "suede", 10, "formal", 50);
	//o.displayOutfit();

	int choice;
	cout << "Enter 1 for Dress, 2 for Shoes, 3 for both: " << endl;
	cin >> choice;
	if (choice == 1)
	{
		string md;
		int szd;
		string std;
		double pd;
		cout << "Enter material: " << endl;
		cin >> md;
		cout << "Enter size: " << endl;
		cin >> szd;
		cout << "Enter style: " << endl;
		cin >> std;
		cout << "Enter price: " << endl;
		cin >> pd;
		Dress::displayDress();
	}
	if (choice == 2)
	{
		string ms;
		int szs;
		string sts;
		double ps;
		cout << "Enter material: " << endl;
		cin >> ms;
		cout << "Enter size: " << endl;
		cin >> szs;
		cout << "Enter style: " << endl;
		cin >> sts;
		cout << "Enter price: " << endl;
		cin >> ps;
		Shoes::displayShoes();
	}
	if (choice == 3)
	{
		string md;
		int szd;
		string std;
		double pd;
		string ms;
		int szs;
		string sts;
		double ps;
		cout << "Enter material for dress: " << endl;
		cin >> md;
		cout << "Enter size for dress: " << endl;
		cin >> szd;
		cout << "Enter style for dress: " << endl;
		cin >> std;
		cout << "Enter price for dress: " << endl;
		cin >> pd;
		cout << "Enter material for shoes: " << endl;
		cin >> ms;
		cout << "Enter size for shoes: " << endl;
		cin >> szs;
		cout << "Enter style for shoes: " << endl;
		cin >> sts;
		cout << "Enter price for shoes: " << endl;
		cin >> ps;
	}

}

void displayOutfit()
{
	Outfit::displayOutfit();
}


All my "Dress::displayDress()" , "Shoes::displayShoes()" and "Outfit::displayOutfit()" have been underlined in the main file which is to be error. I'm not sure how to compile this function.
What error messages are you getting from your compiler? That should give you some info to go on.

(I'll note that, for starters, I don't actually see anywhere in your code where you actually create an object of type Dress or Shoes.)
Last edited on
Line 19-22: These variables are local to the scope of the if. They go out of scope at line 32.

Line 35-38: ditto.

Line 51-58: ditto.

Line 31,47,81: You can't call a member function like that. You have to instantiate an object. Then call the member function via the instantiated object.

You prompt for a lot of information, but never construct objects with that information.

outfit.h lines 11-12: You inherit Dress and Shoes. You don't need these as member variables.
I get errors of illegal call of non-static member function. You mean the member function cant inherit from the Dress and Shoes header files? So may I know how to instantiate the Dress and Shoes?
Edit :
I get errors of illegal call of non-static member function. You mean the member function cant inherit from the Dress and Shoes header files? So may I know how to instantiate the Dress and Shoes?

That means that you have a static member function somewhere, but it happens to call a non-static member function.
Well, the rule are :
+ Non static member functions can call other non static member functions and static member functions.
+ Static member functions can only call other static member functions.
Last edited on
Ah, I didn't see that your Outfit class was inheriting from the other classes.

Why are you trying to make Outfit inherit from Dress and Shoes? Inheritance models an "is-a" relationship. That's not what you have here - an outfit isn't a special type of dress, or a special type of shoes.

What you have is a "has-a" relationship. An outfit has a dress, and it has shoes. For this type of relationship, you should use composition, not inheritance.

In any case, to call a non-static method of a class, you need to create an actual object of that class. I notice that you have some commented-out code in main() for creating an Outfit object, so clearly you know how to do that - although I'm not sure why you commented it out.

Edit: And be wary of people who try to get you to PM them, to discuss the problem in private. This forum has trolls who try and maliciously give people bad advice, and attempt to do it in PM's so that they don't get called out on their behaviour. You can discuss your problem in public in the forum, where everyone can help.
Last edited on
I commented them out cause the question requires me to prompt the user for the choices of dress and shoes. Hence, after taking in their choices of dress and shoes, I wanna use the function of displayDress(), displayShoes() or displayOutfit() [for both] that I created in the earlier cpp files, but it seems to have an error.

Oh thanks for the heads up! Appreciate that :)
So, there are two points here:

1) You need to rework your design, as the current one you have (using inheritance) doesn't match the relationships your software is modelling

2) You need to instantiate an object, and call methods on that object. You can't just pluck those methods out of thin air, because they need to access the state of an actual Dress or Shoes object.
Last edited on
Topic archived. No new replies allowed.