Thread: Error returning istream in class implementation.

  1. #1
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Error returning istream in class implementation.

    I'm working on a class called coord that's just two dimensional coordinates. The only data members are the x-coordinate and the y-coordinate. I wrote a function to input a coord like so, but the compiler is giving me this error:
    Code:
    	is >> char1 >> X >> char2 >> Y >> char3;
                        ^
    File "Hard Drive:Desktop Folder:Comp Prgms:My RPG:coord.cpp"; line 108
    #Error: ambiguous type conversion
    Had: ios::operator const void*() const
    and: ios::operator void*()
    I don't know what the problem is. I followed the same format to implement this function as my textbook used to implement a rational number class. Here's the implementation of my input function:
    Code:
    istream& operator >> (ostream &is, coord &c)
    {
    	char char1, char2, char3;
    	int X = 0, Y = 0;
    	is >> char1 >> X >> char2 >> Y >> char3;
    	assert(char1 == '(');
    	assert(char2 == ',');
    	assert(char3 == ')');
    	coord pair(X,Y);
    	c = pair;
    	return is;
    }
    Any help will be much appreciated. This is my first class that I have written, and I think this is the last bug in it *crosses fingers*. Thanks in advance.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Did the code you posted compile?

    Code:
    istream& operator >> (ostream &is, coord &c)
    // should be
    istream& operator >> (istream &is, coord &c)
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Originally posted by SilentStrike
    Code:
    istream& operator >> (ostream &is, coord &c)
    // should be
    istream& operator >> (istream &is, coord &c)
    Oh, my god, I feel so stupid. *hangs head in shame* I can't believe I made such a silly little mistake. Thanks, man, now it compiles just fine.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146

    Question Another question about my coord class

    How do I assign an ordered pair to a coord? Here's what I'd like to be able to do:
    Code:
    coord ordered_pair;
    ordered_pair = (1,2);
    I know I can already do this:
    Code:
    coord ordered_pair(1,2);
    but, that only works when I first declare the variable. After that, I'm unable to assign to a coord in that manner. I think that all I'll need to fix this problem is the function declaration for the assignment I want. Once again, thanks in advance.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You would need to alter the langauge to allow things like this to do what you want.
    Code:
    ordered_pair = (1,2);
    That will basically evulate the statement 1 first, and then evaulate and return 2. Run the following code in case you are confused.

    Code:
    #include <iostream>
    
    int f() {
      std::cout << "f()\n";
      return 1;
    }
    
    int g() {
      std::cout << "g()\n";
      return 2;
    }
    
    int main()
    {
      int b = (f(),g());
      std::cout << b << std::endl;
      return 0;
      
    }
    You can either make a member, set_coords or soemthing, that takes two parameters, or use the the constructor and assignment. IE,

    Code:
    order_pair = coord(a,b);
    // or
    order_pair.set_coords(a,b)
    The second way is likely to be minimally faster.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    you could always overload operator() to take 2 values so that you could do this:

    ordered_pair( Newx, Newy );

    but that would most likely just be confusing. Best off just using a "Set" method

  7. #7
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Thanks, guys. That's just the help I needed. My first class is now complete. Yay!
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    My coord class is done and tested. I'd now like to make it available to you for criticism and suggestions. I've included a library driver to test the code. Just so you know, my only current plans for this class is to track the location of the player in a text based RPG (I know, not much fun, but I've got to start somewhere). Thanks in advance.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    It'd probably help if I included the file, huh? Well, here it is:
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inherite nonvirtual class functionality
    By DrSnuggles in forum C++ Programming
    Replies: 2
    Last Post: 04-30-2009, 01:52 PM
  2. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM