没有匹配的函数调用构造函数(c ++) [英] no matching functions for call to constructor (c++)

查看:183
本文介绍了没有匹配的函数调用构造函数(c ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDIT



好吧,我又读了几个小时,我想我终于明白了c ++ OOP位更好(至少基础)。我决定重写整个程序和代码一次一点,测试更多。


$ b

pre> #include< string>
#include< iostream>

#ifndef NAMEDSTORM_H_INCLUDED
#define NAMEDSTORM_H_INCLUDED

//不要在头文件中使用namespce,而使用std。

class NamedStorm {
private:
std :: string stormName;
std :: string stormCategory;
double maxWindSpeed;
double stormPressure;
static int stormCount;
public:

//构造函数
NamedStorm(std :: string,double,std :: string,double);
NamedStorm(std :: string);

//析构函数
〜NamedStorm();

//获取函数
int getStormCount();
double getStormPressure();
double getWindSpeed();
std :: string getStormCategory();
std :: string getName();

//设置函数
static void displayOutput();
static void sortByNames();
static void sortByWindSpeed();
static void getAverageWindSpeed();
static void getAverageStormPressure();
};

#endif // NAMEDSTORM_H_INCLUDED

NamedStorm.cpp

  // CPP =>函数定义
#include< string>

#includeNamedStorm.h

using namespace std;

//定义静态变量
int NamedStorm :: stormCount = 0;

//构造方法定义
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(){}

// Get(Accessor)函数定义
int NamedStorm :: getStormCount(){
return stormCount;
}

double NamedStorm :: getStormPressure(){
return stormPressure;
}

string NamedStorm :: getStormCategory(){
return stormCategory;
}

string NamedStorm :: getName(){
return stormName;
}

//设置(Mutator)函数定义
void NamedStorm :: displayOutput(){}
void NamedStorm :: sortByNames(){}
void NamedStorm :: getAverageStormPressure(){}
void NamedStorm :: getAverageWindSpeed(){}
void NamedStorm :: getWindSpeed(){}

main.cpp

  include< iostream> 
#include< string>

#includeNamedStorm.h

using namespace std;

NamedStorm storm [5]; //错误出现在这里

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


解决方案

移除建构函式定义



在您的标题档案( 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操作符



如果您阅读第一部分,那么您应该知道运算符<< / code>。您只提供声明,而不是定义



您可以这样填写:

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

请注意,声明也改变了 - 函数现在 NamedStorm& 而不是 const NamedStorm& ,因为 getName 作为 const 。你可以阅读 const 方法此处



4。定义静态clas变量



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

  int NamedStorm :: stormCount = 






应用这些更改后,代码应该正常工作。但是,仍然有很多语言的细节,你可以阅读关于改进你的代码。其中有些是:



1。将函数参数作为值传递给const引用



在此处阅读:



2。函数返回对象副本与const引用



此问题对SO也有一个很好的答案:返回const引用更有效



3。谨慎使用using namespace



同样,SO:为什么是using namespace std;被认为是不良做法?



如果您真的要使用它,从不头文件,因为它会影响所有包含它的文件。


EDIT

Ok, I've done a bit of reading again for a few hours and I think I finally understand c++ OOP a bit better (at least the basics). I decided to rewrite the entire program and code a bit at a time and test more. I think i narrowed the errors i bit more this time.

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. Remove constructor definition

In your header file (NamedStorm.h) you have defined the default constructor of NamedStorm:

NamedStorm(){};

But what you really wanted is just the constructor declaration:

NamedStorm();

The difference between definition and declaration is that the declaration only tells the compiler that there is some function (for example: NamedStorm constructor), whereas the definition provides the full body of this function.

Note that, if you specify only the declaration for your function, and forget to make the definition, you will get the undefined reference linker error.

Further reading: http://www.cprogramming.com/declare_vs_define.html

2. Correct the second constructor

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

This can't work, because you try to pass two arguments with the same name. I guess you wanted to name the second one sCat, since you use such variable in the constructor definition. Correct version:

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

3. The operator<<

If you read the first section, then you should know what's wrong with the operator<< by now. You provided only the declaration, not the definition.

You can fill it like this:

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

Note that the declaration is also changed - the function now takes NamedStorm& instead of const NamedStorm&, because the getName method is not declared as const. You can read about const methods here.

4. Define static clas variables

Every static variable you declare in your class (only int stormCount in your case) have to be defined. Put this line into your NamedStorm.cpp file:

int NamedStorm::stormCount = 0;


After applying these changes your code should work fine. However, there are still many language nuances that you could read about to improve your code. Some of them are:

1. Passing function arguments as values vs const references

Good read here: Is it better in C++ to pass by value or pass by constant reference?

2. Functions returning object copies vs const references

This question also has a nice answer on SO: Is it more efficient to return a const reference

3. Be careful with "using namespace"

Again, SO: Why is "using namespace std;" considered bad practice?

If you really want to use it, never use it in the header file, because it will affect all files that include it.

这篇关于没有匹配的函数调用构造函数(c ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆