Header File Will Not Compile
Mar 7, 2014 at 3:19am UTC
I am currently learning OOP, and I can't figure out what the problem is with my header file. The rest of the program complies fine though. I am using g++ on UNIX.
myClass.h:9:23: error: Rectangle.h: No such file or directory
myClass.h: In function ‘int main()’:
myClass.h:13: error: ‘Rectangle’ was not declared in this scope
myClass.h:13: error: expected `;' before ‘rect’
myClass.h:14: error: ‘rect’ was not declared in this scope
myClass.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
// Luca Del Signore
// Computer Science II
/* This header file contains
The main function for my
rectangle class program.
*/
#include "Rectangle.h"
using namespace std;
int main(){
Rectangle rect;
rect.displayMaxRectangles();
rect.process();
rect.summary();
return 0;
}
RectangleClass.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 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
// Luca Del Signore
// Computer Science II
/* I created the class Rectangle
and the object rect to orgainze
the data members and member
functions that are used to
create rectangles and store the
information related to them.
*/
#include <iostream>
class Rectangle {
public :
Rectangle();
void displayMaxRectangles();
void rectangleCharacters();
void process();
void summary();
void getInfo();
private :
double length, width;
char borderCharacter, fillCharacter, yesno;
const int MAX, MIN;
};
static const int maxRectangles = 5;
static int rectanglesCreated;
static double totalPerimeter, totalArea;
void Rectangle::displayMaxRectangles(){
std::cout << "The maximum number of rectangles that can be created is " << maxRectangles << std::endl;
}
void Rectangle::rectangleCharacters(){
std::cout << "Enter the desired border character of the rectangle." << std::endl;
std::cin >> borderCharacter;
std::cout << "Enter the desired filler character of the rectangle." << std::endl;
std::cin >> fillCharacter;
}
void Rectangle::process(){
do {
getInfo();
std::cout << "Would you like to display the rectangle?" << std::endl;
std::cin >> yesno;
while ((yesno != 'n' ) || (yesno != 'N' )){
if ((yesno == 'y' ) || (yesno == 'Y' )){
rectangleCharacters();
for (int j = 0; j < length; j++){
if ((j == 0) || (j == length - 1)){
for (int v = 0; v < length; v++){
std::cout << borderCharacter;
}
}
else {
for (int w = 0; w < width; w++){
if ((w == 0) || (w == width - 1)){
std::cout << borderCharacter;
}
else {
std::cout << fillCharacter;
}
}
}
}
}
}
std::cout << std::endl << std::endl;
rectanglesCreated++;
std::cout << "Would you like to create another rectangle?" << std::endl;
std::cin >> yesno;
if ((yesno == 'n' ) || (yesno == 'N' )){
summary();
}
}while (rectanglesCreated < maxRectangles);
}
void Rectangle::summary(){
std::cout << std::endl << std::endl << std::endl;
std::cout << "Rectangles Built: " << rectanglesCreated << std::endl;
std::cout << "Average Area: " << (totalArea / rectanglesCreated) << std::endl;
std::cout << "Average Perimeter: " << (totalPerimeter / rectanglesCreated);
}
void Rectangle::getInfo(){
std::cout << "Enter the desired length of the rectangle." << std::endl;
std::cin >> length;
std::cout << "Enter the desired width of the rectangle." << std::endl;
std::cin >> width;
std::cout << "The perimeter of this rectangle is " << (length * 2) + (width * 2);
std::cout << "The area of this rectangle is " << (length * width);
totalPerimeter += (length * 2) + (width * 2);
totalArea += length * width;
}
Rectangle::Rectangle (): MAX(25), MIN(5){
length = 5;
width = 10;
borderCharacter = '#' ;
fillCharacter = '*' ;
}
Mar 7, 2014 at 3:21am UTC
main() should be in a source file.
The preprocessor can't find Rectangle.h; is that really the name of your header? It seems you have only RectangleClass.cpp.
The definition of the Rectangle class should be in a header file, not a source file as you have it here.
Topic archived. No new replies allowed.