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':找不到标识符

per seekg:是的,我知道我必须包含 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
4

3 回答 3

0

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

尝试:

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;
于 2009-05-05T04:32:46.290 回答
0

好,

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

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

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

extern int atoi(const char*)

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

为了让您的代码正常工作,您应该让函数加载数据返回一个 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);
于 2009-05-05T04:34:10.233 回答
0

我不太确定你锻炼的目标是什么。但是,如果您想从文件中读取“东西”并以您期望的格式获取它(例如 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();
}

你得到这样的输出:

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

那是你所追求的吗?注意:我使用的是 c++,因为我注意到您提到了“main.cpp”

于 2009-05-05T07:56:09.093 回答