Solved

Error C2678: binary '!=': no operator found...

Posted on 2016-11-11
4
48 Views
Last Modified: 2016-11-12
Hello all.

I'm doing some basic work with C++ streams on MS Visual Studio 2015, and I am encountering error C2678 when I attempt to compile this code:

#include <iostream>

using namespace std;

int main()
{
	char ch;
	cout << "enter a phrase: ";
	while ( cin.get(ch) != 0 )
	{
		if (ch == '!')
			cin.putback('$');
		else
			cout << ch;
		while (cin.peek() == '#')
			cin.ignore(1, '#');
	}
	return 0;
}

Open in new window


For line 9, I receive this error output:
C2678      binary '!=': no operator found which takes a left-hand operand of type 'std::basic_istream<char,std::char_traits<char>>' (or there is no acceptable conversion)

Many thanks in advance!
0
Comment
Question by:blossompark
  • 3
4 Comments
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 41884329
According to this link, you are using a form of get that returns basic_istream& and I did not find a != operator that addresses a LHS of basic_istream& and a RHS of int.
http://www.cplusplus.com/reference/istream/basic_istream/get/

The revised while test condition should get your program to build.
But you still have a peek to deal with when you reach the EOF. Here is one approach. Adding in a test for EOF in a peek() allows your program to run smoothly in VS until you hit the eof state. At that point, to emulate an eof in the Visual Studio console, you type in the single char, ctrl/Z, followed by the <ENTER>, at which point you hit the break and your program ends.
	while (cin.get(ch) )
	{
		if (cin.peek() == std::char_traits<char>::eof())
			break;

Open in new window

or you can combine these three lines as follows:
	while( ( ch = cin.get() ) != std::char_traits<char>::eof() )

Open in new window

0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 500 total points
ID: 41884333
Just curious - I see that you just started C++ questions after a good deal of DB questions. Did you volunteer to take on new projects where you have to figure this stuff out?
0
 

Author Comment

by:blossompark
ID: 41884618
Thank you, it worked like a charm.

And, I'm actually borrowing this account at the moment. The DB questions aren't mine (though I hope to work my way through that subject as well!)

Many thanks.
0
 
LVL 32

Expert Comment

by:phoffric
ID: 41884808
I think that you can create your own account for free and ask your own questions for free. There is also a premium membership that gives you extra privileges.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question