[英]C++ - statement cannot resolve address for overloaded function
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;
我不能写
std::endl;
as a stand-alone line? 作为一个独立的线?
Thanks. 谢谢。
std::endl
is a function template. std::endl
是一个函数模板。 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 eg ostream& operator<< ( ostream& (*f)( ostream& ) )
. 在这种情况下,所讨论的流的
operator<<
将被定义为例如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. 定义了
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 eg std::cout << std::endl;
这将使编译器在提供给
std::cout << std::endl;
时提供正确的std::endl
重载std::cout << std::endl;
. 。
Nice question! 好问题!
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. 换句话说,没有成为
<<
操作的一部分,就没有可以推断出的os
。 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;
我知道
7;
is a perfectly valid statement in C but you don't see that kind of rubbish polluting my code :-) 在C中是完全有效的声明,但你没有看到那种垃圾污染我的代码:-)
std::endl
is a function template. std::endl
是一个函数模板。 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.
例如,您可以使用显式强制转换或将其分配给正确类型的变量。
eg 例如
#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 );
}
endl is a function that takes a parameter. endl是一个带参数的函数。 See std::endl on cplusplus.com
请参阅cplusplus.com上的std :: endl
// This works.
std::endl(std::cout);
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. 你
std::endl
拥有std::endl
,因为它需要basic_ostream
作为一种参数。 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)
这就像在函数定义为
void my_func(int n)
时尝试调用my_func()
void my_func(int n)
std::endl is a manipulator. std :: endl是一个操纵者。 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).
The std::endl
terminates a line and flushes the buffer. std::endl
终止一行并刷新缓冲区。 So it should be connected the stream like cout
or similar. 所以它应该像
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.