Reputation: 21
i would like to ask where is the problem?
I got an error: invalid type argument of ‘unary *’
I am new in c++ programming, i use java style. Pointer and dereference is a big problem for me.
My application got input values and save as point object, after this i should find intersection of 2 lines.
I thought to return a Point object in which i will compute x and y value.
.h file
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 file
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();
}
and i call function in this way
int main(int argc, char** argv) {
Triungle a("first");
Triungle b("second");
Point p;
a.lines_intersect(a.a, a.b, a.c, a.a, &p);
}
Upvotes: 1
Views: 676
Reputation: 6339
What you do in your code
*intersection->x_val = x;
is an equivalent to
(*(intersection->x_val)) = x;
because operator of selection through pointer ->
has higher precedence than dereference operator *
, and the latter has higher precedence than assignment operator =
.
So first your select member double x_val
of class Point
.
Second you try to apply unary dereference operator *
to the result. And because x_val is double, not a pointer, which is expected by dereference operator, compiler reports an error.
Thus, dereference operator is excessive here and it's enough to do the following
intersection->x_val = x;
Upvotes: 1
Reputation: 5398
Assuming that the error you get is a compilation error on the two lines:
*intersection->x_val = x; // here i got error
*intersection->y_val = y; // here i got error
The problem is that you are de-referencing a pointer, and then using the derefencing operator ->
on it.
Instead, you should either do:
intersection->x_val = x;
intersection->y_val = y; // leave it as a pointer
or
*intersection.x_val = x;
*intersection.y_val = y; // use it as an object
Upvotes: 0
Reputation: 20151
intersection->member
will dereference a pointer intersection
. This is the same as
(*intersection).member
you don't need to dereference it twice.
Upvotes: 1