22

When I types the following as a stand-alone line:

std::endl;

I got the following error:

statement cannot resolve address for overloaded function

Why is that? Cannot I write std::endl; as a stand-alone line?

Thanks.

Simplicity
  • 47,404
  • 98
  • 256
  • 385

8 Answers8

20

std::endl is a function template. Normally, it's used as an argument to the insertion operator <<. In that case, the operator<< of the stream in question will be defined as e.g. ostream& operator<< ( ostream& (*f)( ostream& ) ). The type of the argument of f is defined, so the compiler will then know the exact overload of the function.

It's comparable to this:

void f( int ){}
void f( double ) {}
void g( int ) {}
template<typename T> void ft(T){}

int main(){
  f; // ambiguous
  g; // unambiguous
  ft; // function template of unknown type...
}

But you can resolve the ambiguity by some type hints:

void takes_f_int( void (*f)(int) ){}

takes_f_int( f ); // will resolve to f(int) because of `takes_f_int` signature
(void (*)(int)) f; // selects the right f explicitly 
(void (*)(int)) ft; // selects the right ft explicitly 

That's what happens normally with std::endl when supplied as an argument to operator <<: there is a definition of the function

 typedef (ostream& (*f)( ostream& ) ostream_function;
 ostream& operator<<( ostream&, ostream_function )

And this will enable the compiler the choose the right overload of std::endl when supplied to e.g. std::cout << std::endl;.

Nice question!

xtofl
  • 40,723
  • 12
  • 105
  • 192
  • 1
    `std::endl` is a single function template, not a function or set of overloaded functions. If it were just a single function the statement `std::endl;` would be fine (if pointless). – CB Bailey Jan 30 '11 at 13:22
  • @Charles Bailey: corrected for that; would it change the reasoning a lot? I don't think so. The ambiguity is resolved using the `operator <<` in casu. – xtofl Jan 30 '11 at 16:04
  • It makes the reasoning more applicable, if anything. – CB Bailey Jan 30 '11 at 16:26
  • 1
    Your obfuscating the real answer. It just needs a parameter. – Martin York Jan 30 '11 at 18:05
  • @Martin York: my guess was OP wanted to know "what did the compiler mean", not "how should I use `std::endl`". I didn't know, either. Indeed, `std::endl` _is_ a function, as stated clearly in this and other answers, but the interesting thing is I never needed to ask myself that - it just worked. In the solitary, nonsensical expression `std::endl;`, the compiler suddenly didn't have a way to know what to do. I think it's a very good question, and I think I learned something by answering it, and that OP learned something by reading it. – xtofl Jan 30 '11 at 22:06
  • @Martin York: ... and you must admit that most people never thought about giving `std::endl` a parameter. – xtofl Jan 30 '11 at 22:07
  • Very interesting! Especially because it's the same for std::flush, which I didn't expect to use the << operator. – orzechow Oct 24 '13 at 20:50
4

The most likely reason I can think of is that it's declaration is:

ostream& endl ( ostream& os );

In other words, without being part of a << operation, there's no os that can be inferred. I'm pretty certain this is the case since the line:

std::endl (std::cout);

compiles just fine.

My question to you is: why would you want to do this?

I know for a fact that 7; is a perfectly valid statement in C but you don't see that kind of rubbish polluting my code :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I made it that way for the seek of readability, and that tells a newline has been inserted after some statement. But, seems it is invalid to write it the way I did. Thanks – Simplicity Jan 30 '11 at 13:09
4

std::endl is a manipulator. It's actually a function that is called by the a version of the << operator on a stream.

std::cout << std::endl
// would call 
std::endl(std::cout).
Martin York
  • 257,169
  • 86
  • 333
  • 562
hbn
  • 1,846
  • 2
  • 12
  • 12
4

std::endl is a function template. If you use it in a context where the template argument cannot be uniquely determined you have to disambiguate which specialization you mean. For example you can use an explicit cast or assign it to a variable of the correct type.

e.g.

#include <ostream>

int main()
{
    // This statement has no effect:
    static_cast<std::ostream&(*)(std::ostream&)>( std::endl );

    std::ostream&(*fp)(std::ostream&) = std::endl;
}

Usually, you just use it in a context where the template argument is deduced automatically.

#include <iostream>
#include <ostream>
int main()
{
    std::cout << std::endl;
    std::endl( std::cout );
}
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
3

http://www.cplusplus.com/reference/iostream/manipulators/endl/

You can't have std::endl by itself because it requires a basic_ostream as a type of parameter. It's the way it is defined.

It's like trying to call my_func() when the function is defined as void my_func(int n)

Marlon
  • 19,924
  • 12
  • 70
  • 101
2

endl is a function that takes a parameter. See std::endl on cplusplus.com

// This works.
std::endl(std::cout);
Martin York
  • 257,169
  • 86
  • 333
  • 562
sinelaw
  • 16,205
  • 3
  • 49
  • 80
1

The std::endl terminates a line and flushes the buffer. So it should be connected the stream like cout or similar.

-1
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class student{

      private: 
           string coursecode;
           int number,total;
      public:
           void getcourse(void);
           void getnumber(void);
           void show(void);
      };

        void  student ::getcourse(){

              cout<<"pleas enter the course code\n";
              cin>>coursecode;

              }


        void  student::getnumber(){

                     cout<<"pleas enter the number \n";
                     cin>>number;

                     }
                void  student::show(){

                             cout<<"coursecode is\t\t"<<coursecode<<"\t\t and number is "<<number<<"\n";

                             } 
                             int main()
                             {

                                   student s;

                                  s.getcourse();
                                   s.getnumber(); 
                                   s.show();
                                   system("pause");









                                   }