ambigous overload of istream operator when default constructor used

This is a discussion on ambigous overload of istream operator when default constructor used within the C++ Programming forums, part of the General Programming Boards category; When I use the default constructor for my book object I get an error trying to compile. Code: #include <stdlib.h> ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    450

    ambigous overload of istream operator when default constructor used

    When I use the default constructor for my book object I get an error trying to compile.
    Code:
    #include <stdlib.h>
    
    #include "book.h"
    #include "author.h"
    
    int main(int argc, char *argv[])
    {
      map<string,Book> library;
      char c;
      cout << "Press Enter to Add a Book Q to continue.";
      cin.get(c);
      while ((c!='Q') && (c!='q')){
          Book tempBook();
          
          //error here but if I use Book tempBook("Some Book") no error
          //21 C:\Dev-Cpp\Book1\main.cpp  ambiguous overload for `std::istream& >> Book (&)()' operator
    
          cin >> tempBook;
          library.insert(make_pair(tempBook.get_ISBN(),tempBook));
          cout << "Press Enter to Add a Book Q to continue.";
          cin.ignore();
          cin.get(c);
      }
      
      map<string,Book>::iterator pos;
      for (pos=library.begin();pos !=library.end();++pos){
        cout << "--------------------------------------------\n";
        cout <<pos->second;
      }
      
      
          
      system("PAUSE");	
      return 0;
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    Book tempBook();
    try
    Code:
    Book tempBook;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    450
    Thanks that worked another question how can I use a pointer to the book as in:
    Code:
    int main(int argc, char *argv[])
    {
      map<string,Book> library;
      char c;
      cout << "Press Enter to Add a Book Q to continue.";
      cin.get(c);
      while ((c!='Q') && (c!='q')){
          Book *tempBook=new Book;
          cin >> *tempBook;
          library.insert(make_pair(*tempBook.get_ISBN(),*tempBook));
          delete tempBook;
          cout << "Press Enter to Add a Book Q to continue.";
          cin.ignore();
          cin.get(c);
      }

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    This line is wrong:

    Code:
    library.insert(make_pair(*tempBook.get_ISBN(),*tempBook));
    It should be:

    Code:
    library.insert(make_pair((*tempBook).get_ISBN(),*tempBook));
    You need the (), because I believe the . has a higher precedence than the dereference operator.

    OR you can also do:

    Code:
    library.insert(make_pair(tempBook->get_ISBN(),*tempBook));


    If I said anything wrong, please someone correct me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utilizing another compiled program for a task.
    By kotoroshinoto in forum C Programming
    Replies: 6
    Last Post: 06-03-2008, 01:43 PM
  2. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM
  3. Having trouble with operator*=()
    By Lurker in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2003, 02:03 PM
  4. istream overload
    By Trauts in forum C++ Programming
    Replies: 6
    Last Post: 05-06-2003, 08:29 PM
  5. Replies: 5
    Last Post: 11-24-2002, 10:05 PM

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