Results 1 to 5 of 5

Thread: Using a static member in a class

  1. #1
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Using a static member in a class

    Hello,
    This is just about C++ and it's very simple.
    I'm using QtCreator and MingW .
    I define a new console project with the usual main.cpp and a class called myclass. The header is as follows:
    class MyClass
    {
    public:
    MyClass();
    static int getInstanceCount(){return _instanceCount;}
    private:
    static int _instanceCount;
    };
    The code is as follows
    #include "myclass.h"

    MyClass::MyClass()
    {
    _instanceCount++;
    }
    The code of main is as follows:
    #include <QtCore/QCoreApplication>
    #include <iostream>
    using namespace std;

    #include "myclass.h"
    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);
    MyClass foo;
    cout<<MyClass::getInstanceCount();
    return a.exec();
    }
    The trouble is that this fails at link time with the repeated message that there is an
    undefined reference to MyClass::_instanceCount

    What have I done wrong and what can I do to fix it?
    Thankyou.

  2. #2
    Join Date
    Oct 2008
    Posts
    71
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Using a static member in a class

    your static member has been declared, but not initialized.

    Put this in your .cpp file:
    Qt Code:
    1. int MyClass::_instanceCount = 0;
    To copy to clipboard, switch view to plain text mode 
    Or change the declaration in the .h file:
    Qt Code:
    1. static int _instanceCount = 0;
    To copy to clipboard, switch view to plain text mode 

    BTW, this is not related to Qt in any way

  3. #3
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using a static member in a class

    I realise that this is not related to Qt in any way. It was just not obvious to find a thread to ask about it and it was pretty easy to answer.

    Thank you.

  4. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using a static member in a class

    So a better forum would be General programming, rather than Qt programming, which clearly states "For general, non-Qt programming issues."

  5. #5
    Join Date
    Nov 2009
    Location
    Laval, France
    Posts
    124
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using a static member in a class

    Yes, how embarassing.

Similar Threads

  1. Using a QMutex as a static class member
    By emostar in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 13:48
  2. Replies: 22
    Last Post: 8th October 2008, 13:54
  3. Replies: 3
    Last Post: 19th February 2008, 13:10
  4. static member initialization
    By high_flyer in forum General Programming
    Replies: 2
    Last Post: 17th September 2007, 21:22
  5. Replies: 5
    Last Post: 14th July 2006, 22:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.