1
using namespace std;
class Sample
{ int x;
  static int count;
   public:
  void get();
  static void showCount();
};
void Sample::get()
{
    cin>>x;
    ++count;
}
static   void Sample::showCount(){    
    cout<<"Total No. of Objects :"<<count;
}
 int main()
{ Sample s1,s2;
    s1.get();
    s2.get();
    Sample::showCount();
   return 0;

}

Compilation Error :[Error] cannot declare member function 'static void Sample::showCount()' to have static linkage[-fpermissive]

4 Answers4

3

Remove static keyword

void Sample::showCount(){    
    cout<<"Total No. of Objects :"<<count;
}

static keyword in a class member function declaration has a different meaning to static keyword in a function definition. The former tells that the function doesn't need an instance of the class (doesn't get this pointer), the later defines static linkage: local to the file, the function is accessible in this particular file only.

You are also missing definition of count. You need a line somewhere before main:

int Sample::count = 0;
...
main() {
...
}
dmitri
  • 3,183
  • 23
  • 28
0
class Sample
{ ...
    static void showCount();
};

static   void Sample::showCount(){    
    cout<<"Total No. of Objects :"<<count;
}
// You can void instead of static, because you are not returning anything.

This is incorrect. You don't need 2nd static.

Shravan40
  • 8,922
  • 6
  • 28
  • 48
0

When declaring static member functions in C++, you only declare it as static inside the class definition. If you implement the function outside the class definition (like you have), don't use the static keyword there.

Outside of a class definition, the static keyword on a function or variable means that the symbol should have "static linkage." This means that it can only be accessed within the source file (translation unit) that it's in. static symbols aren't shared when all of your compiled source files are linked together.

See this SO question if you aren't familiar with the term "translation unit."

You have one other problem, which Saurav Sahu mentioned: you declared the static variable static int count; but never defined it. Add this outside of the class definition:

int Sample::count = 0;
Community
  • 1
  • 1
qxz
  • 3,814
  • 1
  • 14
  • 29
0
#include <iostream>
using namespace std;
class Sample
{
    int x;
public:
    static int count;
    void get();
    static void showCount();
};
int Sample::count = 0;
void Sample::get()
{
    cin >> x;
    ++count;
}
void Sample::showCount(){
    cout << "Total No. of Objects :" << count;
}
int main()
{
    Sample s1, s2;
    s1.get();
    s2.get();
    Sample::showCount();
    return 0;
}
sitev
  • 193
  • 1
  • 2
  • 10