18

This is one class from my program! When I'm trying to compile the whole program, I get an error message like this:

main.cpp:174: error: '((Scene*)this)->Scene::lake' does not have class type

The source

class Scene
{
    int L,Dist;
    Background back ;
    Lake lake(int L);
    IceSkater iceskater(int Dist);
public :
    Scene(int L, int Dist)
    {
        cout<<"Scene was just created"<<endl;
    }

    ~Scene()
    {
        cout<<"Scene is about to be destroyed !"<<endl;
    }
};
Beryllium
  • 12,808
  • 10
  • 56
  • 86
  • 5
    lake is a method, not a variable Lake lake(int L); lake.light_up(); //This not make sense – DGomez Jan 03 '13 at 17:27
  • Lake is a class and I'n trying to create her object lake in class scene –  Jan 03 '13 at 17:37
  • Sure is a class, but lake(in lowcase) is a method, and you're tryng to call a method of that object, this line is wrong, lake.light_up(); – DGomez Jan 03 '13 at 17:39
  • 1
    As a heads up to people having this error in a different situation: I encountered this when converting a `QVariant` type to an `std::string` like this: `x.toString.toStringStd()`. I forgot the `()` at the end of `x.toString()`. This cost me quite a few hairs on my head. – Zimano May 05 '17 at 11:36

4 Answers4

17

Your problem is in the following line:

Lake lake(int L);

If you're just trying to declare a Lake object then you probably want to remove the (int L). What you have there is declaring a function lake that returns a Lake and accepts an int as a parameter.

If you're trying to pass in L when constructing your lake object, then I think you want your code to look like this:

class Scene
{
    int L,Dist;
    Background back ;
    Lake lake;
    IceSkater iceskater;
public :
    Scene(int L, int Dist) :
        L(L),     
        Dist(Dist),
        lake(L),
        iceskater(Dist)
    {
        cout<<"Scene was just created"<<endl;
    }
.....

Notice the 4 lines added to your constructor. This is called member initialization, and its how you construct member variables. Read more about it in this faq. Or some other tidbits I found here and here.

Community
  • 1
  • 1
JaredC
  • 5,150
  • 1
  • 20
  • 45
3

You declare lake as a method that takes one argument and returns a Lake. You then try and call a method on it via lake.light_up(). This causes the error you observe.

To solve the problem, you either need to declare lake to be a variable, e.g. Lake lake;, or you need to stop trying to call a method on it.

Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
1

You've declared (but never defined) lake as a member function of Scene:

class Scene
{
    // ...
    Lake lake(int L);

But then in plot, you try to use lake as if it were a variable:

int plot()
{
    lake.light_up();
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Replace the line Lake lake(int L); with Lake lake= Lake(L); or with this: Lake lake{L};