【问题标题】:no matching functions for call to constructor (c++) [duplicate]没有用于调用构造函数的匹配函数(c ++)[重复]
【发布时间】:2013-07-23 00:00:46
【问题描述】:

编辑

好的,我又读了几个小时,我想我终于更好地理解了 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(){}

ma​​in.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;
}

【问题讨论】:

  • 这个NamedStorm(){};不仅是一个声明,也是一个定义。错字?
  • 您的一个构造函数被定义了两次,一个根本没有,请永远在标题中使用using namespace blah;
  • 你用什么来运行它 - 确保你的文件都以正确的顺序正确链接。 @DyP 出于某种原因,他可能想要一个空的默认构造函数。但是是的,它被定义了两次
  • 这样的代码不太可能帮助您理解 C++。其中相当一部分仍然基本上是 Java 代码,只是用一种恰好被 C++ 编译器接受的奇怪语法编写。
  • 您的displayOutput 函数有错误。使用知道其大小的std::array,您可以使用任一数组一次性初始化对象,就像在 Java 中一样。

标签: c++ function constructor matching


【解决方案1】:

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&lt;&lt; 出了什么问题。您只提供了声明,没有提供定义

你可以这样填写:

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

请注意,声明也已更改 - 该函数现在采用 NamedStorm&amp; 而不是 const NamedStorm&amp;,因为 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.小心“使用命名空间”

再一次,所以:Why is "using namespace std" considered bad practice?

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

【讨论】:

  • +1 有用,但不完整。采用NamedStorm param[] 的“访问器”仍然包含错误和/或容易出错(假设数组大小为 5)。
  • 我决定重新开始整个代码,您的解决方案帮助很大,但代码仍然有我需要修复的错误
  • @Tee-Man:你能说得更具体点吗?如果您按照上述步骤操作,应该不会出现编译器/链接器错误。
  • 对不起,我已经解决了这个问题,而不是“与构造函数不匹配”错误。
猜你喜欢
  • 1970-01-01
  • 2012-05-09
  • 2020-01-22
  • 1970-01-01
  • 1970-01-01
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 2013-10-31
相关资源
最近更新 更多