Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include "stdafx.h"

class Animal
{
public:
	static int ID;
};

int main()
{
	Animal::ID = 5;
	printf("%d \n", Animal :: ID);
	return 0;
}

Whats wrong with this ???

What I have tried:

Asking question here in CodeProject.com
Posted
Updated 30-Jul-17 20:00pm
v2
Comments
Thomas Daniels 30-Jul-17 15:15pm    
Could you please describe what you mean by "what's wrong"? Do you receive a compiler error? Runtime crash? And which compiler are you using?
The_Unknown_Member 30-Jul-17 16:33pm    
Not compiling. Here is the error message (its a screenshot just open the link):
https://i.gyazo.com/bfd20571fb65d0ed231aa5c10720065d.png
Richard MacCutchan 31-Jul-17 3:24am    
You never instantiate an Animal object, so ID does not exist in your program.

1 solution

The problem i that you missing the definition part (you only have the declaration part) and that leads to an incomplete type...
You can fix it like this...
C++
class Animal
{
public:
	static int ID;
};

int Animal:ID;
 
int main()
{
	Animal::ID = 5;
	printf("%d \n", Animal::ID);
	return 0;
}

A much better expatiation is here: static members - cppreference.com[^]
 
Share this answer
 
Comments
CPallini 31-Jul-17 3:52am    
5.
Kornfeld Eliyahu Peter 31-Jul-17 3:53am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900