[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;
}