0

I created a class with static members but I faced a compiling error which is "a storage class can only be specified for objects and functions.".

What's the solution?

#include<iostream>
using namespace std;

static class Hi {

    public: static string ShowMessage(string Message)
    {
        return Message;
    }
};

int main()
{
    cout << Hi::ShowMessage("Hello");
    return 0;
}
max66
  • 65,235
  • 10
  • 71
  • 111
  • What do you mean with "`static class`" ? – max66 Aug 11 '20 at 14:20
  • I think this can help : [Can I write something like `static class`?](https://stackoverflow.com/a/31201984/13007571) – Hokkyokusei Aug 11 '20 at 14:23
  • Some other languages do not allow namespaces to contain code or data, only types, and end up using `static class` as a workaround for this limitation. C++ namespaces *can* have function and data members, and are what you are looking for here. (C++ does still end up using non-instantiable classes to workaround *other* limitations, like that fact that a namespace cannot have template arguments and a class can) – Ben Voigt Aug 11 '20 at 15:11

2 Answers2

2

There is no such thing as "static class" in C++.

You can have a static member or a static method (as your ShowMessage()) inside a class or a struct.

You can also have a static object that instantiate a class/struct.

But there isn't a static class

max66
  • 65,235
  • 10
  • 71
  • 111
1

There is no static specifier for class in C++. A class can be final or abstract but not static. So this will give syntax error.

Karan Kamboj
  • 121
  • 1
  • 5