C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-06-2005, 07:21 AM   #1
C(++)(#)
 
Join Date: Jul 2004
Posts: 309
Linker Error problem

First, here is the code:
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;
}
PBClass.h
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);
    };
}
PBFunctions.CPP
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();
    }
}
Now here is the problem: it won't compile. Everytime I try I get this error:

[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   Reply With Quote
Old 06-06-2005, 07:32 AM   #2
Registered User
 
hk_mp5kpdw's Avatar
 
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...

}
I also think you are missing something in this bit of 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();
}
You need to allocate memory to your npb pointer before you start using it. And then you would of course delete the memory allocated to the pointer after you were done using it.
__________________
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   Reply With Quote
Old 06-06-2005, 07:51 AM   #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();
and
Code:
cout<<PVec.end()-1;
but I keep getting an error about there not being a match for it. This is in the function PhoneBook::AddPerson(); by the way.
__________________
To code is divine
7smurfs is offline   Reply With Quote
Old 06-06-2005, 08:04 AM   #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;
should be:
Code:
person *npb = new person;
Make sure that when you declare a pointer, that before you use the pointer and store variables in its location that you need to assign the memory using the new operator.

Last edited by JoshR; 06-06-2005 at 08:06 AM.
JoshR is offline   Reply With Quote
Old 06-06-2005, 08:04 AM   #5
Registered User
 
hk_mp5kpdw's Avatar
 
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();
to:

Code:
cout << *PVec.begin();
then it should work.
__________________
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   Reply With Quote
Old 06-06-2005, 08:09 AM   #6
*this
 
Join Date: Mar 2005
Posts: 498
Quote:
Originally Posted by hk_mp5kpdw
then it should work.
It would just output a 0 because he hasnt assigned memory to the location, so theres no way his variables made it to the location of the pointer.
JoshR is offline   Reply With Quote
Old 06-06-2005, 08:26 AM   #7
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,870
Quote:
Originally Posted by JoshR
It would just output a 0 because he hasnt assigned memory to the location, so theres no way his variables made it to the location of the pointer.
I already made mention of the fact the OP was not allocating memory to the npb pointer in my first post. I am assuming he corrected that and has now moved on to another seperate issue.
__________________
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   Reply With Quote
Old 06-06-2005, 02:16 PM   #8
*this
 
Join Date: Mar 2005
Posts: 498
O_o i should really start reading the full posts before mine
JoshR is offline   Reply With Quote
Old 06-07-2005, 09:51 AM   #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;
            }
I had to go cout<<*npb, instead of cout<<npb?
__________________
To code is divine
7smurfs is offline   Reply With Quote
Old 06-07-2005, 10:28 AM   #10
Registered User
 
hk_mp5kpdw's Avatar
 
Join Date: Jan 2002
Location: Northern Virginia/Washington DC Metropolitan Area
Posts: 2,870
Quote:
Originally Posted by 7smurfs
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;
}
I had to go cout<<*npb, instead of cout<<npb?
You've defined the stream insertion operator for an object of type person and not for an object of type pointer-to-person. When you output you must therefore output an object of type person and not a pointer to type person. Putting the * in front of your pointer dereferences the pointer turning it from a pointer to type person and into an object of type person.

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;
}
The above would allow you to do:
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 05:33 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22