unresolved symbol -_- (static int)

This is a discussion on unresolved symbol -_- (static int) within the C++ Programming forums, part of the General Programming Boards category; btw: the -_- is a face looking gloom, not a var name Code: #include <iostream.h> class Person { private: static ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    unresolved symbol -_- (static int)

    btw: the -_- is a face looking gloom, not a var name

    Code:
    #include <iostream.h>
    
    class Person
    {
    private:
    	static int myCount;
    	char* name;
    public:
    	Person(char* name)
    	{
    		this->name=name;
    		myCount++;
    	}
    	Person()
    	{
    		name="Unknown";
    		myCount++;
    	}
    	virtual ~Person()
    	{
    		myCount--;
    		name="";
    	}
    	static int count()
    	{
    		return myCount;
    	}
    	friend ostream& operator<<(ostream& out, Person& in);
    };
    
    ostream& operator << (ostream& out, Person& in)
    {
    	out<<in.name;
    	return out;
    }
    
    int main()
    {
    	Person john("John");
    	Person* ptr = new Person("Johnny boy");
    	cout<<john<<endl;
    	cout<<*ptr<<endl;
    	cout<<Person::count()<<endl;
    	delete ptr;
    	cout<<Person::count()<<endl;
    	return 0;
    }
    Just playing around with something and I get the error:
    Code:
    haha.obj : error LNK2001: unresolved external symbol "private: static int  Person::myCount" (?myCount@Person@@0HA)
    I tried playing with some different ways of putting it etc but couldn't really come up with something that worked. Anyone have an idea of what's wrong besides the fact that im using windows -_-.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    try:
    Code:
    int Person::myCount = 0;
    after the class declaration and before int main.

    PS. Is there a reason you are using the old, non-standard <iostream.h> header?

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    because I always do.
    thanks for the help, though I thought static ints were guaranteed to be initialized to 0, I guess not in Classes.

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    It is not that it's not initialized, that code is there because myCount doesn't *exist* until you do that.

    A static variable in a class only *declares* the variable. It just says "I will make a variable with this name somewhere". In one source file, you need to actually create it.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  2. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  3. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

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