Problem with static variable

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackstormdragon
    New Member
    • Feb 2007
    • 32

    Problem with static variable

    In my program I have a static variable that I declared, but when I debug my program is tells me that it hasn't been initialized.
    Code:
    class HotDogStand
    {
    public:
    	HotDogStand(int standID, int dogSold);
    	void JustSold();
    	int ReturnJustSold();
    	static int TotalDogSold(int total);
    
    private:
    	int _standID, _dogSold;
    	static int _totalDogSold;
    };
    
    int HotDogStand::_totalDogSold = 0;
    
    void main()
    I initialized the static variable outside of the class and it looks like the example in my book, so I don't understand whats wrong.
    Thanks
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    I don't have a textbook in front of me, but try adding the static keyword to your initialization? i.e.

    [CODE=cpp]static int HotDogStand::_t otalDogSold = 0;[/CODE]

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      This compiled fine with Linux gnu g++, except for the main() at the end.

      I assume this is a code snippet, because the body for main is not defined, and my compiler won't let me compile a void main(). It wants an int main()

      Comment

      • blackstormdragon
        New Member
        • Feb 2007
        • 32

        #4
        Thanks for the help, it seems my problem was else where. When I went to post some more code here, I saw my problem clear as day. So thanks for the help, both of you.

        Comment

        Working...