我正在学习用 C++ 创建类,我已经创建了一个简单的 Point 类。它以某种方式无法编译,我不知道出了什么问题。请帮忙。
点.h
#ifndef POINT_H
#define POINT_H
class Point {
private:
float x, y;
public:
//default constructor
Point();
//constructor
Point(float x, float y);
float getX();
float getY();
void print();
};
#endif
点.cpp
#include "Point.h"
Point::Point(){
x = 0.0;
y = 0.0;
};
Point::Point(float x, float y){
x = x;
y = y;
}
float Point::getX(){
return x;
}
float Point::getY(){
return y;
}
void Point::print(){
cout << "hello" ;
{
主要.cpp:
#include <Point.h>
#include <iostream>
int main()
{
Point p(10.0f, 20.0f);
p.print();
return 0;
}
下面是构建信息:
||=== Build: Debug in Point (compiler: GNU GCC Compiler) ===|
main.cpp|7|error: no matching function for call to 'Point::Point(float, float)'|
main.cpp|8|error: 'class Point' has no member named 'print'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
最佳答案
在定义正文时,您忘记将 Point::
放在 print
前面。此外,构造函数中的 x = x
不会执行任何操作。您需要分配给 this->x
,对于 y 也是如此。
关于c++ - 在 C++ 中创建和初始化类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33325138/