GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-May-2006, 14:21
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Problems compiling an example (part 2)


I posted previously on this topic - the reason I am starting a new thread is because I decided to strip down the code and figure out a piece at a time. This example is from the source code posted on the authors web site. I edited out all but the most basic parts relevant to calling the first member function. I also put the implementation files for the template class into its header file. I am getting these errors and warnings:

Quote:
basic_string.h:2354: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
basic_string.h:2408: note: candidates are: std::basic_istream<_CharT, _Traits>& std::getline(std::basic_istream<_CharT, _Traits>&, std::basic_string<_CharT, _Traits, _Alloc>&, _CharT) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]
entry.cpp:30: note: std::istream& operator>>(std::istream&, entry&)
entry.cpp:33: error: no matching function for call to 'getline(std::basic_istream<char, std::char_traits<char> >&, std::string, char)'
entry.cpp:35: error: no match for 'operator>>' in 'ins >> entry::getNumber() const()'
istream:131: note: candidates are: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>& (*)(std::basic_istream<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
istream:134: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_ios<_CharT, _Traits>& (*)(std::basic_ios<_CharT, _Traits>&)) [with _CharT = char, _Traits = std::char_traits<char>]
istream:137: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char, _Traits = std::char_traits<char>]
istream:169: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:172: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:175: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:178: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:181: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:184: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:187: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:191: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:194: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long long unsigned int&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:198: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(float&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:201: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(double&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:204: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(long double&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:207: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(void*&) [with _CharT = char, _Traits = std::char_traits<char>]
istream:230: note: std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_streambuf<_CharT, _Traits>*) [with _CharT = char, _Traits = std::char_traits<char>]
main:
CPP / C++ / C Code:
// FILE: TelDirecMenu.cpp
// MENU DRIVEN TELEPHONE DIRECTORY UPDATE PROGRAM

#include "indexList.h"
#include "entry.h"
#include <iostream>
#include <string>
using namespace std;

typedef indexList<entry, 100> telIndexList;

int main()
{
    telIndexList telDirec;           // telephone directory

    // Read the initial directory.
    cout << "Enter the initial directory entries -" << endl;
    telDirec.read(cin);
   
    return 0;
}

indexList.h:
CPP / C++ / C Code:
// File: indexList.h
// Definition of indexed list template class, with function implementation

#ifndef INDEXLIST_H
#define INDEXLIST_H

#include <iostream>
using namespace std;

template <class T, int maxSize>
class indexList
{
public:
	
    indexList()    // constructor
    {
        size = 0;		// list is empty
    }
    void read(istream& ins)
   {
        int numItems;	// input - number of items to read
        T nextItem;		// input - next data item

        cout << "Enter number of list items to read: ";
        ins >> numItems;
        ins.ignore(80, '\n');   // skip newline

        // If numItems is valid, read each list element,
        // starting with first element.
        size = 0;                // The list is empty.
        if (numItems >= 0  &&  numItems <= maxSize)
           while (size < numItems)
           {
              cout << "Enter next item - ";
              ins >> nextItem;
              elements[size] = nextItem;
                  size++;
           }
        else
           cerr << "Number of items " << numItems
               << " is invalid"
                << " - data entry is cancelled!" << endl;
    }
    
       
////////////////////////////////////////////////////////////////////
private:
	T elements[maxSize];  // Storage for elements
	int size;             // Count of elements in list
};

#endif  // INDEXLIST_H
entry.h:
CPP / C++ / C Code:
// File: entry.h
// Definition for a directory entry class

#include <iostream>
#include <string>
using namespace std;

#ifndef ENTRY_H
#define ENTRY_H

class entry
{
public:
    // Member functions
    // constructor
    entry();
   
    string getName() const;
   
    // Get number
    string getNumber() const;

    friend istream& operator >> (istream&, entry&);

private:
    string name;      // Person's name
    string number;    // and phone number
};

#endif  // ENTRY_H
entry.cpp:
CPP / C++ / C Code:
// File: entry.cpp
// Implementation file for class entry
 
#include "entry.h"
#include <iostream>
#include <string>
using namespace std;
 
// constructor
entry::entry()
{
   name = "";
   number = "";
}

// Get name
string entry::getName() const
{
   return name;
}

// Get number
string entry::getNumber() const
{
   return number;
}
// friends

istream& operator >> (istream& ins, 	// INOUT: stream
                      entry& dE)   	// OUT: entry read
{
   cout << "Enter name: ";
   getline(ins, dE.getName(), '\n');
   cout << "Enter number: ";
   ins >> dE.getNumber();

   return ins;
}
I would appreciate any help - I'm getting frustrated beyond belief on this one.

TIA
  #2  
Old 18-May-2006, 15:06
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Problems compiling an example (part 2)


Did you know you can stop the smilies by checking the "Disable smilies in text" box in the Additional Options section below the text input box you enter your post in?

Do you know about the Preview button before you submit your post so you can see what it looks like?
__________________

Age is unimportant -- except in cheese
  #3  
Old 18-May-2006, 15:08
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Problems compiling an example (part 2)


Quote:
Originally Posted by WaltP
Did you know you can stop the smilies by checking the "Disable smilies in text" box in the Additional Options section below the text input box you enter your post in?
No.
Quote:
Do you know about the Preview button before you submit your post so you can see what it looks like?
Yes.
  #4  
Old 18-May-2006, 15:10
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Problems compiling an example (part 2)


Smilies gone. Now can I get some help? Please?
  #5  
Old 18-May-2006, 15:18
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Problems compiling an example (part 2)


As soon as someone that understands your problem arrives, yes. This is not my area of expertise...
__________________

Age is unimportant -- except in cheese
  #6  
Old 18-May-2006, 15:28
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problems compiling an example (part 2)


Quote:
Originally Posted by earachefl
I decided to strip down the code and figure out a piece at a time.
A great move!
Quote:
Originally Posted by earachefl

...I'm getting frustrated...

Been there --- done that! I mean, after all, once the program is running then you can do lots of things to figure out what's wrong. But you can't do anything (much) with nothing.

First of all try to filter the actual error indications from all of that mess of messages:

Code:
entry.cpp:33: error: no matching function for call to 'getline(std::basic_istream<char, std::char_traits<char> >&, std::string, char)' entry.cpp:35: error: no match for 'operator>>' in 'ins >> entry::getNumber() const()'

And what are those lines:

CPP / C++ / C Code:
istream& operator >> (istream& ins, 	// INOUT: stream
                      entry& dE)   	// OUT: entry read
{
   cout << "Enter name: ";
   getline(ins, dE.getName(), '\n'); // <--- Line 33, right?
   cout << "Enter number: ";
   ins >> dE.getNumber(); // <--- Line 35, I think.

   return ins;
}

Line 33: the second argument of this getline() function call should be a std::string. What is it?

Line 35: ins >> extracts something from the stream and stores it where?


Regards,

Dave
  #7  
Old 18-May-2006, 15:45
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Problems compiling an example (part 2)


Thanks for the reply, Dave.
Quote:
Originally Posted by davekw7x
Line 33: the second argument of this getline() function call should be a std::string. What is it?
Well, the way I read his code is that he's calling member function getName, but calling it the wrong way. The second argument of getline() should be the string variable in which to store what's extracted from ins. Instead, it's a function call. It would make more sense to me if the second argument were dE.name so the line would read
CPP / C++ / C Code:
getline(ins, dE.name, '\n');
------and I just put that in and it worked, dagnabit!
Quote:
Line 35: ins >> extracts something from the stream and stores it where?
----and the same works for that.

Like I said earlier, this code came directly from the author's web site, so I don't feel so entirely stupid. I just don't get how the publisher can allow this kind of error to persist through the 4th edition. Plus this is the very first example of friend functions in the book... so very discouraging.

OK, now I guess I have enough fortitude to dig a little more. I'll be Bach....

Thanks, Dave!
  #8  
Old 18-May-2006, 16:58
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,245
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Problems compiling an example (part 2)


Sounds like an email to the author is in order. I'm glad you now have a Handel on the program...
__________________

Age is unimportant -- except in cheese
  #9  
Old 18-May-2006, 17:20
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Problems compiling an example (part 2)


Quote:
Originally Posted by earachefl
Thanks for the reply, Dave.

Like I said earlier, this code came directly from the author's web site, so I don't feel so entirely stupid. I just don't get how the publisher can allow this kind of error to persist through the 4th edition.
.
.
.
OK, now I guess I have enough fortitude to dig a little more. I'll be Bach....

This is a great step forward: Realizing that you have to dig, dig, dig. Don't trust anyone (even me --- especially me!) to give you the exact, precise information that you need every time. I try, as I am sure that the author and editor of your book tried, but, sometimes stuff happens!

It is a little discouraging at first when what is supposed to be a simple example doesn't work, but look at it as a learning experience! (That's what we are here for, right?)

Keep me on your Liszt for further updates!

Regards,

Dave

"Don't ever become a pessimist... a pessimist is correct oftener than
an optimist, but an optimist has more fun, and neither can stop the
march of events. "
--- Robert A. Heinlein
  #10  
Old 18-May-2006, 21:09
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Problems compiling an example (part 2)


Quote:
Originally Posted by WaltP
Sounds like an email to the author is in order. I'm glad you now have a Handel on the program...


Keep me on your Liszt for further updates!
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 4) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems compiling an example earachefl C++ Forum 5 19-May-2006 04:56
[Tutorial] Pointers in C (Part II) Stack Overflow C Programming Language 0 27-Apr-2005 17:36
Chaintech Geforce 5600 FX problems bartster74 Computer Hardware Forum 8 04-May-2004 13:16
ModernBill Order Process Text Box Alignment Problems BobbyDouglas MySQL / PHP Forum 7 30-Jan-2004 21:20

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.