スタンドアロン行として次のように入力すると、次のようになります。
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::endl
たとえばに提供されたときにの適切なオーバーロードを選択できるようになりますstd::cout << std::endl;
。
いい質問です!
std::endlはマニピュレータです。これは実際には、ストリーム上の<<演算子のバージョンによって呼び出される関数です。
std::cout << std::endl
// would call
std::endl(std::cout).
私が考えることができる最も可能性の高い理由は、それが宣言であるということです:
ostream& endl ( ostream& os );
言い換えれば、<<
操作の一部でなくos
ても、推測できるものはありません。私はこれが次の行以来のケースであるとかなり確信しています:
std::endl (std::cout);
うまくコンパイルします。
あなたへの私の質問は:なぜあなたはこれをしたいのですか?
私はCで完全に有効なステートメントであるという事実を知って7;
いますが、そのようなゴミが私のコードを汚染しているのを見ることはありません:-)
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 );
}
http://www.cplusplus.com/reference/iostream/manipulators/endl/
パラメータの一種としてをstd::endl
必要とするため、単独で使用することはできません。basic_ostream
それが定義されている方法です。
my_func()
これは、関数が次のように定義されているときに呼び出そうとするようなものです。void my_func(int n)
endlは、パラメーターを受け取る関数です。cplusplus.comのstd::endlを参照してください
// This works.
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");
}