C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 19th, 2005, 12:46 AM
Spifficus Spifficus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 4 Spifficus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 32 sec
Reputation Power: 0
Question Undeclared identifier - scope problem?

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



Reply With Quote
  #2  
Old April 19th, 2005, 03:39 AM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
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.



Reply With Quote
  #3  
Old April 19th, 2005, 09:24 PM
Spifficus Spifficus is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Posts: 4 Spifficus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 28 m 32 sec
Reputation Power: 0
Quote:
Originally Posted by B-Con
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....


Aha! Thank you, B-Con. It is sometimes the simple mistakes that catch you.

Reply With Quote
  #4  
Old April 20th, 2005, 11:06 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
hth

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Undeclared identifier - scope problem?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT