“未找到搜索标识符”

发布于 2024-07-19 13:32:14 字数 509 浏览 2 评论 0原文

我有一个名为 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”:未找到标识符

为什么会出现此错误以及如何修复它?

I have a program called main:

#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 get the error:

'seekg': identifier not found

why do I get this error and how do I fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洋洋洒洒 2024-07-26 13:32:14

eekg 是 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
}

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

You haven't instantiated any.

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 should

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

Take into account what ParoXon commented in your question.

You should create a file other.cpp containing function's load_data implementation.
File other.h should contain function's load_data declaration. In that file (other.h) you should include all files neccesary for functions declared there to work. And dont forget to protect yourself against multiple includes !

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文