I have been assigned to implement a string class and I am having a hard time with the >> operator overload.

Header File:

std::istream& operator >>(std::istream& ins, obstring& target);

Implementation:

std::istream& operator >>(std::istream& ins, obstring& target)
	// Postcondition: A string has been read from the istream ins, and the
	// istream ins is then returned by the function. The reading operation
	// skips white space (i.e., blanks, newlines, tabs) at the start of ins.
	// Then the string is read up to the next white space or the end of the
	// file. The white space character that terminates the string has not
	// been read.
	// Library facilities used: iostream
        {
		char a;
		ins >> a;
		
		while ((a != " ") || (a != ""))
		{
			target += a;
			ins >> a;
		}
	
		return ins;
	}

When compiling I get the error:

ISO C++ forbids comparison between pointer and integer

This is referencing the line 13 in the code block above.

Any insight into what could be the problem here will be appreciated.

a is a char and " " is considered a string. When comparing something with a char, use the ' ' (single quotes).

At least two problems with your program.

  1. The space is not the only character that is considered white space. It also consists of tabs, back spaces, and returns '\n'. The safest way to check for all these is to use the macro isspace().
  2. You can not use fstream's >> operator to read white space because >> always skips white space. What you want is to use fstream's get() method which does uninterpreted reads, much like calling read() for a single character.

Ah, thanks for the help everyone. Problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.