problem with static member

This is a discussion on problem with static member within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream.h> class hello{ private: int a; public: static int val; }; void main(){ cout<<"gaurav "<<hello.val; } this code ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33

    problem with static member

    Code:
    #include <iostream.h>
    
    class hello{
      private:
        int a;
    
      public:
        static int val;
    };
    
    void main(){
      cout<<"gaurav "<<hello.val;
    }
    this code doesn't compile, it says improper use of typedef 'hello' in function main()

    i am using borland c++ compiler

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Code:
    #include <iostream>
    
    class hello{
      private:
        int a;
    
      public:
        static int val;
    };
    
    int hello::val;
    
    int main(){
      std::cout<<"gaurav "<<hello.val;
    }
    "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
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    You must use the scope resolution operator (::) instead of the dot operator, which is used to reference members of an object.
    Code:
    cout << "gaurav " << hello::val;
    Also, you must declare static member variables as previously mentioned.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Try it this way.
    Code:
    #include <iostream>    // new header
    
    using namespace std;
    
    class hello{
      private:
        int a;
      public:
        static int val;
    };
    
    int hello::val = 12;  // define an initialize it
    
    int main(){          // main must return int
      cout<<"gaurav "<< hello::val;  // scope resolution op needed
    }
    Kurt

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You must use the scope resolution operator (::) instead of the dot operator, which is used to reference members of an object.
    Both the dot operator and the scope resolution operator work for static members with VC++6. But if you don't initialize the static member, then you get errors:
    Code:
    int hello:val;  //intializes to 0
    int hello::val = 12;
    Last edited by 7stud; 02-24-2006 at 01:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. TCP Header problem (error)
    By nasim751 in forum C Programming
    Replies: 1
    Last Post: 04-25-2008, 07:30 AM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM

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