尝试为从类继承的子类编写构造函数,猜测语法,预期主表达式错误?

发布于 2024-12-05 12:44:55 字数 1070 浏览 0 评论 0原文

我有一个继承自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧伤慢歌 2024-12-12 12:44:55

您需要为参数指定名称,而不仅仅是类型,并传递名称,而不是类型。例如:

Sphere(Vector a, float b, float[] c): Shape(a, c) {}

You need to specify names, not just types, for the parameters, and pass the names, not the types. For example:

Sphere(Vector a, float b, float[] c): Shape(a, c) {}
清眉祭 2024-12-12 12:44:55

您还没有为构造函数参数指定任何名称。

没关系,只要您实际上不想使用这些参数即可!

You haven't given your constructor arguments any names.

This is ok, so long as you don't actually want to use those arguments!

等数载,海棠开 2024-12-12 12:44:55

您的初始化列表属于构造函数的实现,而不是构造函数的声明(或原型)的一部分。你似乎把它和两者放在一起。

您可以执行以下操作:

// Sphere.h
struct Sphere {
  Sphere();
};

// Sphere.cpp
Sphere::Sphere() : Shape() {

}

或者您可以执行以下操作:

// Sphere.h
struct Sphere {
  Sphere() : Shape() { }
};

// Sphere.cpp
// No constructor here; you defined it in the header.

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:

// Sphere.h
struct Sphere {
  Sphere();
};

// Sphere.cpp
Sphere::Sphere() : Shape() {

}

Or you can do:

// Sphere.h
struct Sphere {
  Sphere() : Shape() { }
};

// Sphere.cpp
// No constructor here; you defined it in the header.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文