I am trying to use cout, i get this message no operator ">>" matches these operands - iostream

I am getting the error message "no operator ">>" matches these operands".
-In the code I include the header file iostream.
-I delcare that i am using the formatting(?not sure if that's correct way to state) using namespace std).
-I make room for the variable using an assignment operator.
-I then use the insert num1 to be output with the stream insertion operator.
can someone please tell me where I went wrong.
why is the stream insertion operator not being recognized. the >> is underlined with a red squiggly line in my ide. I am using visual studio 2019 if that makes a difference.
#include <iostream>
using namespace std;
int main()
{
int num1 = 1;
cout >> num1;
return 0;
}

It has been awhile. You are using the wrong operator. >> is used with the cin object to read but << is used with cout to write:
cout << num1;

Related

VSCode adds random percentage

Everytime I use the terminal to print out a string or any kind of character, it automatically prints an "%" at the end of each line. This happens everytime I try to print something from C++ or php, havent tried other languages yet. I think it might be something with vscode, and have no idea how it came or how to fix it.
#include <iostream>
using namespace std;
int test = 2;
int main()
{
if(test < 9999){
test = 1;
}
cout << test;
}
Output:
musti#my-mbp clus % g++ main.cpp -o tests && ./tests
1%
Also changing the cout from cout << test; to cout << test << endl; Removes the % from the output.
Are you using zsh? A line without endl is considered a "partial line", so zsh shows a color-inverted % then goes to the next line.
When a partial line is preserved, by default you will see an inverse+bold character at the end of the partial line: a ‘%’ for a normal user or a ‘#’ for root. If set, the shell parameter PROMPT_EOL_MARK can be used to customize how the end of partial lines are shown.
More information is available in their docs.

why the output of the auto variable displays something not related to type?

I tried a example on Auto for variable initialization and STL in C++. For normal variable, type was printed using : typeid(var_name).name() to print i (integer) / d(float) / pi(pointer) which works fine.
But while working on STL,
`#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> st;
st.push_back("geeks");
st.push_back("for");
for (auto it = st.begin(); it != st.end(); it++)
cout << typeid(it).name() << "\n";
return 0;
}
`
which gives output like,
`N9__gnu_cxx17__normal_iteratorIPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIS6_SaIS6_EEEE
N9__gnu_cxx17__normal_iteratorIPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIS6_SaIS6_EEEE`
and I am unable to understand the output logic behind it, can anyone explain why it is giving output like this? and thanks in advance
That's the "name mangled" version of the name of the type of it. typeinfo::name() is not required by the standard to return a name in human-readable format (a shortcoming IMHO) and GCC doesn't do so.
To get the actual, human-readable name, you need to call the abi::__cxa_demangle() function provided by GCC, but note that this is non-portable so if your project needs to work on different compilers you'll need to wrap it appropriately.

Is it ok to use ternary operator in C++ streams?

The following code:
#include <iostream>
using namespace std;
struct A{int number=10;};
int main()
{
A* a = new A();
cout<<"A val: "<< a==nullptr?"":a->number<<endl;
}
compiling on gcc 4.7 with c++11 gives:
error: invalid operands of types 'int' and '' to binary 'operator <<'
I do not understand why, what would be to proper way to do it? I want to keep null checks as short as possible as I expect them to be very common.
First: yes, you can use the ternary operator for an std::ostream, but beware of operator precedence. If you are going to do so, you need to do something like this:
cout << "My name is: " << (my != nullptr ? my->name() : "don't know") << '\n';
In other words, encapsulate the ternary expression in parentheses.
Second, the second and third operands must be convertible to the same type. In other words, your example won't work because you are trying to insert a string literal ("") if a is null, or the actual number (a->number, which is of type int) if a is not null.
Third, you need to fix the syntax. But #quamrana already addressed that one.
You are finding that operator << binds differently than you expect.
Use:
cout << "A val: " << (a==nullptr ? "" : a->number) << endl;
(or have you just made a typo and missed the : from ?:)

Using boost::multiprecision as data type for blitz++

I am trying to do arbitrary precision arithmetic combined with the nice array syntax from blitz++. My problem is, that the generic math functions like cos, exp and so on don't work:
#include <blitz/array.h>
#include <boost/multiprecision/float128.hpp>
using namespace boost::multiprecision;
using namespace blitz;
int main() {
float128 a = 1;
a = cos(a);
cout << a << endl;
Array<float128,3> myarray(2,3,4);
myarray = 1;
myarray = cos(myarray);
cout << myarray;
}
g++ test.cpp -lquadmath -o test
The first block, using only float128 but not blitz, works fine. The second block with blitz however won't do the cos(myarray). The compiler seemingly figures out the iteration, but can not find the function to do the actual cos(x) for the values: Compiler error log
I would also like to use boost::multiprecision::mpfr, but one thing at a time. I hope someone can help.
I have found a solution, but it involves patching blitz. I have written this patch for blitz-0.10 and with the modified blitz the above code just works.

Using cin for input complex number in C++11

#include <iostream>
#include <complex>
using namespace std;
int main(){
complex<double> p;
cin >> p.real() >> p.imag();
}
In g++4.7.2 it works successfully, but in C++11 failed to compile. Why?
It gives me following error message:
prog.cpp: In function ‘int main()’:
prog.cpp:7:19: error: no match for ‘operator>>’ in ‘std::cin >> p.std::complex<double>::real()’
Full version: http://ideone.com/M3BhVR
You can do it even simpler like this:
cin >> p;
Format must be: (real,imag) (see: here)
Or you can do the following:
double real, imag;
cin >> real >> imag;
complex<double> p(real, imag);
The problem is that p.real() and p.imag() do not return references, so they are temporary values and it doesn't make sense to write to temporaries.

Resources