没有类类型C ++ [英] does not have class type C++

查看:74
本文介绍了没有类类型C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我程序中的一门课!当我尝试编译整个程序时,出现如下错误消息:

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:错误:'(((Scene *)this)-> Scene :: lake'没有类类型

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

来源

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;
    }
};

推荐答案

您的问题在以下行中:

Lake lake(int L);

如果您只是想声明一个 Lake 对象,那么您可能要删除(int L).您所拥有的就是声明一个函数 lake ,该函数返回一个 Lake 并接受一个 int 作为参数.

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.

如果您在构造 lake 对象时尝试传递L,那么我认为您希望代码看起来像这样:

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;
    }
.....

注意添加到构造函数的4行.这称为成员初始化,及其构造成员变量的方式.在此常见问题解答中了解有关此内容的更多信息.或我在此处

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.

这篇关于没有类类型C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆