the "this" pointer

This is a discussion on the "this" pointer within the C++ Programming forums, part of the General Programming Boards category; Hi, Could someone take a look at this code (taken from Sam's Teach Yourself C++ in 24 hrs, 2nd ed.) ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Question the "this" pointer

    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.

    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;
     }
    Here are the error msgs:


    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

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Not helpful I know, but compiles and runs fine with MS VC++ 6.0.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Hmm that is strange. Iīve compiled your code under Dev-C++ and it works.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> Iīve compiled your code under Dev-C++ and it works.

    Well, that would indicate that the problem is not compiler specific. What I suggest is you delete your source file, create a new one and cut and paste the source from your mesage above into it. You may have some non-visible screwy characters in your file.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    Well, that would indicate that the problem is not compiler specific. What I suggest is you delete your source file, create a new one and cut and paste the source from your mesage above into it. You may have some non-visible screwy characters in your file.
    I agree, and if it doesnīt work, reinstall Dev-Cpp.
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I get the same problem on the latest version of Dev-C++, but not on older versions. As best I can figure out, the standard C++ headers don't like the identifier Rectangle. If you change it to Rect the program will compile and run without complaint. I don't have time to diagnose the problem fully, but that's your fix...just change the name of the class.
    My best code is written with the delete key.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Perhaps this is an example of where
    I considered that as well, but replacing the directive with the declaration for cout only had the same effect.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    I would suggest that Jesse Liberty's books are so bad, they have somehow corrupted the compiler. Did you leave your book on top of your computer?

  9. #9
    Registered User whistlenm1's Avatar
    Join Date
    Jan 2002
    Posts
    124
    At work right now (i.e., quick look) but if the code example is the same as that of your source code where is the closing brace for class Rectangle.

    Never mind!!!
    Man's mind once streched by a new idea, never regains its original dimensions
    - Oliver Wendell Holmes

    In other words, if you teach your cat to bark (output) and eat dog food (input) that doesn't make him a dog. It would have to chase cars, chew bones, and have puppies before I'd call it Rover ;-)
    - WaltP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  3. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 07:23 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. Problem with "this" pointer
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 02-07-2002, 09:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21