简体   繁体   English

类访问结构

[英]Access of class to struct

In order to do apply operations on point M(x,y) , I've defined a class POINT2D.h : 为了对点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

and a struct Point in the main: 和主要的结构Point

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

When I run it I get an error in the class, saying "Point does not name a type". 当我运行它时,我在类中得到一个错误,说“点不命名类型”。 What is the problem? 问题是什么?

In C++ before you can use an type it must be declared or defined. 在C ++中,必须先声明或定义它,然后才能使用它。 So since POINT2D does not know about the Point struct you declared in your main.cpp you cannot use it. 因此,由于POINT2D不了解您在main.cpp中声明的Point结构,因此无法使用它。 You can forward declare Point or you move Point into its own header file and then include it in POINT2D.h and main . 您可以转发 Point 声明,也可以将Point移动到其自己的头文件中,然后将其包含在POINT2D.hmain

You also have an issue with POINT2D as your constructors are both private. POINT2D也有问题,因为构造函数都是私有的。 You should add public: before them or move them into the public section that is already in your class. 您应该在它们前面添加public:或将它们移到班级中已经存在的public部分中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM