对构造函数(c++)的未识别引用

Unidentified reference to constructor (c++)

本文关键字:识别 引用 构造函数 c++      更新时间:2023-10-16

我已经对这个错误进行了几天的研究和谷歌搜索,据我所知,这是c++中许多人的常见问题,我仍然没有找到这个错误的明确答案。我读过链接文件可以解决这个问题,但我可以找到任何示例代码来做到这一点。我很快就要完成这段代码了,我只需要从主文件中调用构造函数(或者只生成一个简单的对象),但我一直收到这个"对NamedStorm::NamedStorm()的未识别引用"错误,请帮忙。

MAIN.CPP

#include <iostream>
#include <string>
#include "NamedStorm.h"
using namespace std;
NamedStorm storm[2];
int main(){
   // NamedStorm Chris("Chris", 70.0, "T.S.", 990.0);
  // storm[0] = Chris;
    return 0;
}

命名风暴.cpp

// CPP => Function definition
#include <string>
#include "NamedStorm.h"
using namespace std;
// Defining static variables
int NamedStorm::stormCount = 0;
// Constructor definition
NamedStorm::NamedStorm(std::string sName, double wSpeed, std::string sCat, double sPress){
    stormName = sName;
    windSpeed = wSpeed;
    stormCategory = sCat;
    stormPressure = sPress;
    stormCount++;
}
NamedStorm::NamedStorm(std::string sName){
    stormName = sName;
    stormCount++;
}
NamedStorm::NamedStorm(){
    stormName = sName;
    stormCount++;
}
// Destructor definition
//NamedStorm::~NamedStorm(){}
// Get (Accessor) function definition
int NamedStorm::getStormCount(){
    return stormCount;
}
double NamedStorm::getStormPressure(){
    return stormPressure;
}
string NamedStorm::getStormCategory(){
    return stormCategory;
}
string NamedStorm::getName(){
    return stormName;
}
// Set (Mutator) function definition
void NamedStorm::displayOutput(){}
void NamedStorm::sortByNames(){}
void NamedStorm::getAverageStormPressure(){}
void NamedStorm::getAverageWindSpeed(){}
void NamedStorm::getWindSpeed(){}

NamedStorm.h

#ifndef NAMEDSTORM_H_INCLUDED
#define NAMEDSTORM_H_INCLUDED
// NEVER use using namespce in header, use std instead.

class NamedStorm{
private:
    std::string stormName;
    std::string stormCategory;
    double maxWindSpeed;
    double stormPressure;
    static int stormCount;
public:
    // Constructor
    NamedStorm(std::string, double, std::string, double);
    NamedStorm(std::string);
    NamedStorm();
    // Destructor
    //~NamedStorm();
    // Get functions
    int getStormCount();
    double getStormPressure();
    double getWindSpeed();
    std::string getStormCategory();
    std::string getName();
    // Set functions
    static void displayOutput();
    static void sortByNames();
    static void sortByWindSpeed();
    static void getAverageWindSpeed();
    static void getAverageStormPressure();
};
#endif // NAMEDSTORM_H_INCLUDED

为什么默认构造函数的定义与NamedStorm::NamedStorm(std::string)的定义相同?我会从纠正这一点开始。