C++中构造函数没有匹配的函数调用

4

编辑

好的,我又读了几个小时,现在我终于更好地理解了C++面向对象编程(至少是基础)。我决定重新编写整个程序,逐步编写代码并进行更多测试。我觉得这次我把错误缩小了一些。

NamedStorm.h

#include <string>
#include <iostream>

#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);

    // 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.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++;
}

// 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(){}

main.cpp

#include <iostream>
#include <string>

#include "NamedStorm.h"

using namespace std;

NamedStorm storm[5]; // Error occurs here

int main(){
   // NamedStorm Chris("Chris", 70.0, "T.S", 990.0); 
   // storm[0] = Chris;
    return 0;
}

2
这个 NamedStorm(){}; 不仅是声明,也是定义。笔误? - dyp
4
你的构造函数被定义了两次,其中一个没有被定义,而且请永远不要在头文件中使用using namespace blah; - chris
你应该尽量避免在头文件中使用 using namespace std - juanchopanza
4
这样的代码不太可能帮助你理解C++。其中有很多基本上还是Java代码,只不过用一种奇怪的语法编写,并被C ++编译器接受而已。 - Jerry Coffin
1
你的 displayOutput 函数有一个 bug。使用 std::array,它知道自己的大小,并且你可以像在 Java 中一样用任意数组一次性初始化对象。 - chris
显示剩余3条评论
1个回答

4

1.移除构造函数定义

在您的头文件(NamedStorm.h)中,您已经定义了NamedStorm的默认构造函数:

NamedStorm(){};

但是你真正需要的只是构造函数声明

NamedStorm();

定义和声明的区别在于声明只是告诉编译器有某个函数 (例如:NamedStorm 构造函数),而定义则提供了此函数的完整实现。
请注意,如果您仅指定函数的声明,并忘记进行定义,则会出现“未定义的引用”链接错误。
进一步阅读:http://www.cprogramming.com/declare_vs_define.html 2. 更正第二个构造函数。
NamedStorm::NamedStorm(string sName, double wSpeed, string sName, double sPress)

这样做是行不通的,因为你试图传递两个同名参数。我猜你想把第二个参数命名为sCat,因为你在构造函数定义中使用了这样的变量名。正确的版本如下:

NamedStorm::NamedStorm(string sName, double wSpeed, string sCat, double sPress)

3. 运算符<<

如果您已经阅读了第一部分,那么现在您应该知道operator<<的问题所在。您只提供了声明,而没有定义

您可以像这样填写:

std::ostream& operator<<(ostream& out, NamedStorm& namedStorm)
{
    out << namedStorm.getName();
    return out;
}

请注意声明也发生了更改——函数现在采用NamedStorm&而不是const NamedStorm&,因为getName方法没有声明为const。您可以在这里阅读有关const方法的信息。
4. 定义静态类变量
你在类中声明的每个静态变量(在你的情况下,只有int stormCount)都必须被定义。将此行放入您的NamedStorm.cpp文件中:
int NamedStorm::stormCount = 0;

应用这些更改后,你的代码应该可以正常工作。但是,仍然有许多语言细节可以阅读以改进你的代码。其中一些是:
1. 将函数参数传递为值与const引用
好的阅读材料在这里:在C++中传递值还是传递常量引用更好? 2. 函数返回对象副本与const引用
这个问题在SO上也有一个很好的答案:返回const引用是否更有效率 3. 谨慎使用"using namespace"
同样,在SO上:为什么认为"using namespace std"是不好的做法? 如果你真的想使用它,请永远不要在头文件中使用它,因为它会影响所有包含它的文件。

+1 有用,但不完整。 "访问器" 接受 NamedStorm param[] 仍然存在错误和/或容易出错的问题(假设数组大小为5)。 - dyp
我决定重新开始整个代码,你的解决方案非常有帮助,但是代码仍然有错误我需要修复。 - Tee-Man
@Tee-Man:你能更具体一些吗?如果你按照上面的步骤操作,就不应该出现编译器/链接器错误。 - podkova
抱歉,我已经解决了那个问题,现在出现了“构造函数不匹配”的错误。 - Tee-Man

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接