繁体   English   中英

C ++ - 语句无法解析重载函数的地址

[英]C++ - statement cannot resolve address for overloaded function

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

std::endl;

我收到以下错误:

statement cannot resolve address for overloaded function

这是为什么? 我不能写std::endl; 作为一个独立的线?

谢谢。

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::cout << std::endl;时提供正确的std::endl重载std::cout << std::endl;

好问题!

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

ostream& endl ( ostream& os );

换句话说,没有成为<<操作的一部分,就没有可以推断出的os 我很确定这是因为这条线:

std::endl (std::cout);

编译得很好。

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

我知道7; 在C中是完全有效的声明,但你没有看到那种垃圾污染我的代码:-)

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 );
}

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

// This works.
std::endl(std::cout);

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

std::endl拥有std::endl ,因为它需要basic_ostream作为一种参数。 这是它的定义方式。

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

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

std::cout << std::endl
// would call 
std::endl(std::cout).

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

#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");









                                   }    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM