簡體   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