我怎样才能使这个声明有效?

时间:2023-01-09 19:44:57

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

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

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

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

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

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

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.

per seekg:是的我知道我必须包含fstream,我在main.cpp中包含了这个,这是一个单独的.h文件,也包含在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:

此外,如果有帮助,这是map_data.txt:

10
10
00O
99!

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

3 个解决方案

#1


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!!

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

#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.

如果你不想包含stdlib.h,那么你可以将atoi声明为extern,但是在编译这个模块时要注意。

extern int atoi(const char*)

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

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

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

为了使代码工作,您应该使函数加载数据返回char *,而不是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.

如果您使用的是C ++,则load_data函数可以返回std :: string。

std::string load_data(int,int)

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

然后使用c_str()方法,该方法从C ++字符串返回一个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);

#2


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;

#3


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:

我不太确定你锻炼的目标是什么。但是如果你想从文件中读取'stuff'并以你期望的格式获得它(比如int,strings ...),你可以使用operator >>和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();
}

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"

这就是你追求的吗?注意:我正在使用c ++,因为我注意到你提到“main.cpp”

#1


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!!

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

#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.

如果你不想包含stdlib.h,那么你可以将atoi声明为extern,但是在编译这个模块时要注意。

extern int atoi(const char*)

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

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

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

为了使代码工作,您应该使函数加载数据返回char *,而不是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.

如果您使用的是C ++,则load_data函数可以返回std :: string。

std::string load_data(int,int)

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

然后使用c_str()方法,该方法从C ++字符串返回一个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);

#2


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;

#3


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:

我不太确定你锻炼的目标是什么。但是如果你想从文件中读取'stuff'并以你期望的格式获得它(比如int,strings ...),你可以使用operator >>和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();
}

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"

这就是你追求的吗?注意:我正在使用c ++,因为我注意到你提到“main.cpp”