c++ - 在 C++ 中创建和初始化类

标签 c++ class

我正在学习用 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/

相关文章:

c++ - SQLite 函数调用都有无效参数

c++ - 使用 STL find_if() 在对象指针 Vector 中查找特定对象

android - 用于手机文件共享的 USB 类设备

javascript - 为什么JS类里面声明的变量是 `undefined`

c++ - 在 C++ 的派生类构造函数中从基模板类访问变量

jQuery:对列表项进行排序但保留类

c++ - 游戏实体更新方法

c++ - 如何显示成对的模板化 vector ? [解决了]

c++ - Visual Studio 标题

c++ - 文件重定向到程序