0

我有一个名为 main 的程序:

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

然后是其他.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': 未找到标识符

为什么我会收到此错误,我该如何解决?

4

1 回答 1

1

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)中,您应该包含所有文件,以便在该文件中声明的函数正常工作。并且不要忘记保护自己免受多重包含!

文件其他.h

#ifndef __OTHER_H__
#define  __OTHER_H__

#include <iostream>
#include <fstream>

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

文件其他.cpp

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

char* load_data(int begin,int amount){
      //load_data implementation
}
于 2009-05-05T05:15:55.827 回答