C++ 调用构造函数时没有匹配的函数(c+;+;)
编辑 P>好的,我已经做了几小时的阅读,我觉得我终于明白了C++ OOP有点好(至少是基础)。我决定重写整个程序,一次编写一点代码,然后进行更多的测试。我想我缩小了我这次犯的错误 命名格式.hC++ 调用构造函数时没有匹配的函数(c+;+;),c++,function,constructor,matching,C++,Function,Constructor,Matching,编辑 P>好的,我已经做了几小时的阅读,我觉得我终于明白了C++ OOP有点好(至少是基础)。我决定重写整个程序,一次编写一点代码,然后进行更多的测试。我想我缩小了我这次犯的错误 命名格式.h #include <string> #include <iostream> #ifndef NAMEDSTORM_H_INCLUDED #define NAMEDSTORM_H_INCLUDED // NEVER use using namespce in header, us
#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
#包括
#包括
#ifndef名称包括
#定义包含的名称模板
//不要在标题中使用namespce,而是使用std。
类名模板{
私人:
std::字符串名称;
std::字符串类型;
双最大风速;
双风压;
静态计数;
公众:
//建造师
namedForm(std::string,double,std::string,double);
namedForm(std::string);
//析构函数
~NamedStorm();
//获取函数
int getStormCount();
双重压力();
双风速();
std::string getStormCategory();
std::string getName();
//设置函数
静态输出();
静态void sortByNames();
静态无效sortByWindSpeed();
静态void getAverageWindSpeed();
静态孔隙getAverageStormPressure();
};
#endif//NAMEDSTORM_H_包括在内
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(){}
#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;
}
//CPP=>函数定义
#包括
#包括“NamedStorm.h”
使用名称空间std;
//定义静态变量
int NamedStorm::stormCount=0;
//构造函数定义
NamedStorm::NamedStorm(std::string sName、double wSpeed、std::string sCat、double sPress){
stormName=sName;
风速=W速度;
风暴类别=sCat;
风暴压力=气压;
stormCount++;
}
NamedStorm::NamedStorm(std::string sName){
stormName=sName;
stormCount++;
}
//析构函数定义
NamedStorm::~NamedStorm(){}
//获取(访问器)函数定义
int NamedStorm::getStormCount(){
返回风暴计数;
}
双名称表单::getStormPressure(){
回风压;
}
字符串名称表单::getStormCategory(){
返回类别;
}
字符串名称表单::getName(){
返回stormName;
}
//Set(Mutator)函数定义
void NamedStorm::displayOutput(){}
void NamedStorm::sortByNames(){}
void NamedStorm::getAverageStormPressure(){}
void NamedStorm::getAverageWindSpeed(){}
void NamedStorm::getWindSpeed(){}
main.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(){}
#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.h”
使用名称空间std;
命名风暴[5];//这里发生错误
int main(){
//命名为克里斯(“克里斯”,70.0,“T.S”,990.0);
//暴风雪[0]=克里斯;
返回0;
}
1。删除构造函数定义
在头文件(NamedStorm.h)中定义了NamedStorm的默认构造函数:
NamedStorm(){};
但您真正想要的只是构造函数声明:
NamedStorm();
定义和声明之间的区别在于,声明只告诉编译器存在某个函数(例如:NamedForm构造函数),而定义提供了该函数的全部内容
请注意,如果只为函数指定声明,而忘记进行定义,则会出现未定义引用
链接器错误
进一步阅读:
2。更正第二个构造函数
NamedStorm::NamedStorm(string sName, double wSpeed, string sName, double sPress)
这无法工作,因为您试图传递两个同名参数。我猜您想命名第二个sCat
,因为您在构造函数定义中使用了这样的变量。正确版本:
NamedStorm::NamedStorm(string sName, double wSpeed, string sCat, double sPress)
3。运算符为
NamedStorm(){}
不仅是一个声明,也是一个定义。输入错误?您的一个构造函数定义了两次,一个根本没有,请不要使用使用名称空间之类的东西代码>在标题中。您使用什么来运行它-确保所有文件都链接正确且顺序正确@DyP出于某种原因,他可能想要一个空的默认构造函数。但是,是的,它定义了这样的双代码不太可能帮助您理解C++。相当多的一个基本上是java代码,只是用C++编译器所接受的奇怪语法编写的。使用std::array
,它知道对象的大小,您可以使用任意一个数组一次性初始化对象,就像在Java中一样。+1很有用,但不完整。使用NamedStorm param[]
的“访问器”仍然包含错误和/或容易出错(假设数组大小为5)。我决定重新开始完整的代码,您的解决方案帮助很大,但代码仍然有我需要纠正的错误fix@Tee-男:你能说得更具体一点吗?如果您按照上面的步骤操作,应该不会出现编译器/链接器错误。很抱歉,我已经解决了这个问题,不是“函数与构造函数不匹配”错误。