I came across a piece of code that does basically the following:
#include <iostream>
using namespace std;
int main()
{
cout << cerr << " Hi.";
return 0;
}
Output:
0x601088 Hi.
First of all, why would anyone do 'cout << cerr' it does not make sense.
Second of all, what is the meaning of the output above?
Worth to mention that on my machine the above code compiles and executes without errors.
However a much more complex code (doing the same thing as above) on a different machine (server ssh connection) running the same version of gcc 5.4.0, produces this error when doing make (shortened for clarity):
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::ostream {aka std::basic_ostream<char>}’)
cout << cerr << "DB: Field " + e.table + "[" + e.index + "]." + e.field
Any thoughts on this?
Until c++11, std::basic_ios offered an implicit conversion to void*. This code won't compile with c++11 or later. You basically have this, which compiles with older versions of gcc :
#include <iostream>
int main()
{
void * x = std::cerr;
std::cout << x << " Hi.";
return 0;
}
Related
I am starting to code in c++.
I was learning macros when this happenned:
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
int main() {
vector<int> hello;
hello.PB(3);
hello.PB(2);
cout << hello < "\n";
}
My compiler shows, pointing to line 3:
Error: statement cannot resolve address of overloaded function
For your code I get problems with the < instead of << and what I assume the main problem:
error: no match for ‘operator<<’ (operand types are ‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::vector<int>’)
cout << hello << "\n";
It is telling you that there is no known way to output a whole vector to cout.
The simple way to fix that is
cout << hello[0] << " " << hello[1] << "\n";
This gets you an output of
3 2
The more complex way, with more convenient result, is to do the overloading yourself accordingly.
I'm writing a short program to convert a string (consisting of numbers) to an integer. The code runs ok but I keep getting an odd intellisense error on the "int" part of the int main() declaration. The error text is: this declaration has no storage class or type specifier
and shows the first two letters (the "in") in white and the last (the "t") in the yellow that recognized function names are usually tagged with.
Does anyone know what this might be? Is it just an intellisense anomaly or is there something wrong with my code?
Here's the full code listing:
#include <iostream>
#include <string>
int stringConvert(std::string);
int main()
{
std::string str("123");
int stringNum = stringConvert(str);
std::cout << str << " --> " << stringNum << std::endl;
return 0;
}
int stringConvert(std::string stringIn)
{
int n = std::stoi(stringIn);
std::cout << "String conversion completed" << std::endl;
return n;
}
I am working with cryptonote repo for a project and am at the point where I need to compile the binaries.
When I run make, I get the following error:
/Documents/huntcoin/src/CryptoNoteCore/SwappedMap.h:185:14: error: invalid operands of types ‘<unresolved overloaded function type>’ and ‘const char [24]’ to binary ‘operator<<’
std::count << "SwappedMap cache hits: " << m_cacheHits << ", misses: " << m_cacheMisses << " (" << std::fixed << std::setprecision(2) << static_cast<double>(m_cacheMisses) / (m_cacheHits + m_cacheMisses) * 100 << "%)" << std::endl;
~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
I am not super familiar with C++ and am sure it might be a simple parenthesis error, but it could be something more.
For some context, the previous make error I got was that std::cout was not defined, which I assumed was just a typo for count. Maybe that was wrong as well.
Any help with C++ or cryptonote would be much appreciated!
You've got an extra n that is causing you trouble. The code should read:
std::cout << "SwappedMap c.....
std::cout is the default console output (console output) stream while std::count is not defined
The std::cout is defined in a header file iostream so all you need to do is put this line of code next to other #include statements at the top of your file:
#include <iostream>
Cheers
I am coding in C++ in Code Blocks 13.12 with TDM-GCC compiler version 4.8.1 on Windows 10 64-bit, but while in any other PC I have used (both at school and university wtih Win7 64-bit and Win8 64-bit, respectively) there were no problems with debugging and running programs, in my case I am not even able to compile simple "for" loop. I tried all versions of CB, withou success.
Here is the siplest code, that gives errors:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
for (int i = 0,i < 9,i++)
cout << "Test, ",i << endl;
return 0;
}
As you can see, it is a sightly modified basic console application in C++.
The errors I get are following:
error: expected initializer before '<' token
error: expected ';' before '<' token
error: expected primary-expression before '<' token
error: expected ';' before ')' token
error: invalid operands of types 'int' and '<unresolved overloaded function type>' to binary 'operator<<'
I tried everything - form installing different versions of code blocks, to disableing anti-virus software.
Can you tell me what have I been doing wrong?
Your for loop syntax is wrong.
It should be:
for (int i = 0; i < 9; ++i) and also add the {} after the ).
And also
cout << "Test, " <<i << endl; //and also the concatenation was wrong
Hope it helps.
The curly braces are not necessary if the loop body contains only one expression.
You should change commas to semicolons between the braces after "for". "for" is not a function, it is a keyword and the statement "int i=0" is not a parameter.
Here is the right code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!" << endl;
for (int i = 0; i < 9; i++)
cout << "Test, " << i << endl;
return 0;
}
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.