类对结构的访问权限

Access of class to struct

本文关键字:访问权 权限 访问 结构      更新时间:2023-10-16

为了对点M(x,y)执行应用操作,我定义了一个类POINT2D.h:

#ifndef POINT2D_H_INCLUDED
#define POINT2D_H_INCLUDED
class POINT2D {
    POINT2D ();                    //first constructor 
    POINT2D(double x,double y);    // second constructor
private:
    Point M, PointImage;
public:
    void DeclarerM(){
        std::cout << "Entrer les composantes du point M : " << " ";
        std::cin >> M.x >> M.y;
    }
    Point Trnaslation(Point M);         //Functions applied on Point
    Point Rotation(Point M);
    Point SymetrieAxiale (Point M);
    Point Homothetie(Point M);
};
#endif // POINT2D_H_INCLUDED

和一个结构体CCD_ 3:

struct Point{             //structure Point
    double x;                 //the coordinates of the Point
    double y;
};

当我运行它时,我在类中得到一个错误,说"Point没有命名类型"。问题出在哪里?

在C++中,在使用类型之前,必须声明或定义它。因此,由于POINT2D不知道您在main.cpp中声明的Point结构,因此您无法使用它。您可以转发声明Point,也可以将Point移动到其自己的头文件中,然后将其包含在POINT2D.hmain中。

POINT2D也有问题,因为您的构造函数都是私有的。您应该在它们之前添加public:,或者将它们移到类中已经存在的public部分中。