0

編集:セクターをベクトルのベクトルにするための答えも得ました:

vector<vector<char>>sector;

そしてそれは私の残りのエラーを取り除きます。

編集:誰かが提案したように、セクターをポインターの配列にしましたが、それでも3つのエラーが発生します:

編集:プログラムを編集しましたが、すべてのエラーが修正されていません:

私はプログラムのこのセクションを持っています:

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':識別子が見つかりません

seekgごと:はい、fstreamを含める必要があることはわかっています。これをmain.cppに含めました。これは、main.cppにも含まれている別の.hファイルです。

エラーを修正するにはどうすればよいですか?具体的には、すべての変数をグローバルに保ちながらエラーを修正するにはどうすればよいですか?

また、役立つ場合は、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を返します。そのcharをatoi関数に渡します。これはchar*を取ります。それに加えて、おそらくstdlib.hヘッダーファイルをインクルードしていません!!

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

stdlib.hを含めたくない場合は、atoiをexternとして宣言できますが、このモジュールをコンパイルするときは注意してください。

extern int atoi(const char*)

atoi関数の引数はnullで終了する文字列でなければならないことを考慮に入れてください。

コードが機能するためには、関数ロードデータが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 ++文字列からC文字列を返すc_str()メソッドを使用します。

   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

あなたの運動の目標が何なのかよくわかりません。しかし、ファイルから 'stuff' を読み取り、期待する形式 (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?

それはあなたが求めていたものですか?注:あなたが「main.cpp」に言及していることに気づいたので、私はc ++を使用しています

于 2009-05-05T07:56:09.093 に答える