0

main というプログラムがあります。

#include<iostream>
#include<fstream>
using namespace std;
#include"other.h"
int main()
{
//do stuff
}

そして、other.h:

char* load_data(int begin_point,int num_characters)
{
    seekg(begin_point);
    char* return_val=new char[num_characters+1];
    mapdata.getline(return_val,num_characters);
    return return_val;
}

エラーが表示されます:

「seekg」: 識別子が見つかりません

このエラーが発生する理由と修正方法を教えてください。

4

1 に答える 1

1

seekgは、fstream(istreamで宣言されている)クラスのメソッドです。

インスタンス化されていません。

これを例として取り上げます

  ifstream is;
  is.open ("test.txt", ios::binary );

  // get length of file:
  is.seekg (0, ios::end);

ソース:http ://www.cplusplus.com/reference/iostream/istream/seekg/

だから、あなたはすべきです

char* load_data(int begin_point,int num_characters)
{
    ifstream is;
    is("yourfile.txt") //file is now open for reading. 

    seekg(begin_point);
    char* return_val=new char[num_characters+1];
    mapdata.getline(return_val,num_characters);
    return return_val;
}

あなたの質問でParoXonがコメントしたことを考慮に入れてください。

関数のload_data実装を含むファイルother.cppを作成する必要があります。ファイルother.hには、関数のload_data宣言が含まれている必要があります。そのファイル(other.h)には、そこで宣言された関数が機能するために必要なすべてのファイルを含める必要があります。そして、複数のインクルードから身を守ることを忘れないでください!

ファイルother.h

#ifndef __OTHER_H__
#define  __OTHER_H__

#include <iostream>
#include <fstream>

char* load_data(int,int);//no implementation
#endif

ファイルother.cpp

#include "other.h" //assumes other.h and other.cpp in same directory

char* load_data(int begin,int amount){
      //load_data implementation
}
于 2009-05-05T05:15:55.827 に答える