LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-06-2004, 11:20 PM   #1
pH*
Member
 
Registered: Nov 2003
Distribution: Slackware 10
Posts: 40

Rep: Reputation: 15
c++ istream issues


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;
}
 
Old 03-07-2004, 08:38 AM   #2
haobaba1
Member
 
Registered: Jul 2003
Location: VA Tech
Distribution: Mandrake 9.1
Posts: 73

Rep: Reputation: 15
istream& operator>>( istream is, Person& aPerson ) {

is needs to be by reference.
 
Old 03-07-2004, 09:26 AM   #3
pH*
Member
 
Registered: Nov 2003
Distribution: Slackware 10
Posts: 40

Original Poster
Rep: Reputation: 15
What exactly do you mean by 'needs to be by reference'.
 
Old 03-07-2004, 10:39 AM   #4
pH*
Member
 
Registered: Nov 2003
Distribution: Slackware 10
Posts: 40

Original Poster
Rep: Reputation: 15
OK -- finally got it.

istream& operator>>( istream is, Person& aPerson ) {

needs an "&" after the istream within the parentheses, like so:

istream& operator>>( istream& is, Person& aPerson ) {

... creates a reference to the original rather than a duplicate (or something along those lines).

thx haobaba1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
X11 TTF Issues; OOo Font Issues Kenji Miyamoto Slackware 2 05-27-2005 06:30 PM
New to linux, so so lost, auto mounting issues, permissions issues slowhand22 Linux - Newbie 2 02-10-2005 09:41 AM
[Cygwin][C++] How to get file descriptor using istream object? chuanyung Programming 2 07-14-2004 10:42 PM
Using istream::get() & istream::getline() mutiple times kamransoomro84 Programming 0 06-14-2004 07:11 PM
Sound issues + Mouse wheel issues matt3333 Slackware 2 10-12-2003 03:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration