c++ - 2 Link Errors, 1 from a virtual functions, 1 from a derived class -


i'm getting lnk 2019 , 2001 error every time compile. lnk2019 states:

public: __thiscall colmbr::colmbr(unsigned int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0colmbr@@qae@iv?$basic_string@du?$char_traits@d@std@@v?$allocator@d@2@@std@@@z) referenced in function "public: __thiscall alumni::alumni(unsigned int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,int,int,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0alumni@@qae@iv?$basic_string@du?$char_traits@d@std@@v?$allocator@d@2@@std@@hhh0@z 

so there kind of error linking colmbr class alumni. lnk2011 says:

"public: virtual void __thiscall alumni::addclass(unsigned int,unsigned int)" (?addclass@alumni@@uaexii@z) 

so there's issue virtual function call. understand lnk errors means there variable needed declared missed don't see it. firstly, alumni::addclass function there make sure alumni not become abstract class colmbr, it's derived from. secondly, of arguments in alumni defined , declared either in alumni or in colmbr.

if i'd lnk2019 problem const unsigned int idnbr. don't know what's wrong lnk2001. maybe need give function garbage purpose or something.

this header file followed cpp.

#include <iostream> #include <string> #include <sstream> using namespace std;  #ifndef _colmbr #define _colmbr class colmbr { protected:     const unsigned int idnbr;     std::string name; public:     colmbr();     colmbr(const unsigned int idnbr, std::string stdnt_name);     std::string setname(std::string stdnt_name);     virtual void display(void);     virtual void addclass(unsigned int credits, unsigned int gradepoint) = 0; }; #endif // !1  std::string colmbr::setname(std::string stdnt_name)         {             name = stdnt_name;             return name;         }  class student : public colmbr { private:     unsigned int credhrs, qualpts;     std::string degsought;     double gpa; public:     student(unsigned int idnbr, std::string name) : colmbr (idnbr, name)     {         credhrs = 0;         qualpts = 0;         degsought = "unspecified";     }     student(const unsigned int idnbr, std::string stdnt_name, unsigned int credhrs1, unsigned int qualpts1, std::string newdegree) : colmbr(idnbr,stdnt_name)     {         credhrs = credhrs1;         qualpts = qualpts1;         degsought = newdegree;     }     void setdegree(std:: string newdegree);     double getgpa(unsigned int credhrs, unsigned int qualpts);     void addclass(unsigned int newclass, unsigned int newqualpts)     {         credhrs = credhrs + newclass;         qualpts = qualpts + newqualpts;     }     void display(void)     {         std::cout << "this student active." << std::endl <<"id #: "             << idnbr << std::endl << "name: " << name << std::endl             << "gpa: " << gpa << std::endl << "major: " << degsought             << std::endl;     } };  void student::setdegree(std:: string newdegree) {     degsought = newdegree; } double student::getgpa(unsigned int credhrs, unsigned int qualpts) {     double credhrs1, qualpts1;     std::istringstream input(credhrs);     input >> credhrs1;     std::istringstream input1(qualpts);     input >> qualpts1;     gpa = qualpts1 / credhrs1;     return gpa; }  #ifndef _alumni #define _alumni class alumni : public colmbr { private:     std::string degree;     int month, day, year; public:     alumni(const unsigned int idnbr, std::string stdnt_name, int gradmonth, int gradday, int gradyear, std::string newdegree) : colmbr(idnbr, stdnt_name)     {         degree = newdegree;         month = gradmonth;         day = gradday;         year = gradyear;     }     void addclass(unsigned int credits, unsigned int gradepoint);     void display (void)     {         std::cout << "this student alumni." << std::endl <<"id #: "                 << idnbr << std::endl << "name: " << name << std::endl                 << "graduation date: " << month << "/" << day << "/" << year                  << std::endl << "major: " << degree << std::endl;     } }; #endif 

/**********************************************

#include <iostream>  #include <cstdlib>  #include "colmbrs.h"    int main()  {      colmbr *cmbr[4]; // array of base-class pointers      int i; // index array    // create college members       cmbr[0] = new student( 12345, "steven difranco", 15, 33, "aa" );      cmbr[1] = new alumni( 98765, "don green", 12, 15, 1978, "aas" );      cmbr[2] = new alumni( 24680, "henry thoreau", 5, 22, 1846, "aa" );      cmbr[3] = new student( 13579, "millenia best" );    // display array       cout << "all college members:\n";      for( = 0; < 4; ++i )      {          cmbr[i]->display(); // no need check type field or cast          cout << endl;      }    // test addclass student       cmbr[3]->addclass( 3, 12 );      cmbr[3]->display();       cout << endl;      system("pause");      return 0;  }  

so, how got code compile , run correctly.

i declared both constructor , function giving me problems outside class. after had put assignment gpa gpa function student::display(void) function. finally, couldn't figure out how initialize const unsigned in idnbr in colmbr class colmbr(const unsigned int idnbr, std::string stdnt_name) constructor took const label off variable.

here's code header file. .cpp remains same.

edited reflect how solved const unsigned int idnbr problem.

#include <iostream> #include <string> #include <sstream> using namespace std;  #ifndef _colmbr #define _colmbr class colmbr { protected:     unsigned const int idnbr;     std::string name; public:     //a default , initial constructor called.     colmbr();     colmbr(const unsigned int id, std::string stdnt_name) : idnbr(id)     {         name = setname(stdnt_name);     }     virtual ~colmbr();     std::string setname(std::string stdnt_name);     //colmbr made abstract class.  can not called except     //a pointer.     virtual void display(void);     virtual void addclass(unsigned int credits, unsigned int gradepoint) = 0; }; #endif // !1  colmbr::~colmbr(){}  void colmbr::display(void) { }  std::string colmbr::setname(std::string stdnt_name)         {             name = stdnt_name;             return name;         }  class student : public colmbr { private:     unsigned int credhrs, qualpts;     std::string degsought;     double gpa; public:     //a constructor no additional student attributes created.     student(unsigned int idnbr, std::string name) : colmbr (idnbr, name)     {         credhrs = 0;         qualpts = 0;         degsought = "unspecified";     }     //a constructor adds student attributes created.     student(const unsigned int idnbr, std::string stdnt_name, unsigned int credhrs1, unsigned int qualpts1, std::string newdegree) : colmbr(idnbr,stdnt_name)     {         credhrs = credhrs1;         qualpts = qualpts1;         degsought = newdegree;     }     ~student();     //functions initializing variables output.     void setdegree(std:: string newdegree);     static double getgpa(unsigned int credhrs, unsigned int qualpts);     //the virtual functions overwritten.  addclass allows change in gpa.     void addclass(unsigned int newclass, unsigned int newqualpts)     {                credhrs = credhrs + newclass;         qualpts = qualpts + newqualpts;     }     void display(void)     {         gpa = getgpa(credhrs, qualpts);         std::cout << "this student active." << std::endl <<"id #: "             << idnbr << std::endl << "name: " << name << std::endl             << "gpa: " << gpa << std::endl << "major: " << degsought             << std::endl;     } };  student::~student(){}  void student::setdegree(std:: string newdegree) {     degsought = newdegree; } //getgpa declared , new students receive 0 double student::getgpa(unsigned int credhrs, unsigned int qualpts) {     double gpa;     double credhrs1, qualpts1;     if (credhrs == 0)     {         gpa = 0;     }     else     {         credhrs1 = static_cast<double>(credhrs);         qualpts1 = static_cast<double>(qualpts);         gpa = qualpts1 / credhrs1;     }     return gpa; }  #ifndef _alumni #define _alumni class alumni : public colmbr { private:     std::string degree;     int month, day, year; public:     //an alumni constructor defined.     alumni(const unsigned int idnbr, std::string stdnt_name, int gradmonth, int gradday, int gradyear, std::string newdegree) : colmbr(idnbr, stdnt_name)     {         degree = newdegree;         month = gradmonth;         day = gradday;         year = gradyear;     }     ~alumni();     //the virtual classes overwritted.     //even though addclass has no function here overwritten ensure alumni not abstract class.     void addclass(unsigned int credits, unsigned int gradepoint);     void display (void)     {         std::cout << "this student alumni." << std::endl <<"id #: "                 << idnbr << std::endl << "name: " << name << std::endl                 << "graduation date: " << month << "/" << day << "/" << year                  << std::endl << "major: " << degree << std::endl;     } }; #endif  alumni::~alumni(){}  void alumni::addclass(unsigned int credits, unsigned int gradepoint) {} 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -