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
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!!
If you dont wan’t to include stdlib.h, then you could declare atoi as extern, but be aware when you compile this module.
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.
So, now you could do
If you are in C++, load_data function could return a std::string.
and then use c_str() method, which returns a C-String from a C++ string.
In addition to that, you shouldn’t
(regarding
)
You should
and dont forget to free this memory