"Identificativo seekg non trovato"
-
05-07-2019 - |
Domanda
Ho un programma chiamato main:
#include<iostream>
#include<fstream>
using namespace std;
#include"other.h"
int main()
{
//do stuff
}
e poi altro.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 ottengo l'errore:
'seekg': identificatore non trovato
perché visualizzo questo errore e come lo risolvo?
Soluzione
seekg è un metodo della classe fstream (dichiarata in istream).
Non hai creato alcuna istanza.
Prendi questo come esempio
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 /
Quindi, dovresti
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;
}
Prendi in considerazione ciò che ParoXon ha commentato nella tua domanda.
È necessario creare un file other.cpp contenente l'implementazione load_data della funzione. Il file other.h deve contenere la dichiarazione load_data della funzione. In quel file (altro.h) dovresti includere tutti i file necessari per le funzioni dichiarate lì per funzionare. E non dimenticare di proteggerti da molteplici inclusioni!
File other.h
#ifndef __OTHER_H__
#define __OTHER_H__
#include <iostream>
#include <fstream>
char* load_data(int,int);//no implementation
#endif
File other.cpp
#include "other.h" //assumes other.h and other.cpp in same directory
char* load_data(int begin,int amount){
//load_data implementation
}