Reputation: 1
So I have been building on a small example of classes I found floating around to better prepare me for some C++ coding assignments in the future. Trying to compile this code gives me an error: "classExample.cpp:12:2: error: 'list' does not name a type".
The only problem is, I most definitely assigned it's type to be of Rectangle* as you can see in the code below. What am I doing wrong?
// classes example
#include <iostream>
#include <stdlib.h>
#include <string>
#include <sstream>
using namespace std;
class setOfRectangles {
list<Rectangle*> rectangles;
};
class Rectangle {
int width, height; //class variables
public: //method constructors
Rectangle(){width = 0; //constructor for rectangle
height = 0;}
Rectangle(int i, int j){width = i; height=j;}
void set_values (int,int);
string toString();
int area() {return width*height;}
int perimeter(){return (2*width)+(2*height);}
};
void Rectangle::set_values (int x, int y) { //fleshing out class method
width = x;
height = y;
}
string Rectangle::toString()
{
/*
Prints information of Rectangle
Demonstrates int to string conversion (it's a bitch)
*/
std::stringstream sstm; //declare a new string stream
sstm << "Width = ";
sstm << width;
sstm << "\n";
sstm << "Height = ";
sstm << height;
sstm << "\n";
sstm << "Perimeter = ";
sstm << perimeter();
sstm << "\n";
return sstm.str(); //return the stream's string instance
}
int main (int argc, char* argv[]) {
if(argc != 3)
{
cout << "Program usage: rectangle <width> <height> \n";
return 1;
}
else
{
Rectangle rect = Rectangle(2,3); //new instance, rectangle is 0x0
cout << "area: " << rect.area() << "\n";
cout << rect.toString() << endl;
//call this method
rect.set_values (atoi(argv[1]),atoi(argv[2]));
cout << "area: " << rect.area() << "\n";
cout << rect.toString() << endl;
return 0;
}
}
Upvotes: 0
Views: 1409
Reputation: 44931
You need to include the proper header for list
: #include <list>
Also, why are you including the C header <stdlib.h>
in a C++ application? Is it for atoi
? If so, you should look into how to do casts in C++ in the proper way. Or include <cstdlib>
instead (which is the C++ version of the same header).
Also, you need to move this:
class setOfRectangles {
list<Rectangle*> rectangles;
};
to after the declaration of class Rectangle
, or the compiler won't know what a Rectangle is.
Upvotes: 2