Probably a simple problem, but for the life of me, I'm not seeing it. Included header, source (think that's how it should be referred to), and a main test routine.
Code:
Compile error = g++ person.cpp personTest.cpp
/tmp/cc5BXASf.o(.text+0x2d): In function `main':
: undefined reference to `operator>>(std::basic_istream<char, std::char_traits<char> >&, Person&)'
collect2: ld returned 1 exit status
Any help figuring out what's going wrong or what I've done wrong?
file 01 - header***************
Code:
#ifndef PERSON_H
#define PERSON_H
#include <string>
#include <iostream>
using namespace std;
class Person {
public:
friend ostream& operator<<( ostream& os, const Person& aPerson );
friend istream& operator>>( istream& is, Person& aPerson );
//constructors
Person( string firstName, string lastName, int age, int numComputers );
Person();
// Accessors
string getFirst() const;
string getLast() const;
string getFull() const;
string getShort() const;
int getAge() const;
int getNumComputers() const;
void print() const;
// Mutators
void set( string firstName, string lastName, int age, int numComputers );
void setFirstName( string firstName );
void setLastName( string lastName );
void setAge( int age );
void setNumComputers( int numComputers );
private:
string _firstName;
string _lastName;
int _age, _numComputers;
};
#endif
file 02 - source***************
Code:
#include "person.h"
#include <iostream>
#include <string>
using namespace std;
ostream& operator<<( ostream& os, const Person& aPerson ) {
os << aPerson.getFirst() << "/"
<< aPerson.getLast() << "/"
<< aPerson.getAge() << "/"
<< aPerson.getNumComputers();
return os;
}
istream& operator>>( istream is, Person& aPerson ) {
cout << "Please enter first name: ";
string firstName;
is >> firstName;
cout << "Please enter last name: ";
string lastName;
is >> lastName;
cout << "Please enter age: ";
int age;
is >> age;
cout << "Please enter number of computers: ";
int numComputers;
is >> numComputers;
aPerson.set( firstName, lastName, age, numComputers );
return is;
}
//constructors
Person::Person( string firstName, string lastName, int age, int numComputers ) {
set( firstName, lastName, age, numComputers);
}
Person::Person() {
_firstName = "default";
_lastName = "default";
_age = -1;
_numComputers = -1;
}
// Accessors
string Person::getFirst() const {
return _firstName;
}
string Person::getLast() const {
return _lastName;
}
string Person::getFull() const {
return _firstName + " " + _lastName;
}
string Person::getShort() const {
string empty;
return empty + _firstName.at(0) + ". " + _lastName;
}
int Person::getAge() const {
return _age;
}
int Person::getNumComputers() const {
return _numComputers;
}
void Person::print() const {
cout << getFirst() << endl;
cout << getLast() << endl;
cout << getFull() << endl;
cout << getShort() << endl;
cout << getAge() << endl;
cout << getNumComputers() << endl;
}
// Mutators
void Person::set( string firstName, string lastName, int age, int numComputers ) {
setFirstName( firstName );
setLastName( lastName );
setAge( age );
setNumComputers( numComputers );
}
void Person::setFirstName( string firstName ) {
_firstName = firstName;
}
void Person::setLastName( string lastName ) {
_lastName = lastName;
}
void Person::setAge( int age ) {
_age = age;
}
void Person::setNumComputers( int numComputers ) {
_numComputers = numComputers;
}
file 03 - mainTest***************
Code:
#include "person.h"
#include <iostream>
#include <string>
using namespace std;
int main() {
Person test;
cin >> test;
cout << test << endl;
return 0;
}