Using C++ I'm trying to nest an object of one class inside another class and I get a syntax error on line 6 of CarpetClass.h
that says
Error: fuction "Rectangle" is not a type name
myclass.h
class Rectangle{
private:
double length;
double width;
public:
void setLength(double len){
length = len;
}
void setWidth(double wid){
width = wid;
}
double getLength(){
return length;
}
double getWidth(){
return width;
}
double getArea(){
return length*width;
}
};
CarpetClass.h
#include "myclass.h"
class Carpet{
private:
double pricePerSqYd;
Rectangle size;
public:
void setPricePeryd(double p){
pricePerSqYd = p;
}
void setDimensions (double len, double wid){
size.setLength(len / 3);
size.setWidth(wid / 3);
}
double getTotalPrice(){
return (size.getArea*pricePerSqYd);
}
};
Source.cpp
#include <iostream>
#include "CarpetClass.h"
using namespace std;
int main(){
Carpet purchase;
double pricePerYd;
double length;
double width;
cout << "Room length in feet: ";
cin >> length;
cout << "Room width in feet: ";
cin >> width;
cout << "Carpet price per sq. yard: ";
cin >> pricePerYd;
purchase.setDimensions(length, width);
purchase.setPricePeryd(pricePerYd);
cout << "\nThe total price of my new " << length << "x" << width << " carpet is $" << purchase.getTotalPrice() << endl;
}
I don't know why I'm getting an error I copied example code right out of my text book. I have tried putting both classes in my cpp file and also putting them both in the same header file. Neither of those solutions worked. Please help me understand why I'm getting this error.