no matching functions for call to constructor (c++) -
this question has answer here:
edit
ok, i've done bit of reading again few hours , think understand c++ oop bit better (at least basics). decided rewrite entire program , code bit @ time , test more. think narrowed errors bit more 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(); // 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(){} // (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 header file (namedstorm.h) have defined default constructor of namedstorm:
namedstorm(){};
but wanted constructor declaration:
namedstorm();
the difference between definition , declaration declaration tells compiler there function (for example: namedstorm constructor), whereas definition provides full body of function.
note that, if specify declaration function, , forget make definition, undefined reference
linker error.
further reading: http://www.cprogramming.com/declare_vs_define.html
2. correct second constructor
namedstorm::namedstorm(string sname, double wspeed, string sname, double spress)
this can't work, because try pass 2 arguments same name. guess wanted name second 1 scat
, since use such variable in constructor definition. correct version:
namedstorm::namedstorm(string sname, double wspeed, string scat, double spress)
3. operator<<
if read first section, should know what's wrong operator<<
now. provided declaration, not definition.
you can fill this:
std::ostream& operator<<(ostream& out, namedstorm& namedstorm) { out << namedstorm.getname(); return out; }
note declaration changed - function takes namedstorm&
instead of const namedstorm&
, because getname
method not declared const
. can read const
methods here.
4. define static clas variables
every static variable declare in class (only int stormcount
in case) have defined. put line namedstorm.cpp file:
int namedstorm::stormcount = 0;
after applying these changes code should work fine. however, there still many language nuances read improve code. of them are:
1. passing function arguments values vs const references
good read here: is better in c++ pass value or pass constant reference?
2. functions returning object copies vs const references
this question has nice answer on so: is more efficient return const reference
3. careful "using namespace"
again, so: why "using namespace std" considered bad practice?
if really want use it, never use in header file, because affect files include it.
Comments
Post a Comment