FedoraForum.org - Fedora Support Forums and Community
Results 1 to 8 of 8
  1. #1
    darksilver Guest

    Post How to use type "static" in C++?

    Sorry to say that the question is not related to Fedora Core at all, the only thing to do with Fedora Core is I am using its newly integrated Eclipse Platform to do my high school computing homework.

    The question ask me to use static as such
    Code:
    class Bookshop
    {
    private:
    	...
    	static unsigned transOkcount;		//number of successful transactions
    	static unsigned transNotOkcount;	//number of unsuccessful transactions
    	...
    public:
    	...
    	static unsigned gettransOkcount();	//get number of successful transactions
    	static unsigned gettransNotOkcount();//get number of unsuccessful transactions
    	...
    }
    and I declare it the same way as other variable type as int, float etc.
    Code:
    static unsigned Bookshop::gettransOkcount()
    {
    	return transOkcount;
    }
    static unsigned Bookshop::gettransNotOkcount()
    {
    	return transNotOkcount;
    }
    but I got errors
    Code:
    Severity	Description	Resource	In Folder	Location	Creation Time
    error: cannot declare member function ‘static unsigned int Bookshop::gettransOkcount()’ to have static linkage	Bookshop.cpp	CW3	line 48	July 8, 2005 7:44:31 PM
    error: cannot declare member function ‘static unsigned int Bookshop::gettransNotOkcount()’ to have static linkage	Bookshop.cpp	CW3	line 52	July 8, 2005 7:44:31 PM
    I know it may not be right to ask such question here, but any kind person can just help me solve it? REALLY THANKS!

  2. #2
    Join Date
    Nov 2004
    Location
    Netherlands
    Age
    39
    Posts
    1,426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If I remember ok, then you don't need to declare functions static again here, so
    unsigned Bookshop::gettransOkcount(){
    return transOkcount;
    }
    unsigned Bookshop::gettransNotOkcount(){
    return transNotOkcount;
    }
    Registered Linux user number 389291

    Laptop: Nec Versa p550, Pentium M 1.86GHz, 1024MB ram, x300, 80 GB HD, bluetooth, 2915BG Wlan card
    Desktop: Amd Athlon x2 4200+, 2GB ram, Geforce 7300GT 512MB silent, 160GB HD in a nice centurion 534 case :cool:

  3. #3
    darksilver Guest
    I tried and still get errors
    Code:
    Severity	Description	Resource	In Folder	Location	Creation Time
    undefined reference to `Books::transOkcount'	Books.cpp	CW3	line 50	July 8, 2005 8:29:45 PM
    undefined reference to `Books::transNotOkcount'	Books.cpp	CW3	line 54	July 8, 2005 8:29:45 PM

  4. #4
    Join Date
    Feb 2005
    Location
    Scotland
    Posts
    445
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had a look on Google, cos I'm not a C++ programmer, but it seemed like an interesting problem. The best I have found so far is http://www.gamedev.net/community/for...opic_id=327007 Search for linkage or something. Sounds like you need to set the function as static in the header file but not cpp. I dunno if this helps.

  5. #5
    Join Date
    Nov 2004
    Location
    Netherlands
    Age
    39
    Posts
    1,426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.eng.usyd.edu.au/tutorial/...lusplus07.html
    I was right, but you made another error, unsigned doesn't exist, use unsigned int or unsigned char
    Registered Linux user number 389291

    Laptop: Nec Versa p550, Pentium M 1.86GHz, 1024MB ram, x300, 80 GB HD, bluetooth, 2915BG Wlan card
    Desktop: Amd Athlon x2 4200+, 2GB ram, Geforce 7300GT 512MB silent, 160GB HD in a nice centurion 534 case :cool:

  6. #6
    Join Date
    Aug 2004
    Posts
    4,256
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    // I think you need to have a declaration (or should I say "definition"?) of your static variables outside the class itself, as in this example:

    // An example to show that a static member function of a class can be used without
    // creating an instance of that class.

    class classA
    {
    public:

    static int getZ();
    static void setZ(int x){ Z = x; }
    private:

    static int Z;
    int Y;
    };

    int classA::Z;

    int classA::getZ(){ return(Z);}

    int main()
    {
    classA::setZ(3);
    int s = classA::getZ();

    }
    "Never let the task you are trying to accomplish distract you from the study of computers."

  7. #7
    Join Date
    Nov 2004
    Location
    Netherlands
    Age
    39
    Posts
    1,426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @darksilver:
    I don't know if it is a typo or so, but you don't have a ; behind your class' }
    Registered Linux user number 389291

    Laptop: Nec Versa p550, Pentium M 1.86GHz, 1024MB ram, x300, 80 GB HD, bluetooth, 2915BG Wlan card
    Desktop: Amd Athlon x2 4200+, 2GB ram, Geforce 7300GT 512MB silent, 160GB HD in a nice centurion 534 case :cool:

  8. #8
    darksilver Guest
    Thanks all.
    Quote Originally Posted by tashirosgt
    I think you need to have a declaration (or should I say "definition"?) of your static variables outside the class itself,
    Yes, it does solve my problem.

Similar Threads

  1. Replies: 4
    Last Post: 8th September 2008, 01:00 AM
  2. Replies: 0
    Last Post: 21st January 2007, 09:32 PM
  3. Replies: 0
    Last Post: 31st July 2006, 10:05 AM
  4. Replies: 1
    Last Post: 2nd September 2005, 07:22 AM
  5. Replies: 0
    Last Post: 23rd May 2005, 04:59 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •