问题
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:
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.
来源:https://stackoverflow.com/questions/17818282/no-matching-functions-for-call-to-constructor-c