我有一个名为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
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top