继承的简单实现中构建错误的来源 "expected primary-expression before 'float' "
Source of build error "expected primary-expression before 'float' " in simple implementation of inheritance
我似乎无法找到生成错误的原因
expected primary-expression before "float"
在此实现中...
using namespace std;
class Point{
public:
Point(float X = 0.0, float Y = 0.0);
void set(float X, float Y);
void setX(float X);
void setY(float Y);
void get(float * P_x, float * P_y);
float getX();
float getY();
float * pX();
float * pY();
SDL_Point returnSDL();
private:
float x;
float y;
};
class Vector : public Point{
public:
Vector(float X = 0.0, float Y = 0.0);
};
///The errors occur in this constructor...
Vector::Vector(float X, float Y) : Point(float X, float Y){
}
我仍在学习 类 的优点,如果有任何帮助,我将不胜感激。我知道它与继承有关,因为当 Vector
不继承 Point
时,程序会正常构建。据我所知,这是继承的正确语法和实现。我找到的 Web 帮助目前无法回答。
您混淆了函数的声明和使用。当你声明一个函数时,你需要告诉编译器参数的类型是什么。
Vector::Vector(float X, float Y)
现在在成员初始化部分你有
: Point(float X, float Y)
您在这里向函数调用添加类型,这不是您想要做的。当你调用一个函数时,你只需将 values/variables 传递给它。
: Point( X, Y)
^ ^ no type here as we just pass X and Y to the Point constructor.
///The errors occur in this constructor...
Vector::Vector(float X, float Y) : Point(float X, float Y){
}
这段代码中有两个相似的结构:
Vector::Vector(float X, float Y)
和 : Point(float X, float Y)
:
- 第一个(
Vector::Vector(float X, float Y)
)是classVector
的构造函数的声明;
- 另一个(
: Point(float X, float Y)
)是一个函数调用;调用 class Point
的构造函数;注意引入成员初始值设定项列表的冒号 (:
)。
现在,如果您看到两者之间的区别(function/method 声明或定义与 function/method 调用),您可以自己找到错误:编译器期望参数中的表达式而不是类型Point::Point()
构造函数的调用列表。
// Look, ma! No errors!
Vector::Vector(float X, float Y) : Point(X, Y) {
}
有关详细信息,请查看有关 constructors and member initializer lists 的文档页面。
我似乎无法找到生成错误的原因
expected primary-expression before "float"
在此实现中...
using namespace std;
class Point{
public:
Point(float X = 0.0, float Y = 0.0);
void set(float X, float Y);
void setX(float X);
void setY(float Y);
void get(float * P_x, float * P_y);
float getX();
float getY();
float * pX();
float * pY();
SDL_Point returnSDL();
private:
float x;
float y;
};
class Vector : public Point{
public:
Vector(float X = 0.0, float Y = 0.0);
};
///The errors occur in this constructor...
Vector::Vector(float X, float Y) : Point(float X, float Y){
}
我仍在学习 类 的优点,如果有任何帮助,我将不胜感激。我知道它与继承有关,因为当 Vector
不继承 Point
时,程序会正常构建。据我所知,这是继承的正确语法和实现。我找到的 Web 帮助目前无法回答。
您混淆了函数的声明和使用。当你声明一个函数时,你需要告诉编译器参数的类型是什么。
Vector::Vector(float X, float Y)
现在在成员初始化部分你有
: Point(float X, float Y)
您在这里向函数调用添加类型,这不是您想要做的。当你调用一个函数时,你只需将 values/variables 传递给它。
: Point( X, Y)
^ ^ no type here as we just pass X and Y to the Point constructor.
///The errors occur in this constructor... Vector::Vector(float X, float Y) : Point(float X, float Y){ }
这段代码中有两个相似的结构:
Vector::Vector(float X, float Y)
和 : Point(float X, float Y)
:
- 第一个(
Vector::Vector(float X, float Y)
)是classVector
的构造函数的声明; - 另一个(
: Point(float X, float Y)
)是一个函数调用;调用 classPoint
的构造函数;注意引入成员初始值设定项列表的冒号 (:
)。
现在,如果您看到两者之间的区别(function/method 声明或定义与 function/method 调用),您可以自己找到错误:编译器期望参数中的表达式而不是类型Point::Point()
构造函数的调用列表。
// Look, ma! No errors!
Vector::Vector(float X, float Y) : Point(X, Y) {
}
有关详细信息,请查看有关 constructors and member initializer lists 的文档页面。