Thread: Compile ok,Excute error,why???

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Hangzhou,China
    Posts
    7

    Compile ok,Excute error,why???

    Hi, I have a question here

    Code:
    #include<iostream>
    using namespace std;
    
    class Counted{
    public:
    	Counted():id(count++){ cout << id << endl; }
    	~Counted();
    private:
    	static int count;
    	int id;
    	
    };
    
    Counted::~Counted()
    {
    	cout << "Being destroyed" << endl;
    }
    
    void main()
    {
    	Counted c;
    }
    When i compile it ,it's OK!
    When i excute it ,the following error message:
    Linking...
    HW_Ch13_1.obj : error LNK2001: unresolved external symbol "private: static int Counted::count" (?count@Counted@@0HA)
    Debug/HW_Ch13_1.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    HW_Ch13_1.exe - 2 error(s), 0 warning(s)

    so, could anyone tell me why??thx~~~

  2. #2
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Usually that happens when you declare something in a class but never really define it. ...And... main() should return an int.

  3. #3
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    You forgot to initialize "static int count"

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Lorin_sz
    When i compile it ,it's OK!
    When i excute it ,the following error message:
    Linking...
    HW_Ch13_1.obj : error LNK2001: unresolved external symbol "private: static int Counted::count" (?count@Counted@@0HA)
    Debug/HW_Ch13_1.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.

    HW_Ch13_1.exe - 2 error(s), 0 warning(s)
    First, that's not execution, that's linking.

    Second, static data members need to be initialized like this.
    Code:
    #include<iostream>
    using namespace std;
    
    class Counted{
    public:
    	Counted():id(count++){ cout << id << endl; }
    	~Counted();
    private:
    	static int count;
    	int id;
    	
    };
    
    int Counted::count = 0;
    
    Counted::~Counted()
    {
    	cout << "Being destroyed" << endl;
    }
    
    int main()
    {
    	Counted c;
    }
    http://www.parashift.com/c++-faq-lit...html#faq-10.11
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Hangzhou,China
    Posts
    7
    thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM