2
Multiple markers at this line
- candidates are:
- no matching function for call to 
 'Coordinate::Coordinate()'

我在我的类的构造函数中遇到了这个错误,我不明白为什么。这是涉及的代码:

RadialScan 标头

#ifndef RADIALSCAN_H_
#define RADIALSCAN_H_
#include "EasyBMP/EasyBMP.h"
#include <vector>
#include "Coordinate.h"

using namespace std;

class RadialScan {
    vector<int> distanceTimeSeries;
    vector<Coordinate> timeSeries;
    BMP image;
    Coordinate center;

    Coordinate getNextPoint(Coordinate c);
    bool isBlack(Coordinate c);
    void computeTimeSeries();

public:
    RadialScan(char* filename);
    vector<int> getDistances();
    vector<Coordinate> getCoordinates();
};

#endif

RadialScan 类(所有方法都已实现,但错误在构造函数中,这就是我提供的代码):

#include "RadialScan.h"

RadialScan::RadialScan(char* filename){
    image.ReadFromFile(filename);
    int centerX = image.TellWidth()/2;
    int centerY = image.TellHeight()/2;
    center = Coordinate(centerX, centerY);
}
...

错误似乎在构造函数中。如果我删除构造函数,一切似乎都可以正确编译。如果我删除构造函数中的代码,我仍然会收到错误消息。我不明白为什么它一直问我 Coordinate::Coordinate() 构造函数,即使我没有在 RadialScan(char* filename) 构造函数中定义坐标对象。

此外,这些是 Coordinate 类的文件:标头:

#ifndef COORDINATE_H_
#define COORDINATE_H_

class Coordinate {
    int x;
    int y;
public:
    Coordinate(int x, int y);
    void setX(int oneX);
    void setY(int oneY);
    int getX();
    int getY();
    double getMagnitude();
    Coordinate operator-(const Coordinate&);
    bool operator==(const Coordinate&);
    Coordinate operator=(const Coordinate&);
};

#endif

cpp类:

#include "Coordinate.h"
#include <math.h>

Coordinate::Coordinate(int oneX, int oneY) {
    x = oneX;
    y = oneY;
}

//Setters
void Coordinate::setX(int oneX) {
    x = oneX;
}

void Coordinate::setY(int oneY) {
    y = oneY;
}

//Getters
int Coordinate::getX() {
    return x;
}

int Coordinate::getY() {
    return y;
}

double Coordinate::getMagnitude() {
    return sqrt(x * x + y * y);
}

Coordinate Coordinate::operator-(const Coordinate& p) {
    return Coordinate(x - p.x, y - p.y);
}

bool Coordinate::operator==(const Coordinate& p) {
    return x == p.x && y == p.y;
}

Coordinate Coordinate::operator=(const Coordinate& p) {
    return Coordinate(p.x, p.y);
}
4

1 回答 1

2

您的构造函数必须看起来像

RadialScan::RadialScan(char* filename) : center (0, 0) {
    image.ReadFromFile(filename);
    int centerX = image.TellWidth()/2;
    int centerY = image.TellHeight()/2;
    center = Coordinate(centerX, centerY);
}

这是因为您没有实现默认构造函数,并且默认情况下无法创建中心对象,因此唯一的方法是使用一些默认值调用显式坐标构造函数。

于 2015-10-13T23:36:39.830 回答