簡體   English   中英

如何使此聲明生效?

[英]How can I make this declaration work?

編輯:我也得到一個答案,使扇區成為向量的向量:

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:“正在初始化”:無法從“字符(*)[1]”轉換為“字符**”

第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

您不能返回指向堆棧變量的指針。 並且數組需要作為指針類型返回。

嘗試:

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;

好,

函數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函數的參數必須是一個以空值結尾的字符串。

為了使代碼正常工作,您應該使函數加載數據返回一個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-String。

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

我不太確定您的運動目標是什么。 但是,如果您想從文件中讀取“填充”並以所需的格式(例如int,string ...)獲取它,則可以使用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”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM