C++ C++;线段交点

C++ C++;线段交点,c++,C++,我想问问题出在哪里? 我得到一个错误:“一元*”的类型参数无效。 我是C++编程新手,我使用java风格。 指针和解引用对我来说是个大问题 我的应用程序得到了输入值并保存为point对象,在此之后,我应该找到两条线的交点 我想返回一个点对象,我将在其中计算x和y值 .h文件 class Point { public: double x_val, y_val; Point(double, double); double x(); double y();

我想问问题出在哪里?

我得到一个错误:“一元*”的类型参数无效。

我是C++编程新手,我使用java风格。 指针和解引用对我来说是个大问题

我的应用程序得到了输入值并保存为point对象,在此之后,我应该找到两条线的交点

我想返回一个点对象,我将在其中计算x和y值

.h文件

class Point {
public:
    double x_val, y_val;

    Point(double, double);

    double x();
    double y();

    double dist(Point other);

    Point add(Point b);
    Point sub(Point b);

    void move(double a, double b);



};



 class Triungle {
public:


        Triungle(std::string);
        void compute_length();
        void lines_intersect(Point a, Point b, Point c, Point d, Point *intersection);

        Point a, b, c;
};
.cpp文件

Point::Point(double x = 0.0, double y = 0.0) {
    x_val = x;
    y_val = y;
}

double Point::x() {
    return x_val;
}

double Point::y() {
    return y_val;
}

double Point::dist(Point other) {
    double xd = this->x() - other.x();
    double yd = this->y() - other.y();
    return sqrt(xd * xd + yd * yd);
}

Point Point::add(Point b) {
    return Point(x_val + b.x_val, y_val + b.y_val);
}

Point Point::sub(Point b) {
    return Point(x_val - b.x_val, y_val - b.y_val);
}

void Point::move(double a, double b) {
    x_val += a;
    y_val += b;
}

    void Triungle::lines_intersect(Point a, Point b, Point c, Point d, Point *intersection) {

        double x, y;
        double A1 = b.y_val - a.y_val;
        double B1 = b.x_val - a.x_val;
        double C1 = a.y_val - (A1 / B1) * a.x_val;

        double A2 = d.y_val - c.y_val;
        double B2 = d.x_val - c.x_val;
        double C2 = c.y_val - (A2 / B2) * c.x_val;

        double det = (A1 / B1) - (A2 / B2);
        if (det == 0) {
            // lines are paralel
        } else {
            x = (C2 - C1) / det;
            y = (A1 * C2 - A2 * C1) / det;
        }
        *intersection->x_val = x;  // here i got error
        *intersection->y_val = y;  // here i got error
       }

Triungle::Triungle(std::string s) {

    cout << "enter first point of " << s << " triangle: ";
    cin >> a.x_val;
    cin >> a.y_val;
    if (!(cin)) {
        cout << "input error." << endl;
        exit(1);
    }
    cin.clear();
    cout << "enter second point of " << s << " triangle: ";
    cin >> b.x_val;
    cin >> b.y_val;
    if (!(cin)) {
        cout << "input error." << endl;
        exit(1);
    }
    cin.clear();
    cout << "enter 3 point of " << s << " triangle: ";
    cin >> c.x_val;
    cin >> c.y_val;
    if (!cin) {
        cout << "input error." << endl;
        exit(1);
    }
    cin.clear();

}
将取消引用指针
交点
。这和

(*intersection).member

您不需要取消引用两次。

假设您得到的错误是两行上的编译错误:

*intersection->x_val = x;  // here i got error 
*intersection->y_val = y;  // here i got error 
问题是您正在取消引用指针,然后对其使用解除限制运算符
->

相反,您应该:

intersection->x_val = x; 
intersection->y_val = y;  // leave it as a pointer


您在代码中做了什么

*intersection->x_val = x;
相当于

(*(intersection->x_val)) = x;
因为通过指针选择的运算符
->
的优先级高于取消引用运算符
*
,后者的优先级高于赋值运算符
=

因此,首先选择类
的成员
double x_val
。 其次,尝试对结果应用一元解引用运算符
*
。由于x_val是双精度的,而不是去引用操作符所期望的指针,所以编译器会报告一个错误

因此,这里的解引用运算符过多,只需执行以下操作即可

intersection->x_val = x;

这真的是你用的同一个核心吗?您的函数在类
Triungle
中,但您的
main()
函数使用
Triangle
。请粘贴您正在使用的确切代码,因为可能是您没有粘贴的地方有一个打字错误导致了问题。另外,不需要

。只需选择您的代码并单击工具栏中的
{}
按钮。
*intersection->x_val = x;
(*(intersection->x_val)) = x;
intersection->x_val = x;