2012-10-19 15 views
1

我想问问题在哪里?

我得到了一个错误:的“一元*”C++段线路口

我在C++编程新的,我用Java的方式无效的类型参数。 指针和取消引用对我来说是个大问题。

我的应用程序得到了输入值并保存为点对象,此后我应该找到2行的交集。

我以为要返回一个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(); 

} 

和我通话功能以这种方式

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); 

} 
+0

你得到了什么错误? – TZHX

+0

这真的是你使用的核心吗?你的函数在'Triungle'类中,但你的main()函数使用'Triangle'。请粘贴您正在使用的_exact_代码,因为您可能在某处没有粘贴导致问题的错误。 – bdonlan

+0

另外,不需要'
'。只需选择您的代码并点击工具栏中的“{}”按钮即可。 – bdonlan

回答

1
intersection->member 

将取消引用指针intersection。这与

(*intersection).member 

你不需要两次取消引用它。

0

假设你的错误是在两行编译错误:

*intersection->x_val = x; // here i got error 
*intersection->y_val = y; // here i got error 

的问题是,你是一个指针,然后使用derefencing操作->它引用去。

相反,你应该要么:

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

*intersection.x_val = x; 
*intersection.y_val = y; // use it as an object 
1

你在你的代码

*intersection->x_val = x; 

做的是一个相当于

(*(intersection->x_val)) = x; 

,因为通过指针->进行选择的运算符比取消引用运算符*具有更高的precedence,并且后者具有比赋值运算符=更高的优先级。

因此首先您的选择会员double x_val类别Point。 其次,您尝试将一元解引用运算符*应用于结果。并且由于x_val是double,而不是由解引用运算符期望的指针,编译器会报告错误。

因此,引用操作过度这里,它是足够做以下

intersection->x_val = x;