c++ - 没有用于调用构造函数的匹配函数 (c++)

标签 c++ function constructor matching

<分区>

编辑

好的,我又读了几个小时,我想我终于更好地理解了 c++ OOP(至少是基础知识)。我决定一次重写整个程序和代码并进行更多测试。我想我这次将错误缩小了一些。

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;
}

最佳答案

<强>1。删除构造函数定义

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

NamedStorm(){};

但您真正想要的只是构造函数声明:

NamedStorm();

定义和声明之间的区别在于,声明仅告诉编译器存在某个函数(例如:NamedStorm 构造函数),而定义提供了该函数的完整主体。

请注意,如果您只指定函数的声明而忘记进行定义,您将得到 undefined reference链接器错误。

进一步阅读: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 的信息方法 here .

<强>4。定义静态类变量

您在类中声明的每个静态变量(在您的例子中只有 int stormCount)必须定义。将此行放入您的 NamedStorm.cpp 文件中:

int NamedStorm::stormCount = 0;

应用这些更改后,您的代码应该可以正常工作。但是,您仍然可以阅读许多语言细微差别来改进您的代码。其中一些是:

<强>1。将函数参数作为值传递与 const 引用

这里很好读:Is it better in C++ to pass by value or pass by constant reference?

<强>2。函数返回对象拷贝与 const 引用

这个问题在 SO 上也有很好的答案:Is it more efficient to return a const reference

<强>3。小心“使用命名空间”

同样,SO:Why is "using namespace std" considered bad practice?

如果你真的想使用它,永远不要在头文件中使用它,因为它会影响所有包含它的文件。

关于c++ - 没有用于调用构造函数的匹配函数 (c++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17818282/

相关文章:

python - 函数修改列表

php - 如何从 View 中使用模型函数? - 拉维尔 5.4

c++ - C++:通过函数传递指针数组的语法

c++ - 旧 vector 是否被清除?如果是,如何以及何时?

c++ - C++ 编译器如何优化模板代码?

c++ - 使用 C++ 替换或删除正则表达式中不存在的字符

c++ - 可变模板构造函数和复制构造函数

javascript - 如何从其构造函数中定义的 HTML 调用类方法?

c++ - 如何在 Ubuntu 上安装 OpenSSL 库?

c++ - 无法将参数传递给 GTK 回调