Hi,
Could someone take a look at this code (taken from Sam's Teach Yourself C++ in 24 hrs, 2nd ed.) and take a guess at why Dev-Cpp wouldn't compile it? Thanks.
Here are the error msgs:Code:
// Listing 10.4
// Using the this pointer
#include <iostream>
#include <cstdlib>
using namespace std;
class Rectangle
{
public:
Rectangle();
~Rectangle();
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";
theRect.SetLength(20);
theRect.SetWidth(10);
cout << "theRect is " << theRect.GetLength()<< " feet long.\n";
cout << "theRect is " << theRect.GetWidth()<< " feet wide.\n";
system("pause");
return 0;
}
Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\C\STYCPP24\SOURCE\Chapt10\List1004.cpp" -o "C:\C\STYCPP24\SOURCE\Chapt10\List1004.exe" -g3 -I"C:\DEVCPP\include\c++" -I"C:\DEVCPP\include\c++\mingw32" -I"C:\DEVCPP\include\c++\backward" -I"C:\DEVCPP\include" -L"C:\DEVCPP\lib"
C:/C/STYCPP24/SOURCE/Chapt10/List1004.cpp: In function `int main()':
C:/C/STYCPP24/SOURCE/Chapt10/List1004.cpp:35: `Rectangle' undeclared (first use
this function)
C:/C/STYCPP24/SOURCE/Chapt10/List1004.cpp:35: (Each undeclared identifier is
reported only once for each function it appears in.)
C:/C/STYCPP24/SOURCE/Chapt10/List1004.cpp:35: parse error before `;' token
C:/C/STYCPP24/SOURCE/Chapt10/List1004.cpp:36: `theRect' undeclared (first use
this function) Execution terminated
C:/C/STYCPP24/SOURCE/Chapt10/List1004.cpp:35: `Rectangle' undeclared (first use
I guess this is the offending line: Rectangle theRect;
...but it looks fine to a rookie like yours truly. :) Any ideas? Snide remarks?
Swaine777