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.
Related
I'm trying to use the stoi function inside a switch but it keeps giving me back this error "[Error] call to non-constexpr function 'int std::stoi(const string&, std::size_t*, int)'"
I've tried multiple things, i even tried converting "PUE" into const int first and putting the variable in there but it still gives me back the same type of error saying that its not a constant expression. Maybe there is another way of writing this switch?
Basically I'm using a barcode scanner to obtain a string and i want to use the substr A to compare with some predefined data and display this on screen.
Thanks.
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
main()
{
string A, B1, B2, C;
string scan;
cout << "Esperando a scan...";
cin >> scan;
cout << "Codigo:" << scan;
A = scan.substr (0,3);
B1 = scan.substr (4,3);
B2 = scan.substr (5,7);
C = scan.substr (13,4);
//comparing
switch(stoi(A))
case stoi("PUE",nullptr,0):
A << "PUERTA";
case stoi("PAN"):
A << "PANEL";
case stoi("LAC"):
A << "LACADO";
cout << "\n Producto:" << A << "\n Acabado:" << B1 << "\n Color:" << B2 << "\n Nº Pedido:" << C;
}
change:
switch(stoi(A))
case stoi("PUE",nullptr,0):
A << "PUERTA";
case stoi("PAN"):
A << "PANEL";
case stoi("LAC"):
A << "LACADO";
to:
switch((A))
case "PUE":
A << "PUERTA";
case "PAN":
A << "PANEL";
case "LAC":
A << "LACADO";
I do not know why you would convert the string to int, you do not need it at all in the code you provided....
Adding on to the suggestions provided by others:
checks performed by the switch statements are static. That means the expressions must be known at compile time. So you can't add any dynamic expressions while using switch case as you did using the stoi() function.
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.
Im trying to learn stl:bitmap, but I'm getting the following error:
Headers added
- bitset
- string
Ive searched other SO posts for this error, but they are not related to bitset.
My Code
int main(){
bitset<size> bs;
bitset<10> bs2(45);
bitset<13> bs3("1100101");
cout << "bs: " << bs << endl;
cout << "bs1: " << bs2 << endl;
cout << "bs2: " << bs3 << endl;
cout << endl;
cout << "bs has " << bs.count() << " set bits" << endl;
cout << bs.size() << endl;
cout << bs2.size() << endl;
cout << bs3.size() << endl;
}
My Error: Error in the last 3 cout statements.
$ g++ test.cpp
test.cpp:28:16: error: expected unqualified-id
cout << bs.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
test.cpp:29:17: error: expected unqualified-id
cout << bs2.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
test.cpp:30:17: error: expected unqualified-id
cout << bs3.size() << endl;
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16
^
3 errors generated.
$
Remove the #define size 16 from your program. I guess you've written this line at the top of your program.
size macro that you'defined conflicts with size() member function. Use const variable instead of macros. You should use const int size=16;
You seem to have a macro defined in test.cpp line 6 which is string replacing your attempt to call the function size.
Your line is actually saying:
cout << bs.16() << endl;
cout << bs2.16() << endl;
cout << bs3.16() << endl;
If you want to use macro's it's good practice to make them as descriptive as possible and use ALL_UPPER_CASE to avoid these type of issues.
e.g.
#define BITSET_DEFAULT_SIZE 16
The error descriptions given to you by the compiler are very descriptive and let you know that a macro is the reason for this problem:
test.cpp:28:16: error: expected unqualified-id
cout << bs.size() << endl; <- this is telling you the starting position of the error
^
test.cpp:6:14: note: expanded from macro 'size'
#define size 16 <- this is telling you a macro is involved, and giving its value
Also, it's not good practice to use using namespace std in your programs due to std containing so many generic named functions. For example, if you create a function called size, you've suddenly overwritten std::size.
Here is a good post pointing out why this is a bad idea
Use
#undef size
immediately after the line
bitset<size> bs;
This will hide your macro and the rest of the code should now compile.
NOTE: This is not a permanent fix. But in case the macro is in a header file which is included in many files, this will give a temporary fix. But using const in C++ is recommended over a macro.
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 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;
}