“ seekg identifier not found”
-
05-07-2019 - |
質問
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':識別子が見つかりません
このエラーが発生する理由と修正方法を教えてください
解決
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
}
所属していません StackOverflow