C++ Error: expected primary-expression before 'double'

时间:2015-10-30 23:22:04

标签: c++

I need to write a class program that calculates the distance between two points - Point.cpp, and another class program that calls my Point.cpp program to calculate the length(basically distance from Point.cpp) and slope between two points - LineSegment.cpp. I tested my Point.cpp separately and it runs fine. But when I run everything together (in conjunction with the LineSegment.cpp), I get the following error message: LineSegment.cpp: In member function ‘void LineSegment::setEnd1(Point)’: LineSegment.cpp:31: error: expected primary-expression before ‘double’ LineSegment.cpp:32: error: expected primary-expression before ‘double’ LineSegment.cpp: In member function ‘void LineSegment::setEnd2(Point)’: LineSegment.cpp:37: error: expected primary-expression before ‘double’ LineSegment.cpp:38: error: expected primary-expression before ‘double’ My codes are below. I have marked the 4 lines that the error message refers to with a comment (everytime I posted my code with line numbers I received a suggested edit to take the line numbers out, so I did not include line numbers this time). My guess is that I am calling the functions from Point.cpp incorrectly, but my textbook does not tell me how to call functions from a nested class. Any guidance would be appreciated. Thank you all for your time! LineSegment.hpp #ifndef LINESEGMENT_HPP #define LINESEGMENT_HPP #include "Point.hpp" class LineSegment { private: Point p1; Point p2; public: LineSegment(Point, Point); void setEnd1(Point p1); void setEnd2(Point p2); Point getEnd1(); Point getEnd2(); double length(); double slope(); }; #endif LineSegment.cpp #include <iostream> #include <cmath> #include "LineSegment.hpp" //constructor LineSegment::LineSegment(Point p1, Point p2) { setEnd1(p1); setEnd2(p2); } //set and get points void LineSegment::setEnd1(Point p1) { p1.setXCoord(double); // <-- error p1.setYCoord(double); // <-- error setEnd1(p1); } void LineSegment::setEnd2(Point p2) { p2.setXCoord(double); // <-- error p2.setYCoord(double); // <-- error setEnd2(p2); } Point LineSegment::getEnd1() { return p1; } Point LineSegment::getEnd2() { return p2; } //calculations double LineSegment::length() { return p1.distanceTo(p2); } double LineSegment::slope() { return(p2.getYCoord()-p1.getYCoord())/p2.getXCoord()-p1.getXCoord(); } Point.hpp #ifndef POINT_HPP #define POINT_HPP class Point { private: double xCoord; double yCoord; public: Point(); Point(double x1, double y1); void setXCoord(double x1); void setYCoord(double y1); double getXCoord(); double getYCoord(); double distanceTo(const Point&); }; #endif Point.cpp #include <cmath> #include <iostream> #include "Point.hpp" //default constructor Point::Point() { xCoord = 0.0; yCoord = 0.0; } //constructor Point::Point(double x, double y) { xCoord = x; yCoord = y; } //get and set functions void Point::setXCoord(double x) { xCoord = x; } void Point::setYCoord(double y) { yCoord = y; } double Point::getXCoord() { return xCoord; } double Point::getYCoord() { return yCoord; } // calculate distance double Point::distanceTo(const Point& p2) { double dx = p2.xCoord - xCoord; double dy = p2.yCoord - yCoord; return sqrt(dx * dx + dy * dy); }

4 个答案:

答案 0 :(得分:1)

p2.setXCoord(double); // <-- error p2.setYCoord(double); // <-- error double is a type, not a valid actual parameter name. Call those functions with a value or a variable that is double or at least convertible to double.

答案 1 :(得分:1)

double is a reserved keyword for the double type, it can't be passed to a function as an argument, you need to forward the actual values from the Point type. void LineSegment::setEnd1(Point p1) { p1.setXCoord(p1.getXCoord()); p1.setYCoord(p1.getYCoord()); }

答案 2 :(得分:1)

double is a type specifier not an object. And why does for example setEnd1 call itself? It seems you mean the following //set and get points void LineSegment::setEnd1(Point p1) { this->p1.setXCoord(p1.getXCoord()); // <-- error this->p1.setYCoord(pq.getYCoord()); // <-- error } void LineSegment::setEnd2(Point p2) { this->p2.setXCoord(p2.getXCoord()); // <-- error this->p2.setYCoord(p2.getXCoord()); // <-- error } You could write even simpler //set and get points void LineSegment::setEnd1(Point p1) { this->p1 = p1; } void LineSegment::setEnd2(Point p2) { this->p2 = p2; } And at least in the class Point all these member functions should be declared with qualifier const double getXCoord() const; double getYCoord() const; double distanceTo(const Point&) const;

答案 3 :(得分:1)

void LineSegment::setEnd1(Point p1) {
   p1.setXCoord(double);       // <-- error
   p1.setYCoord(double);       // <-- error
   setEnd1(p1);
}

LineSegment的set函数每个都接受Point类的一个参数。所以当你给它一个Point类型的变量时,你必须将它保存为point。

void Point::setXCoord(double x) {
   xCoord = x;
}

您的代码的这一部分是您想要做的事情的模板。您需要为p1键入Point,而不是调用目标。

。而不是类型double