この宣言を機能させるにはどうすればよいですか?
-
05-07-2019 - |
質問
編集:セクターをベクトルのベクトルにするための回答もありました:
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&gt;エラーC2540:配列バインドとしての非定数式
行5&gt;エラーC2440: 'initializing': 'char(*)[1]'から 'char **'に変換できません
14行目&gt;エラー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
解決
まあ、
function 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_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);
他のヒント
スタック変数へのポインタを返すことはできません。また、配列はポインター型として返される必要があります。
試してください:
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;
あなたの運動の目標は何なのかよくわかりません。ただし、ファイルから「もの」を読み取り、期待する形式(int、stringsなど)で取得する場合は、operator&gt;&gt;を使用できます。このような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?
それはあなたが望んでいたことですか?注:&quot; main.cpp&quot;に言及していることに気付いたので、私はc ++を使用しています。