我怎样才能使这个声明发挥作用?

发布于 2024-07-19 13:13:24 字数 1061 浏览 3 评论 0原文

编辑:我还得到了一个答案,使扇区成为向量的向量:

vector<vector<char>>sector;

这消除了我的其余错误。

编辑:我已经按照某人的建议将扇区设置为指针数组,但仍然出现三个错误:

编辑:我已经编辑了程序,但它尚未修复所有错误:

我有程序的这一部分:

char* load_data(int begin_point,int num_characters);
ifstream mapdata("map_data.txt");
const int maxx=atoi(load_data(0,2));
const int maxy=atoi(load_data(2,2));
char** sector=new char[maxx][maxy];

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

并且我得到这些错误:

第 5 行>错误 C2540:非常量表达式作为数组绑定

第 5 行>错误 C2440:“初始化”:无法从“char (*)[1]”转换为“char **”

第 14 行>错误 C3861: 'seekg':未找到

每个eekg的标识符:是的,我知道我必须包含fstream,我将其包含在main.cpp中,这是一个单独的.h文件,也包含在main.cpp中。

我该如何修复这些错误? 具体来说,如何修复错误,同时保持所有变量全局?

另外,如果有帮助的话,这是map_data.txt:

10
10
00O
99!

1
55X
19
What is a question?
18
This is an answer
1
1
2
1

EDIT: I also got an answer to make sector a vector of vectors:

vector<vector<char>>sector;

and that gets rid of the rest of my errors.

EDIT: I've made sector an array of pointers as someone suggested, and still get three errors:

EDIT: I have edited the program, but it has not fixed all of the errors:

I have this section of a program:

char* load_data(int begin_point,int num_characters);
ifstream mapdata("map_data.txt");
const int maxx=atoi(load_data(0,2));
const int maxy=atoi(load_data(2,2));
char** sector=new char[maxx][maxy];

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 these errors:

line 5>error C2540: non-constant expression as array bound

line 5>error C2440: 'initializing' : cannot convert from 'char (*)[1]' to 'char **'

line 14>error C3861: 'seekg': identifier not found

per seekg: yes I know I have to include fstream, I included that in main.cpp, this is a separate .h file also included in main.cpp.

How do I fix the errors? Specifically, how to I fix the errors while keeping all my variables global?

Also, if it helps, this is map_data.txt:

10
10
00O
99!

1
55X
19
What is a question?
18
This is an answer
1
1
2
1

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

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

发布评论

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

评论(3

迷你仙 2024-07-26 13:13:25

您无法返回指向堆栈变量的指针。 并且数组需要作为指针类型返回。

尝试:

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

char* foo = load_data(...);
...
delete [] foo;

You can't return a pointer to a stack variable. And arrays need to be returned as pointer types.

Try:

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

char* foo = load_data(...);
...
delete [] foo;
蓝眼睛不忧郁 2024-07-26 13:13:25

我不太确定你锻炼的目标是什么。 但是,如果您想从文件中读取“内容”并以您期望的格式获取它(例如 int、字符串...),您可以使用运算符>>> 和 getline 这样:

#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream ifs("data.txt");
    if (!ifs.is_open()) return 0;

    int maxx;
    int maxy;

    ifs >> maxx >> maxy;
    cout << maxx << " " << maxy << endl;

    // ----

    char OO_0[4];       // can use char[] or string, see next
    ifs >> OO_0;
    OO_0[sizeof(OO_0)] = 0;

    cout << OO_0 << endl;

    // ----
    string _99;
    ifs >> _99;

    cout << _99 << endl;

    int one;
    string _55_X;
    int _19;
    string what_is;

    ifs >> one >> _55_X >> _19 >> ws;
    // ws gets rid of white space at the end of the line ...
    // this is because getline would only read that ws up to eol

    getline(ifs,what_is);

    cout << one << " " << _55_X << " " << _19 << " " << what_is << endl;

    ifs.close();
}

你得到这样的输出:

10 12
00O
99!
1 55X 19 What is a question?

这就是你想要的吗? 注意:我使用 c++ 因为我注意到你提到了“main.cpp”

I'm not quite sure what is the goal of your exercise. But if you want to read 'stuff' from file and get it in format that you expect (like int, strings ...) you can just use operator>> and getline like this:

#include <fstream>
#include <string>

using namespace std;

int main()
{
    ifstream ifs("data.txt");
    if (!ifs.is_open()) return 0;

    int maxx;
    int maxy;

    ifs >> maxx >> maxy;
    cout << maxx << " " << maxy << endl;

    // ----

    char OO_0[4];       // can use char[] or string, see next
    ifs >> OO_0;
    OO_0[sizeof(OO_0)] = 0;

    cout << OO_0 << endl;

    // ----
    string _99;
    ifs >> _99;

    cout << _99 << endl;

    int one;
    string _55_X;
    int _19;
    string what_is;

    ifs >> one >> _55_X >> _19 >> ws;
    // ws gets rid of white space at the end of the line ...
    // this is because getline would only read that ws up to eol

    getline(ifs,what_is);

    cout << one << " " << _55_X << " " << _19 << " " << what_is << endl;

    ifs.close();
}

And you get output like this:

10 12
00O
99!
1 55X 19 What is a question?

Is that what you were after? NOTE: I'm using c++ because I noticed you mentioned "main.cpp"

爱的那么颓废 2024-07-26 13:13:24

嗯,

函数 load_data(int,int) 返回一个 char。
您将该 char 传递给 atoi 函数,该函数采用 char*。 除此之外,您可能不包括 stdlib.h 头文件!

#include <cstdlib>
int atoi(const char*);

如果您不想包含 stdlib.h,那么您可以将 atoi 声明为 extern,但在编译此模块时请注意。

extern int atoi(const char*)

考虑到 atoi 函数的参数必须是一个以 null 结尾的字符串。

为了使您的代码正常工作,您应该使函数加载数据返回 char*,而不是 char。

char* load_data(int,int);

所以,现在你可以做

//notice these aren't const, they rely on non-compile time available data.
int maxx = atoi (load_data(....));
int maxy = atoi (load_data(....));

如果你使用 C++,load_data 函数可以返回 std::string。

std::string load_data(int,int)

然后使用 c_str() 方法,该方法从 C++ 字符串返回 C 字符串。

   const char* std::string:c_str()


    int maxx = atoi(load_data(....).c_str());
    int maxy = atoi(load_data(....).c_str());

除此之外,您不应该

(关于

line 5>error C2540: non-constant expression as array bound

line 5>error C2440: 'initializing' : cannot convert from 'char (*)[1]' to 'char **'

char sector[maxx][maxy]; 

您应该

 char** sector = new char[maxx][maxy]();

并且不要忘记释放此内存

delete[](sector);

Well,

function load_data(int,int) returns a char.
You are passing that char to the atoi function, that takes a char*. In addition to that, you are probably not including stdlib.h header file!!

#include <cstdlib>
int atoi(const char*);

If you dont wan't to include stdlib.h, then you could declare atoi as extern, but be aware when you compile this module.

extern int atoi(const char*)

Take into account that the argument of atoi function must be a null-terminated string.

In order for your code to work, you should make function load data return a char*, not a char.

char* load_data(int,int);

So, now you could do

//notice these aren't const, they rely on non-compile time available data.
int maxx = atoi (load_data(....));
int maxy = atoi (load_data(....));

If you are in C++, load_data function could return a std::string.

std::string load_data(int,int)

and then use c_str() method, which returns a C-String from a C++ string.

   const char* std::string:c_str()


    int maxx = atoi(load_data(....).c_str());
    int maxy = atoi(load_data(....).c_str());

In addition to that, you shouldn't

(regarding

line 5>error C2540: non-constant expression as array bound

line 5>error C2440: 'initializing' : cannot convert from 'char (*)[1]' to 'char **'

)

char sector[maxx][maxy]; 

You should

 char** sector = new char[maxx][maxy]();

and dont forget to free this memory

delete[](sector);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文