![]() |
| | #1 |
| C(++)(#) Join Date: Jul 2004
Posts: 309
| Linker Error problem Main.CPP Code: #include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "PBClass.h"
using namespace std;
PhoneBook::PhoneBook* PB = new PhoneBook::PhoneBook::PhoneBook();
int main(int argc, char *argv[])
{
PB->AddPerson();
system("PAUSE");
return EXIT_SUCCESS;
}
Code: #include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using std::string;
namespace PhoneBook
{
class PhoneBook
{
private:
struct person
{
int number;
string f_name;
string l_name;
};
std::vector<person> PVec;
public:
PhoneBook();
~PhoneBook();
void AddPerson(person *pb);
void AddPerson();
//void RemovePerson(person *pb);
};
}
Code: #include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "PBClass.h"
using namespace std;
namespace PhoneBook
{
void PhoneBook::AddPerson(PhoneBook::person *pb)
{
PVec.push_back(*pb);
}
void PhoneBook::AddPerson()
{
person *npb;
cout<<"Please put in the phone number of the person:\n";
cin>>npb->number;
cout<<"\nPlease input the first name of the person:\n";
cin>>npb->f_name;
cout<<"\nPlease input the last name of the person:\n";
cin>>npb->l_name;
cout<<endl;
PVec.push_back(*npb);
//cout<<PVec.end();
}
}
[Linker error] undefined reference to `PhoneBook::PhoneBook::PhoneBook()' and to be perfectly honest, I'm lost as to what to do. As always, any help would be appreciated.
__________________ To code is divine |
| 7smurfs is offline | |
| | #2 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,870
| You must include code for all the functions you declare in the class interface: PBFunctions.CPP Code: namespace PhoneBook
{
PhoneBook::PhoneBook()
{
}
PhoneBook::~PhoneBook()
{
}
// Plus remainder of your code...
}
Code: void PhoneBook::AddPerson()
{
person *npb;
cout<<"Please put in the phone number of the person:\n";
cin>>npb->number;
cout<<"\nPlease input the first name of the person:\n";
cin>>npb->f_name;
cout<<"\nPlease input the last name of the person:\n";
cin>>npb->l_name;
cout<<endl;
PVec.push_back(*npb);
//cout<<PVec.end();
}
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
| hk_mp5kpdw is offline | |
| | #3 |
| C(++)(#) Join Date: Jul 2004
Posts: 309
| Thanks for the help! Er...any idea why it won't let me use cout<< on the vector? I tried: Code: cout<<PVec.begin(); Code: cout<<PVec.end()-1;
__________________ To code is divine |
| 7smurfs is offline | |
| | #4 |
| *this Join Date: Mar 2005
Posts: 498
| Because you have spots in your vector that contain NULL pointers because no memory has been allocated to your person pointer. Code: person *npb; Code: person *npb = new person; Last edited by JoshR; 06-06-2005 at 08:06 AM. |
| JoshR is offline | |
| | #5 |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,870
| PVec.begin() is an iterator (basically a pointer) to type person. If you had overloaded the stream insertion operator (<<) for type person and you changed the way you were outputting from: Code: cout << PVec.begin(); Code: cout << *PVec.begin();
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 |
| hk_mp5kpdw is offline | |
| | #6 | |
| *this Join Date: Mar 2005
Posts: 498
| Quote:
| |
| JoshR is offline | |
| | #7 | |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,870
| Quote:
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 | |
| hk_mp5kpdw is offline | |
| | #8 |
| *this Join Date: Mar 2005
Posts: 498
| O_o i should really start reading the full posts before mine |
| JoshR is offline | |
| | #9 |
| C(++)(#) Join Date: Jul 2004
Posts: 309
| Thanks guys, I got it to work. However, I was wondering why, with this code: Code: friend std::ostream& operator<<(std::ostream &o, const person &X)
{
o<<X.number<<" "<<X.f_name<<" "<<X.l_name<<"\n";
return o;
}
__________________ To code is divine |
| 7smurfs is offline | |
| | #10 | |
| Registered User Join Date: Jan 2002 Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,870
| Quote:
You could also have overloaded the stream insertion operator for an object of type pointer-to-person. Code: friend std::ostream& operator<<(std::ostream &o, const person * X)
{
o<<X->number<<" "<<X->f_name<<" "<<X->l_name<<"\n";
return o;
}
Code: person * npb; ... cout << npb;
__________________ On two occasions I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. --Charles Babbage, 1792-1871 09 F9 11 02 9D 74 E3 5B D8 41 56 C5 63 56 88 C0 | |
| hk_mp5kpdw is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Linker problem... no idea | cyreon | C Programming | 2 | 04-03-2009 02:53 PM |
| linker | George2 | C++ Programming | 6 | 02-23-2008 01:25 AM |
| Linker errors in VC++ 2005 | C+/- | C++ Programming | 0 | 05-18-2007 07:42 AM |
| Linker errors when compiling | The Wazaa | C++ Programming | 4 | 10-07-2006 12:55 PM |
| MSVis-Studio C++ libraries and linker errors | kellydj | Windows Programming | 10 | 03-12-2002 02:03 PM |