1

これbasic_istream::tellg()は、 VS2010での関数の定義です。この関数は type の変数を返すことに注意してくださいpos_type。ただし、streamoff以下の例で使用されている型を で置き換えるpos_typeと、コンパイラは文句を言います (C2065: 'pos_type' : undeclared identifier)。

pos_typeで定義され<fstream>ていtypedef typename _Traits::pos_type pos_type;ます。

// basic_istream_tellg.cpp
// compile with: /EHsc
#include <iostream>
#include <fstream>

int main()
{
    using namespace std;
    ifstream file;
    char c;
    streamoff i; // compiler complains if I replace streamoff by pos_type

    file.open("basic_istream_tellg.txt");
    i = file.tellg();
    file >> c;
    cout << c << " " << i << endl;

    i = file.tellg();
    file >> c;
    cout << c << " " << i << endl;
}
4

3 に答える 3

4

pos_type資格なしでただ書くことはできません。のメンバーであることに注意してくださいifstream。だからあなたはこれを書く必要があります:

ifstream::pos_type i; //ok

これでうまくいくはずです。

また、using namespace std; は bad と見なされるため、回避する必要があり、代わりに次のように完全な修飾を使用することをお勧めします。

std::ifstream file;        //fully-qualified
std::ifstream::pos_type i; //fully-qualified

C++11 では、代わりに使用できますauto

auto i = file.tellg();

コンパイラに と推測iさせますstd::ifstream::pos_type

それが役立つことを願っています。

于 2013-01-30T19:07:16.520 に答える
0

pos_type名前空間スコープの typedef ではなく、クラスに属するメンバ typedef です。のようなものが必要ですifstream::pos_type

于 2013-01-30T19:06:01.440 に答える
0
std::ifstream file;        
std::ifstream::pos_type i; 

あなたはこれを使うことができます:開発C ++で私にとってはうまくいきます

 i = file.tellg();
于 2016-05-26T15:12:38.097 に答える