member pointer

This is a discussion on member pointer within the C++ Programming forums, part of the General Programming Boards category; Hello there peoples! How are ya! Can anyone here help me with a little problem I have? I am trying ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    member pointer

    Hello there peoples! How are ya! Can anyone here help me with a little problem I have?
    I am trying to create class with a member pointer, but when i try to dereference it the program crashes, but i dont get any error message from the compiler. Here is the code:

    Code:
    class TEST
    {
    	public:	
    	Test();				//constructor
    	int GetAge () const;
    	void SetAge (int age);
    
    	private:
    	int * pAge;
    };
    
    TEST::Test()
    {
    	int * pAge = new int;
    }
    
    
    
    
    void TEST::SetAge(int age)
    {
    	*pAge = age;
    }
    this is the class, constructor and function. When i try to set age i get error. Can anyone help me?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    TEST::TEST()
    {
    	pAge = new int;
    }
    Should work like that. Remember case sensetivity, Test should be TEST for the constructor.

    [edit]Also should be capitalized properly in the definition of the class above.[/edit]
    Last edited by hk_mp5kpdw; 07-09-2003 at 12:18 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    The reason is C++ is case-sensitive. TEST is different from Test, and consequently, Test is not the constructor of TEST, which means you never call "new int" before trying to set it.

    The compiler should warn you about the function "Test" having no return type, though...

    ** edit **
    Rats... beaten to it!
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    sorry about the mistype...it actually is

    TEST::TEST

  5. #5
    Registered User Casey's Avatar
    Join Date
    Jun 2003
    Posts
    47
    >>sorry about the mistype...it actually is
    Then your problem is the second problem. The first being a typo, the second being a local variable hiding the class member.
    Code:
    int * pAge = new int;
    should be
    Code:
    pAge = new int;

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    great tx!,

    i will do it and get back to ya!

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    By the way, looks like you may need a custom destructor to avoid the potential memory leak:
    Code:
    TEST::~TEST()
    {
        delete pAge;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    And a custom copy constructor and assignment operator, as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. casting a void* to a pointer to a member function
    By Sebastiani in forum C++ Programming
    Replies: 13
    Last Post: 10-15-2002, 08:57 AM

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