-1

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

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
gabrupro
  • 39
  • 4
  • 5
    First of all never write shortening macros like that. It doesn't make the code "better", quite the opposite. Secondly please [edit] your question to include a proper [mcve], and add a comment in the source on the line where you get the error. And also include a *full* and *complete* copy-paste (as text) of the error output. – Some programmer dude Jan 16 '21 at 11:50
  • Also please take some time to read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Jan 16 '21 at 11:51
  • 3
    What is line 3? If I count correctly in the shown code line 3 is empty. The problem is either the missing overload for `operator<<(ostream &,vector)` (see also [this question](https://stackoverflow.com/questions/32785418/c-is-it-possible-to-cout-a-whole-vector?noredirect=1&lq=1) or the missing overload for `operator<(vector, const char[])` caused by a typo (`<` should be `<<`). – Lukas-T Jan 16 '21 at 11:51
  • 1
    Following up on the comment by @churill, did you (gabrupro) actually *read* the full and complete error message? – Some programmer dude Jan 16 '21 at 11:52
  • Yeah, it probably complains on attempt to `cout` a vector object – Alexey S. Larionov Jan 16 '21 at 11:53
  • @churill The "shift" operators have higher precedence so it would really be `operator<(std::ostream&, const char[])`. Not that it really matters much in this case though. :) – Some programmer dude Jan 16 '21 at 11:54
  • 5
    Fixing the markdown of your code I found that hair-raising `#include ` . Please don't. https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – Yunnosch Jan 16 '21 at 12:00
  • 1
    This doesn't address the question, but if you're just starting out in C++ you need to learn good habits. Get rid of that `#include`; replace it with `#include ` and `#include `. Get rid of `using namespace std;`. Get rid of that macro. The code in the body of `main` should be `std::vector hello; hello.push_back(3); hello.push_back(2); std::cout << hello[0] << ' ' << hello[1] << '\n';`. – Pete Becker Jan 16 '21 at 15:11
  • 1
    Also, as a beginner, you don't need to be "learning macros". Don't use them. – Pete Becker Jan 16 '21 at 15:12
  • Actually, I don't aim to improve my coding style here. I know we don't use such things in development type of coding, but I am aiming for competitive programming which asks for and macros. Shoulda added this is the OP – gabrupro Jan 17 '21 at 06:27
  • And yes, this was the full error message...I didn't copy and paste it but I wrote it verbatim, except the first e in Error, which should have been lower case – gabrupro Jan 17 '21 at 06:28
  • Competetive programming doesn't _ask_ for ``. It's just that many sites use a toolchain where that file exists and it happens to include a )(/#"¤-load of "useful" headers - most of which you don't need. – Ted Lyngmo Jan 21 '21 at 10:59
  • Ok...didn't know about that. And yeah, even the top level competitive programmers in the world use the `````` header. So I don't think it is bad to use it in such an environment. – gabrupro Jan 27 '21 at 18:18

1 Answers1

1

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.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54