#include using namespace std; #include"other.h" int main() { //do st...">

"Seekg id not found"

I have a main program:

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

      

and then 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;
}

      

and I am getting the error:

'seekg': id not found

why am i getting this error and how to fix it?

0


source to share


1 answer


seekg is a method from the fstream class (declared in istream).

You haven't created any instances.

Let's take this as an example

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

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

      

source: http://www.cplusplus.com/reference/iostream/istream/seekg/

So you must

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;
}

      



Please note what ParoXon commented in your question.

You should create a file other.cpp containing the load_data function. The other.h file must contain the load_data function declaration. In this file (other.h) you have to include all the files needed for the functions declared there to work. And don't forget to protect yourself from multiple inclusions!

File other.h

#ifndef __OTHER_H__
#define  __OTHER_H__

#include <iostream>
#include <fstream>

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

      

Other.cpp file

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

char* load_data(int begin,int amount){
      //load_data implementation
}

      

+1


source







All Articles