22

当我将以下内容作为独立行键入时:

std::endl;

我收到以下错误:

statement cannot resolve address for overloaded function

这是为什么?我不能写成std::endl;一个独立的行吗?

谢谢。

4

8 回答 8

20

std::endl是一个函数模板。通常,它用作插入运算符的参数<<。在这种情况下,所operator<<讨论的流的 将被定义为例如ostream& operator<< ( ostream& (*f)( ostream& ) )。定义了参数的类型f,因此编译器将知道函数的确切重载。

它与此相当:

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...
}

但是您可以通过一些类型提示来解决歧义:

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 

std::endl当作为参数提供给 时,通常会发生这种情况operator <<:函数有一个定义

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

这将使编译器能够std::endl在提供给 eg 时选择正确的重载std::cout << std::endl;

好问题!

于 2011-01-30T12:50:32.513 回答
4

我能想到的最可能的原因是它的声明是:

ostream& endl ( ostream& os );

换句话说,如果不是<<操作的一部分os,就无法推断出任何东西。我很确定这是因为这条线:

std::endl (std::cout);

编译就好了。

我对你的问题是:你为什么要这样做

我知道一个事实,这7;是 C 语言中一个完全有效的语句,但你看不到那种垃圾污染了我的代码 :-)

于 2011-01-30T12:38:18.087 回答
4

std::endl 是一个操纵器。它实际上是一个由流上的 << 运算符的一个版本调用的函数。

std::cout << std::endl
// would call 
std::endl(std::cout).
于 2011-01-30T12:43:24.723 回答
4

std::endl是一个函数模板。如果您在无法唯一确定模板参数的上下文中使用它,则必须消除您所指的专业化的歧义。例如,您可以使用显式强制转换或将其分配给正确类型的变量。

例如

#include <ostream>

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

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

通常,您只需在自动推导出模板参数的上下文中使用它。

#include <iostream>
#include <ostream>
int main()
{
    std::cout << std::endl;
    std::endl( std::cout );
}
于 2011-01-30T13:30:52.887 回答
3

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

您不能单独拥有std::endl,因为它需要 abasic_ostream作为参数类型。这是它的定义方式。

my_func()这就像在函数定义为时尝试调用void my_func(int n)

于 2011-01-30T12:41:34.307 回答
2

endl 是一个带参数的函数。请参阅cplusplus.com 上的 std::endl

// This works.
std::endl(std::cout);
于 2011-01-30T12:40:40.523 回答
1

std::endl终止一行并刷新缓冲区。所以它应该连接类似cout或类似的流。

于 2011-01-30T12:38:48.793 回答
-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");









                                   }    
于 2017-01-24T12:23:07.130 回答