0

I am having trouble with printing a string in C++.

I know there are lots of topics about this matter on SO, but most say to include <string>, <iostream> or namespace std. But I did all of that but still encounter the issue. Here is my code and the error.

#include <iostream>
#include <string>
using namespace std;
//...
void affiche_date(int annee, int nbjours) {
    string mois;
    if (nbjours>31) {
         mois = "avril";
         nbjours -= 31;
    } else {
        mois = "avril";
    }
    cout << "Date de Paques en " << annee << " : " << nbjours << " " << mois << end;
}

int main() {
    int annee ( demander_annee() ) ;
    int jour ( date_paques(annee) );
    affiche_date(annee, jour);
}

Here is the error I get when I compile:

"error: no match for ‘operator<<’ (operand types are ‘std::basic_ostream<char>’ and ‘<unresolved overloaded function type>’)" 

This error is coming from the line with the cout in the function I gave you.

I am using Geany on linux Ubuntu and using c++11.

Thanks for you help

AndyG
  • 39,700
  • 8
  • 109
  • 143
A.david
  • 37
  • 10
  • 5
    I think you meant `std::endl` – AndyG Nov 09 '17 at 18:44
  • 1
    You forgot a letter. Voting to close as typo. ("endl" in your book is *not* a typo. Only The Dane knows why it's spelled that way.) – molbdnilo Nov 09 '17 at 18:45
  • 1
    you also have function definitons inside main - thats not right – pm100 Nov 09 '17 at 18:47
  • 2
    @pm100: Those are declarations that stray really close to the most vexing parse. `annee` and `jour` are declared as integers that are initialized with the result of the function calls in parentheses. Edit: [Demo](https://wandbox.org/permlink/L5XnaDC3TxAws4kR) – AndyG Nov 09 '17 at 18:51
  • 1
    @AndyG oh yes you are right. They just look so wrong – pm100 Nov 09 '17 at 18:53
  • Thanks everyone, I fell stupid as indeed it was a typo for endl. I've been looking at this for an hour... Since it is the first time I tried cout with a string and I saw many people having the same issue I assumed it came from there. Sorry and thanks for the quick answer – A.david Nov 09 '17 at 18:55
  • @molbdnilo: It's a typo, but it feels like this won't be the first time someone makes this innocent mistake. I wonder if we can reword the title to be more relevant instead? – AndyG Nov 09 '17 at 18:55
  • It's off topic but i'am trying to learn, so why do you annee and jour should be declared differently> They are initialized with the value returned by values returned by other functions. It works but it should not be done? – A.david Nov 09 '17 at 19:02
  • @AndyG Could be an idea, I can't even find a dupe. Something about "trying to print newline with `std::end"`? – molbdnilo Nov 09 '17 at 19:04
  • 2
    @molbdnilo -- `endl` is short for **end of line**, just as `ends` its short for **end of string**. – Pete Becker Nov 09 '17 at 19:05
  • @A.david Regarding the declaration style: you will sooner or later encounter the [Most Vexing Parse](https://stackoverflow.com/questions/1424510/most-vexing-parse-why-doesnt-a-a-work) and spend ages trying to figure out what's wrong. – molbdnilo Nov 09 '17 at 19:06
  • Thanks for the link. It is not very clear yet but I'm going to read about this issue so I'll try not to fall in the trap. – A.david Nov 09 '17 at 19:13

1 Answers1

5

std::end() is a function for getting an iterator to the end of a container.

You meant to use the std::endl stream manipulator instead.

Note: avoid using namespace std; in your actual code, either take advantage of using directives to bring in only what you need, or favor qualifying names with their namespaces, like I have here.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
AndyG
  • 39,700
  • 8
  • 109
  • 143
  • Thanks for your answer. As you said it was only a typo... But what you said is interesting. Could you please explain me why not to use using namespace std? I used it only because I am following a tutorial and they say to use it – A.david Nov 09 '17 at 18:57
  • @A.david https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – AndyG Nov 09 '17 at 19:01
  • great. Thanks for your help – A.david Nov 09 '17 at 19:05