does not have class type C++

前端 未结 4 947
眼角桃花
眼角桃花 2021-02-02 13:28

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)

相关标签:
4条回答
  • 2021-02-02 14:11

    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.

    0 讨论(0)
  • 2021-02-02 14:11

    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();
    
    0 讨论(0)
  • 2021-02-02 14:28

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

    0 讨论(0)
  • 2021-02-02 14:30

    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.

    0 讨论(0)
提交回复
热议问题