[英]error: expected constructor, destructor, or type conversion before ‘(’ token. Even though I have a constructor?
I am trying to compile my LineADT.cpp file but keep getting this error: 我正在尝试编译LineADT.cpp文件,但始终收到此错误:
error: expected constructor, destructor, or type conversion before ‘(’ token
LineT::LineT(PointT::PointT st, MapTypes::CompassT ornt, unsigned int l) {
My LineADT.cpp: 我的LineADT.cpp:
#include "MapTypes.h"
#include "PointADT.h"
#include "LineADT.h"
LineT::LineT(PointT::PointT st, MapTypes::CompassT ornt, unsigned int l) { //Error
this->s = st;
this->o = ornt;
this->L = l;
}
My LineADT.h: 我的LineADT.h:
#ifndef LINET_H
#define LINET_H
#include "MapTypes.h"
#include "PointADT.h"
class LineT {
private:
PointT s;
MapTypes::CompassT o;
unsigned int L;
public:
LineT (PointT st, MapTypes::CompassT ornt, unsigned int l);
};
#endif
My PointADT.h: 我的PointADT.h:
#ifndef POINTT_H
#define POINTT_H
class PointT {
private:
double xc;
double yc;
public:
PointT (double x, double y);
};
#endif
My maptypes.h: 我的maptypes.h:
#ifndef MAPTYPES_H
#define MAPTYPES_H
class MapTypes {
public:
enum CompassT {N, S, E, W};
enum LandUseT {Recreational, Transport, Agricultural, Residential, Commercial};
enum RotateT {CW, CCW};
};
#endif
What I don't understand is why the compiler doesn't recognize that the line is a constructor (at least what I think). 我不明白的是为什么编译器无法识别该行是构造函数(至少我认为是这样)。
Two problems. 两个问题。
First: 第一:
LineT::LineT(PointT::PointT st, MapTypes::CompassT ornt, unsigned int l) {
// ^^^^^^^^
Nope. 不。
LineT::LineT(PointT st, MapTypes::CompassT ornt, unsigned int l) {
Second: PointT
has no default constructor, so you must initialise it, not merely assign to it later. 第二: PointT
没有默认构造函数,因此您必须对其进行初始化,而不仅仅是以后分配给它。
LineT::LineT(PointT st, MapTypes::CompassT ornt, unsigned int l)
: s(st)
, o(ornt)
, L(l)
{}
As a point of style, I would also recommend using much clearer and more consistent names. 作为样式,我还建议使用更清晰,更一致的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.