| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello post. Caveat begins with 'I began learning C++ a few weeks ago.' I do appreciate any more experienced programmers that may be able to pinpoint my mistakes in the following code.
I have a compile error that I cannot decipher. The explanation is actually straightforward; it complains of two undeclared identifiers, 'length' and 'height', and I thought that I knew exactly what that means...perhaps not. The problem is that the identifiers appear to have been declared in the header. Rectangle.h declares the identifiers 'length' and 'height' as floats. Rectangle.cpp includes the Rectangle.h header file. Rectangle.cpp attempts to assign a value to 'length' and 'height' in their respective 'set' functions. Seems that would be pretty easy. Apparently I have overlooked something. Thanks to any who may be inclined to assist. //header file Rectangle.h follows #ifndef RECTANGLE_H #define RECTANGLE_H #include "Vertex.h" #include <stdio.h> //**************************************** // Class: Rectangle // Purpose: Represent a rectangle in 2D space //**************************************** class Rectangle { public: const static int NUM_VERTS = 4; //************************************* // Method Rectangle -- Constructor // Return value none // Parameters Vertex [ ] vertices -- an array of class Vertex // Purpose defines the four coordinate points of a rectangle //************************************* Rectangle(Vertex lowL, Vertex lowR, Vertex upR, Vertex upL); //************************************* // Method setLength // Return value none // Parameters float lengthInput // Purpose set the length of this rectangle //************************************* void setLength(float lengthInput); //************************************* // Method setHeight // Return value none // Parameters float heightInput // Purpose set the height of this rectangle //************************************* void setHeight(float heightInput); //************************************* // Method getArea // Return value float // Parameters none // Purpose determine the area encompassed by the defining four // coordinate points of this rectangle //************************************* float getArea( ); //************************************* // Method getPerimeter // Return value float // Parameters none // Purpose determine the total length of the sides defined by // the four coordinate points of this rectangle //************************************* float getPerimeter( ); //************************************* // Method toString // Return value string // Parameters char charArray [ ] // Purpose formats the four vertices of a rectangle // into a single string in the form // <float, float>, <float, float>, // <float, float>, <float, float> //************************************* void toStringVerticies(char charArray[]); //variables private: float length, height; Vertex lowerLeft, lowerRight, upperRight, upperLeft; }; // end of class Rectangle #endif //end of file Rectangle.h // source code Rectangle.cpp follows #include "Rectangle.h" #include "Vertex.h" //************************************* // Method Rectangle -- Constructor // Return value none // Parameters Vertex [ ] vertices -- an array of class Vertex // Purpose defines the four coordinate points of a rectangle //************************************* Rectangle::Rectangle(Vertex lowL, Vertex lowR, Vertex upR, Vertex upL){ lowerLeft = lowL; lowerRight = lowR; upperRight = upR; upperLeft = upL; } //************************************* // Method setLength // Return value none // Parameters float lengthInput // Purpose set the length of this rectangle //************************************* void setLength(float lengthInput){ length = lengthInput; } ////************************************* //// Method setHeight //// Return value none //// Parameters float heightInput //// Purpose set the height of this rectangle ////************************************* void setHeight(float heightInput){ height = heightInput; } //************************************* // Method getArea // Return value float // Parameters none // Purpose determine the area encompassed by the defining four // coordinate points of this rectangle //************************************* float Rectangle::getArea( ){ return length * height; } //************************************* // Method getPerimeter // Return value float // Parameters none // Purpose determine the total length of the sides defined by // the four coordinate points of this rectangle //************************************* float Rectangle::getPerimeter( ){ return 2 * (length + height); } //************************************* // Method toString // Return value string // Parameters char charArray [ ] // Purpose formats the four vertices of a rectangle // into a single string in the form // <float, float>, <float, float>, // <float, float>, <float, float> //************************************* void Rectangle::toStringVerticies(char charArray[]){ sprintf(charArray, "<%1.2f,%1.2f>, <%1.2f,%1.2f>, <%1.2f,%1.2f>, <%1.2f,%1.2f>", lowerLeft.getX( ), lowerLeft.getY( ), lowerRight.getX( ), lowerRight.getY( ), upperRight.getX( ), upperRight.getY( ), upperLeft.getX( ), upperLeft.getY( )); } //end of file Rectangle.cpp |
|
#2
|
||||
|
||||
|
you are declaring your setlength() and setheight() functions as normal forward declarations.... look at them closer, you need to be defining them as part of the class that they are in.... the reason that "length" and "height" are not recognized is that the compiler is trying to create the functions independantly, but if you put the class identifier on them then it will look in the class members to find the varaibles....
you actually have the correct code used for functions like "Rectangle::getPerimeter( )", you just need to add "Rectangle::" to the front of the setlength() and setheight() functions....
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips. ![]() |
|
#3
|
|||
|
|||
|
Quote:
Aha! Thank you, B-Con. It is sometimes the simple mistakes that catch you. |
|
#4
|
||||
|
||||
|
hth
![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Undeclared identifier - scope problem? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|