My C++ is little rusty. Can anyone see why I am seeing “not a class or namespace” compile-time error in main() while accessing i.
#include <iostream>
using namespace std;
class Singleton2 {
Singleton2(){
i = 0;
}
public:
int i;
friend Singleton2 & singleton2();
};
Singleton2 & singleton2(){
static Singleton2 ms;
return ms;
}
int main() {
Singleton2 ms = singleton2();
int i = ms::i; // error: `ms' is not a class or namespace
cout << i << endl;
}
You use dot
.operator to access non-static members, not the scope resolution operator::Note that the assignment below
makes a copy of your singleton. Your
singleton2()returns a reference, so you should probably makemsa reference as well: