简体   繁体   English

C ++错误:“ double”之前的预期主表达式

[英]C++ Error: expected primary-expression before 'double'

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. 我需要编写一个计算两个点之间的距离的类程序-Point.cpp,以及另一个调用我的Point.cpp程序以计算两个点之间的长度(基本上是从Point.cpp的距离)和斜率的类程序-LineSegment。 CPP。 I tested my Point.cpp separately and it runs fine. 我分别测试了我的Point.cpp,它运行正常。 But when I run everything together (in conjunction with the LineSegment.cpp), I get the following error message: 但是,当我一起运行所有内容(与LineSegment.cpp结合使用)时,出现以下错误消息:

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). 我已经用注释标记了错误消息引用的4行(每次我用行号发布代码时,我都会收到建议的编辑以删除行号,因此这次我不包括行号)。

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. 我的猜测是我从Point.cpp中错误地调用了函数,但是我的教科书没有告诉我如何从嵌套类中调用函数。 Any guidance would be appreciated. 任何指导将不胜感激。 Thank you all for your time! 谢谢大家的时间!

LineSegment.hpp 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 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 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 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);
}
 p2.setXCoord(double); // <-- error p2.setYCoord(double); // <-- error 

double is a type, not a valid actual parameter name. double是类型,不是有效的实际参数名称。

Call those functions with a value or a variable that is double or at least convertible to double . 用一个值或一个为double或至少可转换为double的变量调用这些函数。

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. double是double类型的保留关键字,它不能作为参数传递给函数,您需要转发Point类型的实际值。

void LineSegment::setEnd1(Point p1) {
   p1.setXCoord(p1.getXCoord());   
   p1.setYCoord(p1.getYCoord());   
}

double is a type specifier not an object. double是类型说明符,不是对象。 And why does for example setEnd1 call itself? 例如,为什么setEnd1自己调用呢?

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 至少在Point类中,所有这些成员函数都应使用限定符const声明

double getXCoord() const;
double getYCoord() const;
double distanceTo(const Point&) const;
void LineSegment::setEnd1(Point p1) {
   p1.setXCoord(double);       // <-- error
   p1.setYCoord(double);       // <-- error
   setEnd1(p1);
}

LineSegment's set functions each take one argument of class Point. LineSegment的set函数每个都使用Point类的一个参数。 So when you give it a variable of type Point, you must save it as point. 因此,当给它一个Point类型的变量时,必须将其保存为point。

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

This bit of your code is the template for what you want to do. 这部分代码是您想要做什么的模板。 Instead of type double, you want type Point for p1 and whatever the destination is called. 而不是double类型,您要为p1和任何要调用的目标键入Point。

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

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