没有用于调用构造函数的匹配函数 (c++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17818282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
no matching functions for call to constructor (c++)
提问by Tee-Man
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.
好的,我又读了几个小时,我想我终于对 C++ OOP 有了更好的理解(至少是基础知识)。我决定重写整个程序并一次编写一点代码并进行更多测试。我想这次我缩小了我的错误。
NamedStorm.h
命名风暴.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
// 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;
}
回答by podkova
1. Remove constructor definition
1. 移除构造函数定义
In your header file (NamedStorm.h) you have definedthe default constructor of NamedStorm:
在您的头文件 ( NamedStorm.h) 中,您已经定义了 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.
定义和声明的区别在于,声明只告诉编译器存在某个函数(例如:NamedStorm 构造函数),而定义提供了该函数的完整主体。
Note that, if you specify only the declaration for your function, and forget to make the definition, you will get the undefined referencelinker error.
请注意,如果您只为函数指定声明,而忘记进行定义,则会出现undefined reference链接器错误。
Further reading: http://www.cprogramming.com/declare_vs_define.html
进一步阅读:http: //www.cprogramming.com/declare_vs_define.html
2. Correct the second constructor
2.修正第二个构造函数
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:
这行不通,因为您尝试传递两个同名的参数。我猜你想命名第二个sCat,因为你在构造函数定义中使用了这样的变量。正确版本:
NamedStorm::NamedStorm(string sName, double wSpeed, string sCat, double sPress)
3. The operator<<
3.运算符<<
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.
如果你阅读了第一部分,那么你现在应该知道有什么问题了operator<<。您只提供了声明,而不是定义。
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 getNamemethod is not declared as const. You can read about constmethods here.
请注意,声明也已更改 - 函数现在采用NamedStorm&代替const NamedStorm&,因为该getName方法未声明为const。您可以在此处阅读有关const方法的信息。
4. Define static clas variables
4.定义静态类变量
Every static variable you declare in your class (only int stormCountin your case) have to be defined. Put this line into your NamedStorm.cppfile:
您在类中声明的每个静态变量(仅int stormCount在您的情况下)都必须定义。将此行放入NamedStorm.cpp文件中:
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
1. 将函数参数作为值与常量引用传递
Good read here: Is it better in C++ to pass by value or pass by constant reference?
好读在这里:在 C++ 中是按值传递还是按常量引用传递更好?
2. Functions returning object copies vs const references
2. 函数返回对象副本 vs const 引用
This question also has a nice answer on SO: Is it more efficient to return a const reference
这个问题在 SO 上也有一个很好的答案:返回常量引用是否更有效
3. Be careful with "using namespace"
3.小心“使用命名空间”
Again, SO: Why is "using namespace std" considered bad practice?
同样,SO:为什么“使用命名空间 std”被认为是不好的做法?
If you reallywant to use it, neveruse it in the header file, because it will affect all files that include it.
如果你真的要使用它,千万不要在头文件中使用它,因为它会影响所有包含它的文件。

