c++ - unknown function already exists error -


i have strange error when compiled code says member function alreadys exist within classes not error says

error lnk2005 "public: void __thiscall membershiptype::print(void)" (?print@membershiptype@@qaexxz) defined in persontype.obj project1 c:\users\okpal\source\repos\project1\project1\source.obj

and also

error lnk1169 1 or more multiply defined symbols found project1 c:\users\okpal\source\repos\project1\debug\project1.exe 1

i wondering if figure out error class code below

#include <iostream> #include <string> using namespace std; class addresstype {  //class defintions , prototypes member variables public:     addresstype();     string streetaddressnum, streetname, streettype, city, stateinitials;     int zipcode; }; class persontype { public:     persontype();     string firstname;     string lastname;     int personnum;     char gender;     int personid;     addresstype address;     void setinterest1(string interest1);//mutator     void setinterest2(string interest2);     void printperson();     string  getinterest1() const;    // accessor     string  getinterest2() const; private:     string setinterest1;     string setinterest2; }; //define membershiptype class class membershiptype :public persontype { public:     char membership_type;     char membership_status;     membershiptype();  // 1st constructor     membershiptype(char, char);  // 2nd constructor     void print();  };  void membershiptype::print()  {     cout << getinterest1(); } 

the source code persontype

#include "persontype.h" persontype::persontype() {     int personnum = 0;     int personid = 0; } addresstype::addresstype() {     int zipcode = 0; } void persontype::setinterest1(string interest1) {     setinterest1 = interest1; }//mutator void persontype::setinterest2(string interest2) {     setinterest2 = interest2; } string persontype::  getinterest1() const {     return setinterest1; }// accessor string persontype:: getinterest2() const {     return setinterest2; }  void persontype :: printperson() {//constructor      cout << firstname << " " << lastname << " " << gender << " " <<         personid << " " << address.streetaddressnum << " "         << address.streetname << " " << address.streettype         << " " << address.city << " " << address.stateinitials         << " " << address.zipcode << " " << setinterest1 << " " << setinterest2 << endl; } 

you have definition of membershiptype::print() in first code block, presume copied header file. in c++, header files' contents inserted every file includes them. presumably, have @ least 2 source files in program include header. when these source files compiled object files, both contain definition of membershiptype::print(). when try link them, linker detect both files contain definitions of same symbol. doesn't know use where, returns error.

the easiest way fix move definition of membershiptype::print() source file. can fix making inline.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -