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

发布于 2024-10-15 03:24:54 字数 172 浏览 7 评论 0原文

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

std::endl;

我收到以下错误:

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

这是为什么?我不能将 std::endl; 写为独立行吗?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

错爱 2024-10-22 03:24:54

std::endl 是一个函数模板。通常,它用作插入运算符 << 的参数。在这种情况下,所讨论的流的运算符<<将被定义为例如ostream&。运算符<< (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::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!

面如桃花 2024-10-22 03:24:54

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

ostream& endl ( ostream& os );

换句话说,如果不成为<<操作的一部分,就没有os< /code> 可以推断。我很确定情况就是这样,因为该行:

std::endl (std::cout);

编译得很好。

我向您提出的问题是:您为什么想要这样做?

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

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 :-)

緦唸λ蓇 2024-10-22 03:24:54

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

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 );
}
骄兵必败 2024-10-22 03:24:54

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

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

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).
戴着白色围巾的女孩 2024-10-22 03:24:54

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

您不能单独使用 std::endl,因为它需要 basic_ostream 作为参数类型。这就是它的定义方式。

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

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)

小伙你站住 2024-10-22 03:24:54

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

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

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

// This works.
std::endl(std::cout);
ヅ她的身影、若隐若现 2024-10-22 03:24:54

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

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

冰火雁神 2024-10-22 03:24:54
#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");









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









                                   }    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文