尝试为从类继承的子类编写构造函数,猜测语法,预期主表达式错误?
我有一个继承自 Shape 类的 Sphere 类(用于家庭作业项目):
在 Shape 中,我有三个构造函数。 Shape.h 中的声明如下:
Shape();
Shape(Vector);
Shape(Vector, float[]);
在 Sphere 中,我的构造函数继承自这些构造函数。我的 Sphere.h 文件中的声明如下:
Sphere(): Shape() {}//line 17
Sphere(Vector, float): Shape(Vector) {}//line 18
Sphere(Vector, float, float[]): Shape(Vector, float[]) {}//line 19
我这里的语法主要基于查看模板。虽然我的第一语言是 C++,但不幸的是,我只用 Java 教授了其他概念,例如继承。
无论如何,我在“make”时收到以下错误消息:
Sphere.h: In constructor ‘Sphere::Sphere(Vector, float)’:
Sphere.h:18: error: expected primary-expression before ‘)’ token
Sphere.h: In constructor ‘Sphere::Sphere(Vector, float, float*)’:
Sphere.h:19: error: expected primary-expression before ‘,’ token
Sphere.h:19: error: expected primary-expression before ‘float’
您能帮助我理解这些消息以及可能导致它们的原因吗?我首先尝试让它们以典型的方式表达,即,而不是
Sphere(): Shape();
然后在 .cc 文件中描述构造函数本身,我按照我在一些在线教程中看到的那样做了,但没有真正理解原因:
Sphere(): Shape() {}
这没有改变无论如何,问题仍然存在。感谢您的帮助!
I have a class Sphere that inherits from class Shape (for a homework project):
Within Shape I have three constructors. The declarations from Shape.h are as follows:
Shape();
Shape(Vector);
Shape(Vector, float[]);
Within Sphere my constructors inherit from these constructors. The declarations in my Sphere.h file are as follows:
Sphere(): Shape() {}//line 17
Sphere(Vector, float): Shape(Vector) {}//line 18
Sphere(Vector, float, float[]): Shape(Vector, float[]) {}//line 19
My syntax here is based largely on looking at templates. While my first language was C++, I was unfortunately taught other concepts, like inheritance, only in Java.
Anyway, I have the following error messages upon `make':
Sphere.h: In constructor ‘Sphere::Sphere(Vector, float)’:
Sphere.h:18: error: expected primary-expression before ‘)’ token
Sphere.h: In constructor ‘Sphere::Sphere(Vector, float, float*)’:
Sphere.h:19: error: expected primary-expression before ‘,’ token
Sphere.h:19: error: expected primary-expression before ‘float’
Can you help me understand these messages and what might be causing them? I first tried letting them be expressed in the typical way, i.e., instead of
Sphere(): Shape();
and then describing the constructor itself in the .cc file, I did as I had seen done in some online tutorials, without really understanding why:
Sphere(): Shape() {}
This didn't change anything, the problem remained. Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要为参数指定名称,而不仅仅是类型,并传递名称,而不是类型。例如:
You need to specify names, not just types, for the parameters, and pass the names, not the types. For example:
您还没有为构造函数参数指定任何名称。
没关系,只要您实际上不想使用这些参数即可!
You haven't given your constructor arguments any names.
This is ok, so long as you don't actually want to use those arguments!
您的初始化列表属于构造函数的实现,而不是构造函数的声明(或原型)的一部分。你似乎把它和两者放在一起。
您可以执行以下操作:
或者您可以执行以下操作:
Your initialization list belongs in the constructor's implementation and isn't part of the constructor's declaration (or prototype). You seem to be putting it with both.
You can either do:
Or you can do: