没有类类型 C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14143967/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
does not have class type C++
提问by JaredC
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
main.cpp:174: 错误:'((Scene*)this)->Scene::lake' 没有类类型
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;
}
};
回答by JaredC
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.
如果您只是想声明一个Lake
对象,那么您可能想要删除(int L)
. 您所拥有的是声明一个lake
返回 aLake
并接受 anint
作为参数的函数。
If you're trying to pass in L when constructing your lake
object, then I think you want your code to look like this:
如果您在构建lake
对象时尝试传入 L ,那么我认为您希望您的代码如下所示:
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 hereand here.
注意添加到构造函数中的 4 行。这称为成员初始化,以及如何构造成员变量。在此常见问题解答中阅读更多相关信息。或者我在这里和这里找到的其他一些花絮。
回答by Stuart Golodetz
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.
您声明lake
为一个方法,它接受一个参数并返回一个Lake
. 然后,您尝试通过lake.light_up()
. 这会导致您观察到的错误。
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.
为了解决这个问题,你要么需要声明lake
为一个变量,例如Lake lake;
,要么你需要停止尝试调用它的方法。
回答by Jerry Coffin
You've declared (but never defined) lake
as a member function of Scene:
您已声明(但从未定义)lake
为 Scene 的成员函数:
class Scene
{
// ...
Lake lake(int L);
But then in plot
, you try to use lake
as if it were a variable:
但是在 中plot
,您尝试将lake
其用作变量:
int plot()
{
lake.light_up();
回答by Ayush Jindal
Replace the line Lake lake(int L);
with Lake lake= Lake(L);
or with this: Lake lake{L};
Lake lake(int L);
用Lake lake= Lake(L);
或替换该行:Lake lake{L};