0

[November] 13.

Greetings and salutations!
I am currently experiencing some trouble with some novice coding, and some assistance would be greatly appreciated.
My goal is to access the static data member and static function of friend class Counter without actually creating a Counter object. I am under the impression that this is possible since the data member and function are both static.

When I compile the code bellow in Dev C++ I receive the following linking errors:
[Linker error] undefined reference to `Counter::count'
[Linker error] undefined reference to `Counter::count'
ld returned 1 exit status

I added three comments to the portions of the code where the main issues seem to originate.
Please. What must I do to get this code to compile properly?
If a Counter object must be created, this is fine. But I would like to get this working without the Counter object if possible.
Thank you!

#include <iostream>
using std::cout;
using std::cin;
using std::endl;   	
     
    class Counter
 	{
        friend class Example;
 	public:
 	    static int getCount()
 	    { 
 	       return count; 
	    }
	private:
	    static int count; 
	};
 
 
	class Example
	{ 
	public:
	    Example(int initData) 
	        : data(initData)
	    { 
	        Counter::count++; // this seems to be a problem
	    }
	 
	    void process() 
	    {
	        data = data * 10; 
	    }
	private:
	    int data;
 	};


    int main()
    {
        cout << "The counter is: " << Counter::getCount() << endl; 
        // this seems to be a problem
        
        Example application( 5 );
        
        cout << "The counter is: " << Counter::getCount() << endl; 
        // this seems to be a problem
        
        system("pause");
        return 0;
    }
2
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by dkalita
1

u must intialize a static member.
Add the line

int Counter::count = 0;

before main() or other module where u are using or just after declaring your class.


A static data member must always be initialized before being used.

Edited by dkalita: n/a

0

[November] 13.

Your help is greatly appreciated, dkalita, but when I do this I receive the following error:

36 `Count' has not been declared

I only made one change in accordance with your suggestion. Here is what I did for clarity:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;   	
     
    class Counter
 	{
        friend class Example;
 	public:
 	    static int getCount()
 	    { 
 	       return count; 
	    }
	private:
	    static int count; 
	};
 
 
	class Example
	{ 
	public:
	    Example(int initData) 
	        : data(initData)
	    { 
	        Counter::count++; // this seems to be a problem
	    }
	 
	    void process() 
	    {
	        data = data * 10; 
	    }
	private:
	    int data;
 	};

    int Count::count = 0;

    int main()
    {
        cout << "The counter is: " << Counter::getCount() << endl; 
        // this seems to be a problem
        
        Example application( 5 );
        
        cout << "The counter is: " << Counter::getCount() << endl; 
        // this seems to be a problem
        
        system("pause");
        return 0;
    }

Edited by William Byrd II: n/a

2

hey thats the Counter class. Sorry I misspelled it.
Make it as

int Counter::count = 0;
Votes + Comments
Simple fix, problem solved!
0

[November] 13.

Fantastic!
I should have caught that as well–my apologies.
I am no longer stuck, and I can now reinforce these concepts before moving forward.
Thank you very much, dkalita, for this has helped me a great deal!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.