代码之家  ›  专栏  ›  技术社区  ›  GenerationLost

C++新手的类成员函数[重复]

  •  2
  • GenerationLost  · 技术社区  · 7 年前

    我写了一个简单的类来看看事情是如何以及是否工作的:

    class Point
    {
    private:
        int x;
        int y;
    
    public:
        Point(int arg1, int arg2)
        {
            x = arg1;
            y = arg2;
        }
    };
    

    我尝试了x和y的两个简单成员函数,使存储在x和y变量中的值加倍。

    首先我试了一下:

    void doubleX()
    {
        x *= 2;
    };
    
    void doubleY()
    {
        y *= 2;
    };
    

    void doubleX()
    {
        Point::x = 2 * Point::x;
    };
    
    void doubleY()
    {
        Point::y = 2 * Point2::y;
    };
    

    类定义。

    “错误C3867‘Point::doubleX’:非标准语法;使用'&'创建指向成员的指针”

    这个问题有什么快速的解决方案和解释吗?

    提前感谢!

        #include "stdafx.h"
    #include <iostream>
    
    using namespace std;
    
    class Point
    {
    public:
        int x;
        int y;
    
        Point(int arg1, int arg2)
        {
            x = arg1;
            y = arg2;
        }
    
        void doubleX()
        {
            x *= 2;
        };
    
        void doubleY()
        {
            y *= 2;
        };
    };
    
    int main()
    {
        Point p(1,1);
    
        int &x = p.x;
        int &y = p.y;
    
        cout << x << "|" << y;
    
        p.doubleX; p.doubleY; //error message here
    
        cout << x << "|" << y;
    
        cin.get();
    }
    
    4 回复  |  直到 7 年前
        1
  •  4
  •   Valy    7 年前

    也许你没有声明成员函数 类别定义?下面是一个基于您的类的完整工作示例:

    #include <iostream>
    
    
    class Point
    {
    private:
        int x;
        int y;
    
    public:
        Point(int arg1, int arg2)
        {
            x = arg1;
            y = arg2;
        }
    
        void doubleX()
        {
            x *= 2; /* or this->x *= 2; */
        }
    
        void doubleY()
        {
            y *= 2;
        }
    
        int getX()
        {
            return x;
        }
    
        int getY()
        {
            return y;
        }
    };
    
    
    int main()
    {
        Point p(2, 3);
    
        std::cout << "p.x = " << p.getX() << " | p.y = " << p.getY() << std::endl;
    
        p.doubleX();
        p.doubleY();
    
        std::cout << "p.x = " << p.getX() << " | p.y = " << p.getY() << std::endl;
    
        return 0;
    }
    

    你可以把这个放在一个 main.cpp g++ 它运行良好。

        2
  •  2
  •   duong_dajgja    7 年前

    瓦利给出的答案是正确的。但我想提醒大家,C++为您提供了另一种声明和定义方法的选择,即在类声明内声明方法,并在类声明外定义方法。这使您能够轻松地将接口和实现分离到 .h .cpp

    h点

    class Point
    {
    private:
        int x;
        int y;
    
    public:
        Point(int arg1, int arg2);
        void doubleX();
        void doubleY();
        int getX();
        int getY();
    };
    

    #include "Point.h"
    
    Point::Point(int arg1, int arg2)
    {
        x = arg1;
        y = arg2;
    }
    
    void Point::doubleX()
    {
        x *= 2;
    }
    
    void Point::doubleY()
    {
        y *= 2;
    }
    
    int Point::getX()
    {
        return x;
    }
    
    int Point::getY()
    {
        return y;
    }
    

    //点测试.cpp

    #include "Point.h"
    
    int main()
    {
        // Do something with Point here
        Point pt(1, 2);
    
        std::cout << "Original: (" << pt.getX() << ", " << pt.getY() << ")" << std::endl;
    
        pt.doubleX();
        pt.doubleY();
    
        std::cout << "After being doubled: (" << pt.getX() << ", " << pt.getY() << ")" << std::endl;
    
        return 0;
    }
    

    g++ -o PointTest PointTest.cpp Point.cpp
    
        3
  •  1
  •   CJ_macar    7 年前

    由于声誉问题,无法发表评论,但如果您尝试调用,vc++似乎会输出您所说的错误消息

    Point::doubleX
    

    下面是输出的一个实时示例: http://rextester.com/ZLCEW66682

    您应该创建类的实例,并使用paren调用函数

        4
  •  -2
  •   AdityaG    7 年前

    在第二组函数中

    void doubleX()
    {
        Point2::x = 2 * Point2::x;
    };
    
    void doubleY()
    {
        Point2::y = 2 * Point2::y;
    };
    

    Point::y …这不是您访问成员数据的方式。只有静态成员变量可以这样访问。正确的方法是

    void doubleX()
    {
        this->x = 2 * this->x;
    };
    
    void doubleY()
    {
        this->y = 2 * this->y;
    };
    

    this