E0349 no operator ">>" matches these operands - c++

I'm new to C++, so please forgive me if this is extremely simple.
Just trying to make my own independent projects, and then this error happens to me:
E0349 no operator ">>" matches these operands
// usd-to-aud.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <windows.h>
#include <iostream>
// MAIN
int main()
{
std::cout << "Welcome to Currency conversion!\n";
Sleep(1000);
std::cout << "This application is used to convert USD to AUD\n";
Sleep(1000);
int num();
std::cout << "Enter amount in USD: ";
std::cin >> num();
std::cout << num() << " is " << num() * 1.68 << " in AUD.\n";
return 0;
}

Assuming you're not intending to declare a function named num taking no arguments, and calling it repeatedly, simply removing the parentheses at every use of num (declaration included) should fix your problem. If it's not a callable of some sort, you don't want parens here.

Related

this code compiles but doesnt show output when it runs (c++) in visual studio 2015 community

#include <conio.h> // include conio.h file
#include <iostream.h> // or #include<iostream>
int main()
{
int cout = 5;
cout << cout;
return 0;
}
Why does this happen ??
The code compiles correctly but it does not give expected output when it runs
This does not give the output 5 and all other stuff
It also does not give a warning.
The following line declares an int that happens to be called cout (it is not the std::cout stream)
int cout = 5;
The << operator peforms a bit shift.
So
cout << cout;
is only performing a bit shift and not storing the result.
To clarify, have a look at the following program:
#include<iostream>
int main()
{
int cout = 5;
auto shiftedval = cout << cout;
std::cout << "cout's value is " << cout << ", and the result of the bit shift is " << shiftedval << '\n';
return 0;
}
It will output:
cout's value is 5, and the result of the bit shift is 160
What is happening behind the scenes is that operator<< has been overloaded to take an ostream on the left hand side.
By including iostream you get this function and the compiler will know what you mean if you have an ostream to the left of the << operator.
Without a library the << would just simply have been a bitwise shift operator.
Also note that if you had ill-advisedly included using namespace std; or using std::cout then cout would then mean ostream and << would trigger a call to the library operator<< function. If after adding the using declaration above you include another declaration of cout the newly declared name will hide the previous declaration and cout will now be considered an int again and we're back to the bit shift operator functionality being used.
Example:
#include<iostream>
using namespace std; // using std:: at global scope
int main()
{
int cout = 5;
auto shiftedval = cout << cout;
//the following will not compile, cout is an int:
cout << "cout's value is " << cout << ", and the result of the bit shift is " << shiftedval << '\n';
//but we can get the cout from the global scope and the following will compile
::cout << "cout's value is " << cout << ", and the result of the bit shift is " << shiftedval << '\n';
return 0;
}
You are naming your variable cout, which you are confusing with std::cout. Your example preforms a bit shift operation. Try this instead :
int main()
{
int cout = 5;
std::cout << cout;
return 0;
}
Better yet, name your variable anything else to avoid the confusion :
int main()
{
int foo = 5;
std::cout << foo;
return 0;
}
If you don't explicitly declare the std namespace, either by including using namespace std; in your code or calling std::cout then cout resolves to the local variable cout in your main() function.
Even if you do declare using namespace std; cout will still resolve to the local variable instead - this is one reason why a lot of people, books, and tutorials will recommend that you explicitly write std::whatever instead of using the namespace.

C++ Demystified Chapter 13 (2004) Jeff Kent: Ifstream program does not return expected output

C++, expected output that is not outputting is contingent on the existence of students.dat.
If students.dat does not yet exist (and it does not yet), the output would be:
"(infile) = 000000000 (infile.fail()) = 1"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream infile;
infile.open("students.dat");
cout << "(infile) = " << infile << endl;
cout << " (infile.fail()) = " << infile.fail() << endl;
return 0;
}
The error message I receive is the following:
error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'std::basic_ostream<char,std::char_traits<char>>' (or there is no acceptable conversion)
Thanks for the support, Scott Kelly
That code was never really supposed to work (what does it mean to write an input stream to an output stream?!) but used to work "by accident" because streams in C++03 had an implicit conversion to void* which could be used to test the stream's status, and so you could print the value of the void*.
In C++11 the conversion has been replaced with an explicit conversion to bool, so the modern equivalent of that code (which is much clearer what it does) is:
cout << "(infile) = " << (bool)infile << endl;
or:
cout << "(infile) = " << static_cast<bool>(infile) << endl;
I think you are right and this must be a version issue since my book is so old and I am typing in exactly what it says and it isn't working. I guess I should learn from a more updated book.

Syntax Query, calling modules in Visual C++

I have tried to adapt my knowledge of modularity to Visual C++ however, upon what seems to be an endless search scouring for syntax, I simply can't get this right. Basically in this code, the menu is called first, once the user enters their choice (only coded option 1 thus far) to return that value to the main, which then steps into the if statement and calls fahrenheit. I am requesting the syntax for passing by reference, I know C#'s syntax for this, but not Visual C++
Here's the code.
// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void Celsius()
{
}
void fahrenheit()
{
cout << "Success!" << endl; //....Outputs this just to see if the module is being called properly.
}
int menu(int Mystring) //....I was testing this syntax to pass the variable.
{
cout << "What would you like to do : " << endl;
cout << "1) Fanreheit to Celsius" << endl;
cout << "2) Celsius to Fahrenheit" << endl;
cout << "Choice : " ;
cin >> Mystring;
return Mystring;
}
int main()
{
int celsius = 0;
int fahrenheit = 0;
int Mystring = 0;
menu(Mystring); //....Testing this syntax to pass Mystring to menu.
if (Mystring == 1) //....I was hoping the menu would return Mystring as value = 1.
{
fahrenheit(); //.......I want this to call fahrenheit module if Mystring = 1
}
}
The "things" you're talking about aren't called modules, but functions. That's a pretty big difference and I think you should know it, since you won't understand nearly any article without that knowledge.
That being cleared, the problem in your code is, that you pass the variable by value (int menu(int Mystring)), while - in order to change it inside the function - you need to pass it by reference or pointer:
int menu(int &Mystring)
There are plenty of articles about functions in C++. You should check them out probably.

C++ Array passed by reference, but how to understand this?

The arrays are passed by reference. Any changes made to the array within the function changeArray will be observed in the calling scope (main function here).
However the codes below print 0 1 in the 1st cout, and print 2 in the 2nd "cout". What I don't understand is that why the first cout prints the original value of array[0]=1 instead of the changed value of array[0]=2?
Thanks a lot.
#include <iostream>
using namespace std;
int changeArray(int array[]) {
array[0]=2*array[0];
return 0;
}
int main() {
int array[]={1,2,3,4};
cout << changeArray(array) << " " << array[0] << endl;
cout << array[0] << endl;
return 0;
}
To make sure that the compiler doesn't reorder the execution:
cout << array[0] << endl;
changeArray(array);
cout << array[0] << endl;
This prints 1 and then 2.
The C++ compiler is allowed to optimize the code by reordering the execution of code within a single expression (e.g. cout << changeArray(array) << " " << array[0] << endl). To avoid that, and to make sure changeArray gets called first, you need to split your expression to separate statements, e.g. by using the semicolon (;). Everything before the semicolon gets executed before anything after the semicolon can start.

Program always returns binary '>>' : no operator found which takes a left-hand operand of type error

So I've been set a task to create a temperature converter in C++ using this equation:
Celsius = (5/9)*(Fahrenheit – 32)
and so far I've come up with this (I've cut out the 10 lines worth of comments from the start so the code posted begins on line 11, if that makes any sense)
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
float celsius;
float farenheit;
std::cout << "**************************" << endl;
std::cout << "*4001COMP-Lab5-Question 1*" << endl;
std::cout << "**************************" << endl << endl;
std::cout << "Please enter a temperature in farenheit: ";
std::cin >> farenheit >> endl;
std::cout << "Temperature (farenheit): " << endl;
std::cout << "Temperature (celsius): " << celsius << endl;
std::cin.get();
return 0;
}
Everytime I try to run this program I get a heap of errors with this one appearing every time:
1>m:\visual studio 2010\projects\week 5\week 5\main.cpp(26): error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::basic_istream<_Elem,_Traits>' (or there is no acceptable conversion)
I've tried everything I can think of to get rid of this error but it reappears every time, any idea on how to fix this?
std::cin >> farenheit >> endl;
This statement is invalid. remove >> endl from it.
std::cin >> farenheit ;
This statement is only valid when you cout something. Like here.
std::cout << farenheit << endl ;
The reason is that endl is the end-line character used to output a new line. So only the output stream accepts it. You can look up more about return values and prototypes of cin and cout here.
http://en.cppreference.com/w/cpp/io
std::endl is actually a function and the operator to stream into it is not defined. Yes it's a confusing error message as it is complaining about the LHS not the RHS.
Its implementation is something like:
namespace std {
std::ostream& endl( std::ostream& os )
{
os << '\n';
os.flush();
return os;
}
}
Streaming has then defined something like this:
namespace std {
std::ostream & operator<<( std::ostream & os, (std::ostream& *)(std::ostream&) func )
{
return func(os); // or is it (*func)(os)
}
}
It's actually quite a "powerful" feature of iostream, because you can then write a function with that signature and stream the function into your stream to do stuff with the iostream.
That is in fact a similar concept as to how the <iomanip> library also works (although that uses objects).