"No matching constructor for initialization of. . ."

"No matching constructor for initialization of. . ."

本文关键字:initialization of for constructor No matching      更新时间:2023-10-16

我知道有人问过这类问题,我看了那些回答,但仍然觉得我遗漏了一些东西。我得到了这个"没有匹配构造函数错误",因为我没有构造函数,但话虽如此,我所看到的关于构造函数的一切都表明,如果你还没有在类中包含变量名,你就需要它们。但我已经这么做了,还需要构造函数吗?如果我做了,它应该是什么样子?我是c++的新手,正在上课,这是一个作业。

下面是我的sensor_node.h文件和类声明:
#ifndef SENSORNODE_H
#define SENSORNODE_H
#include <iostream>
class LOCATION {
    float lat, longi, height;
public:
    LOCATION (float lat, float longi, float height);
    void setx(float xx);
    void sety(float yy);
    void setz(float zz);
    void print();
};
class SensorNode {
    char* NodeName;
    int NodeID;
    LOCATION Node1;
    float batt;
    int func;

public:
    SensorNode(char *n, float x, float y, float z, int i, float ah);
    void print();
    void setOK(int o);
    int getOK();
    void setLOC(float longi, float lat, float h);
};
#endif /* defined(__Project_3__sensor_node__) */

这里是我的main.cpp错误(在说"LOCATION"的那行):

#include <iostream>
using namespace std;

#include "sensor_node.h"
int main() {
    LOCATION a; SensorNode s1("Pulse",15.9,-30.1,0,157,2.0);
    int hold;

实际上,您有一个构造函数:LOCATION (float lat, float longi, float height)。因为它是唯一的,c++尝试应用它。但是,您没有提供任何参数,因此此构造函数不匹配。

你有一个LOCATION的构造函数(为什么不一致的资本化,顺便说一下?),它需要三个浮点数,但是行LOCATION a试图调用默认构造函数,你还没有定义。