이 선언을 어떻게 작동시킬 수 있습니까?
-
05-07-2019 - |
문제
편집 : 또한 섹터를 벡터의 벡터로 만들기위한 답변을 얻었습니다.
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': 식별자를 찾을 수 없습니다.
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
해결책
잘,
함수 load_data (int, int) char를 반환합니다. 당신은 그 숯을 Atoi 기능으로 전달하고 있습니다. 그 외에도, 당신은 아마도 stdlib.h 헤더 파일을 포함하지 않을 것입니다 !!
#include <cstdlib>
int atoi(const char*);
stdlib.h를 포함시키지 않으면 atoi를 외부로 선언 할 수 있지만이 모듈을 컴파일 할 때는 알아야합니다.
extern int atoi(const char*)
ATOI 함수의 인수는 무효가 종결 된 문자열이어야한다는 점을 고려하십시오.
코드가 작동하려면 기능 부하 데이터가 숯이 아닌 숯*을 반환해야합니다.
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 :: 문자열을 반환 할 수 있습니다.
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);
다른 팁
스택 변수에 대한 포인터를 반환 할 수 없습니다. 배열은 포인터 유형으로 반환해야합니다.
노력하다:
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와 같은 형식으로 가져 오려면 연산자 >>를 사용할 수 있고 다음과 같이 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 ++를 사용하고 있습니다.