Error: Expression must have class type

I am really puzzled about an error I am getting. I have two singleton object classes as described below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
class Bar;

class Foo{
private:
 static Foo* single_foo;
 Foo();
public:
 static Foo* instance(){
   if ( single_foo == NULL )
     single_foo = new Foo();
   return single_foo;
 }; 
 int dummy(){ return 3;};
friend class Bar;
};

class Bar{
private:
 static Bar* single_bar;
 Bar();
public:
 static Bar* instance(){
    if( single_bar == NULL )
        single_bar = new Bar();
    return single_bar;
 };
 void func();
friend class BFoo;
};

Foo* Foo::single_foo = NULL;
Bar* Bar::single_bar = NULL;

......
......
void Bar::func()
{
   Foo* myInspect = Foo::instance();
   numsend = myInspect->dummy();
};


While compiling func() , I get an error saying:

1
2
3
error: expression must have class type
   numsend = myInspect->dummy();
             ^


I am totally stumped, could some one see the reason why this is happening?
Last edited on
I don't know, but two things:

What is numsend?
And you need to initialize Foo::single_foo to NULL, otherwise your program is going to explode when you run it anyways.
numsend is just some integer, and I have initialized Foo:single_foo to NULL;

Foo* Foo::single_foo = NULL

Last edited on
You normally don't initialize private class members outside of a class.
I am trying to implement Foo and Bar as singleton classes ( hence the static pointer to the same class and private constructor ). You cannot initialize a member of a class in the class definition unless it is a const. This is the way to do it, I have done it before.
Funny. Never used singletons in C++ before, so I sort of missed out on that one. Yeah, anyways. Frankly, I am stumped aswell. Maybe some of your other code is messing with you, this works just fine for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define NULL 0
class Foo{
private:
 static Foo* single_foo;
public:
 static Foo* instance(){
   if ( single_foo == NULL )
     single_foo = new Foo();
   return single_foo;
 }; 
 int dummy(){ return 3;};
};
Foo* Foo::single_foo = NULL;
int main()
{
int numsend;
Foo* myInspect = Foo::instance();
   numsend = myInspect->dummy();
   return 0;
}
Yup,
I was some other part of the code. Sorry about that. I am going to report this thread. I was working on a more complicated class structure and the error was buried deep within :)

Thanks hanst99 for your quick responses.
Topic archived. No new replies allowed.