我怎样才能使这个声明发挥作用?
编辑:我还得到了一个答案,使扇区成为向量的向量:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法返回指向堆栈变量的指针。 并且数组需要作为指针类型返回。
尝试:
You can't return a pointer to a stack variable. And arrays need to be returned as pointer types.
Try:
我不太确定你锻炼的目标是什么。 但是,如果您想从文件中读取“内容”并以您期望的格式获取它(例如 int、字符串...),您可以使用运算符>>> 和 getline 这样:
你得到这样的输出:
这就是你想要的吗? 注意:我使用 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:
And you get output like this:
Is that what you were after? NOTE: I'm using c++ because I noticed you mentioned "main.cpp"
嗯,
函数 load_data(int,int) 返回一个 char。
您将该 char 传递给 atoi 函数,该函数采用 char*。 除此之外,您可能不包括 stdlib.h 头文件!
如果您不想包含 stdlib.h,那么您可以将 atoi 声明为 extern,但在编译此模块时请注意。
考虑到 atoi 函数的参数必须是一个以 null 结尾的字符串。
为了使您的代码正常工作,您应该使函数加载数据返回 char*,而不是 char。
所以,现在你可以做
如果你使用 C++,load_data 函数可以返回 std::string。
然后使用 c_str() 方法,该方法从 C++ 字符串返回 C 字符串。
除此之外,您不应该
(关于
)
您应该
并且不要忘记释放此内存
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