Thread: Operator >> problems

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    11

    Lightbulb Operator >> problems

    Ok guys. I have a class String, and i need to put the operator >> in that class. so i do this:

    istream & operator>>(istream &in,String &str)
    {
    char string[30];
    in.getline(string,30,'\n');
    str=string;
    return in;
    }

    but when i do:

    void main()
    {
    String x;
    cin>>x;
    }

    i have this error
    error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'String' (or there is no acceptable conversion)

    why?? I have the operator << and it works with no problems. Any idea??

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Talking

    Did you make the operator>> a friend in your class?
    Mr. C: Author and Instructor

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Mister C
    Did you make the operator>> a friend in your class?
    That shouldn't matter here from the error he's getting.

    He proly didn't include a declaration of the function before main.

    Also, use int main not void main.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    11

    Angry Well it woks

    Ok ok, i put a declarition of the function before main and it don't give me errors, but when a want to read a string from the stdin, it just read one letter, for exeample i want to write My car is red, but when i write the M the program just read that letter and don't read the rest of the string....

    What can be???
    Is that C++???

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    Would this help u detect your bug

    This code works, now compare and figure out where u r going wrong

    Code:
    #include <iostream>
    
    class String
    {
    	char str[30];
    public:
    	String()
    	{
    		str[0] = 0;
    	}
    
    	friend std::istream& operator>>(std::istream& in, String &ob);
    	friend std::ostream& operator<<(std::ostream& out, String &ob);
    };
    
    std::istream& operator>>(std::istream& in, String &ob)
    {
    	in.getline(ob.str,30,'\n');
    	return in;
    }
    std::ostream& operator<<(std::ostream& out, String &ob)
    {
    	out << ob.str;
    	return out;
    }
    
    int main()
    {
    	String s1;
    
    	std::cin >> s1;
    	std::cout << s1;
    
    	return 0;
    }
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  6. #6
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    hi shiv_tech_quest looks like you are from Bangalore.. I am from Mysore.. verry close...

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    11

    Talking Thanks, i just have one solution....

    Thanks shiv_tech_quest now that wokrs....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  5. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM