C++ - 语句无法解析重载函数的地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4842901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ - statement cannot resolve address for overloaded function
提问by Simplicity
When I types the following as a stand-alone line:
当我将以下内容作为独立行键入时:
std::endl;
std::endl;
I got the following error:
我收到以下错误:
statement cannot resolve address for overloaded function
statement cannot resolve address for overloaded function
Why is that? Cannot I write std::endl;
as a stand-alone line?
这是为什么?我不能写std::endl;
一个独立的行吗?
Thanks.
谢谢。
采纳答案by xtofl
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.
std::endl
是一个函数模板。通常,它用作插入运算符的参数<<
。在这种情况下,所operator<<
讨论的流的 将被定义为 eg ostream& operator<< ( ostream& (*f)( ostream& ) )
。的参数类型f
已定义,因此编译器将知道该函数的确切重载。
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
std::endl
当作为参数提供给时,通常会发生这种情况operator <<
:有函数的定义
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;
.
这将使编译器能够std::endl
在提供给 eg 时选择正确的重载std::cout << std::endl;
。
Nice question!
好问题!
回答by CB Bailey
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.
std::endl
是一个函数模板。如果您在无法唯一确定模板参数的上下文中使用它,则必须明确您的意思是哪种专业化。例如,您可以使用显式强制转换或将其分配给正确类型的变量。
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 );
}
回答by paxdiablo
The most likelyreason 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:
换句话说,如果不参与<<
操作,os
就无法推断出任何内容。我很确定情况确实如此,因为该行:
std::endl (std::cout);
compiles just fine.
编译就好了。
My question to you is: why would you wantto 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 :-)
我知道一个事实7;
是 C 中的一个完全有效的语句,但你没有看到那种垃圾污染了我的代码 :-)
回答by sinelaw
endl is a function that takes a parameter. See std::endl on cplusplus.com
endl 是一个带参数的函数。请参阅cplusplus.com 上的 std::endl
// This works.
std::endl(std::cout);
回答by Marlon
http://www.cplusplus.com/reference/iostream/manipulators/endl/
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.
您不能单独拥有std::endl
,因为它需要 abasic_ostream
作为参数类型。这是它的定义方式。
It's like trying to call my_func()
when the function is defined as void my_func(int n)
这就像my_func()
在函数定义为时尝试调用void my_func(int n)
回答by hbn
std::endl is a manipulator. It's actually a function that is called by the a version of the << operator on a stream.
std::endl 是一个操纵器。它实际上是一个由流上的 << 运算符的 a 版本调用的函数。
std::cout << std::endl
// would call
std::endl(std::cout).
回答by Alexander Chuikov
The std::endl
terminates a line and flushes the buffer. So it should be connected the stream like cout
or similar.
所述std::endl
终止线和刷新缓冲区。所以它应该像cout
或类似地连接流。
回答by Muhammad Tanzeel
#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");
}