“Identificador seekg não encontrado”
-
05-07-2019 - |
Pergunta
Eu tenho um programa chamado principal:
#include<iostream>
#include<fstream>
using namespace std;
#include"other.h"
int main()
{
//do stuff
}
e, em seguida, 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;
}
e eu recebo o erro:
'seekg': identificador não encontrado
Por que eu recebo este erro e como faço para corrigir isso?
Solução
seekg é um método do fstream (declarado em istream) classe.
Você não instanciado qualquer.
Leve isso como um exemplo
ifstream is;
is.open ("test.txt", ios::binary );
// get length of file:
is.seekg (0, ios::end);
Fonte: http://www.cplusplus.com/reference/iostream/ istream / seekg /
Assim, você deve
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;
}
Leve em conta que ParoXon comentou na sua pergunta.
Você deve criar um arquivo other.cpp contendo implementação load_data da função. Arquivo other.h deve conter declaração load_data da função. Nesse arquivo (other.h) você deve incluir todos os arquivos neccesary para funções declarados lá para trabalhar. E não se esqueça de se proteger contra múltipla inclui!
Arquivo other.h
#ifndef __OTHER_H__
#define __OTHER_H__
#include <iostream>
#include <fstream>
char* load_data(int,int);//no implementation
#endif
other.cpp Arquivo
#include "other.h" //assumes other.h and other.cpp in same directory
char* load_data(int begin,int amount){
//load_data implementation
}