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>
usingnamespace 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();
};
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.
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?
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.
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.
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.
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.