NEWBIE: Trouble compiling example from Sams Book

A

A. Name

Hello All,

I am having trouble trying to compile an example from the Sam's 21
days series. The error is "'Rectangle' undeclared (first use this
function)"

I have even dowloaded and tried to compile there version of the code
and still receive the same error.

Any help would be appreciated.

Code Example. Listing 8.8

// Using the "this" Pointer

#include <iostream.h>

using std::cout;

class Rectangle
{
public:
Rectangle(); // constructor
~Rectangle(); // deconstructor
void SetLength(int length) { this->itsLength = length; }
int GetLength() const { return this->itsLength; }

void SetWidth(int width) { itsWidth = width; }
int GetWidth() const { return itsWidth; }
private:
int itsLength;
int itsWidth;
};

Rectangle::Rectangle()
{
itsWidth = 5;
itsLength = 10;
}

Rectangle::~Rectangle()
{}

int main()
{
Rectangle theRect;

cout << "theRect is " << theRect.GetLength() << " feet long.\n";
cout << "theRect is " << theRect.GetWidth() << " feet wide.\n\n";

cout << "We are now changing the attributes of theRect.\n\n";

theRect.SetLength(20);
theRect.SetWidth(10);

cout << "theRect is now " << theRect.GetLength() << " feet
long.\n";
cout << "theRect is now " << theRect.GetWidth() << " fee
wide.\n\n";

return 0; // application termination
}
 
R

Ron Natalie

A. Name said:
Hello All,

I am having trouble trying to compile an example from the Sam's 21
days series. The error is "'Rectangle' undeclared (first use this
function)"

It more or less looks right (other than the use of the incorrect header name).
Are you sure you are compiling it in C++ mode (your file is .cpp or whatever
your compiler expects).
 
G

Gianni Mariani

A. Name said:
Hello All,

I am having trouble trying to compile an example from the Sam's 21
days series. The error is "'Rectangle' undeclared (first use this
function)"

I have even dowloaded and tried to compile there version of the code
and still receive the same error.

Any help would be appreciated.

Code Example. Listing 8.8

// Using the "this" Pointer

#include <iostream.h>

loose the ".h"

should be:

#include <iostream>

The code compiles and runs fine on gcc 3.3.1 and VC++ 6.0 SP5.

I think you need a better compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top