-2
Decision.h외부 구조에 액세스하는 방법?
typedef struct DST
{
float salary;
float x1;
float x2;
};
struct DST person;
decision()
{
std::vector<std::vector<DST>> person(300);
for(int i = 0; i < 300; i++)
person[i].resize(300);
//And made some computation to save the data in 2d structure person
}
check.h
//In this header I want to access person structure
extern DST person;
check()
{
for(int i=0; i<300; i++)
{
for(int j=0; j<300;j++)
{
conf[0]+= person[j][i].salary;
}
}
}
하지만 오류 다음 얻고는 :
error C2676: binary '[' : 'DST' does not define this operator or a conversion to a type acceptable to the predefined operator
error C2228: left of '.salary' must have class/struct/union
이 좀 도와주십시오.
합니다
당신이 뭔가를 할 것이라고이 기능을 사용하려면 당신의'struct DST' (그것은'class'가되어야합니다)의 메소드 (또는 멤버 함수)를'check'하십시오. 훨씬 더 C++ 프로그래밍 언어 책을 읽으십시오. –
구조체가 클래스가 될 필요가 없습니다. 유일한 차이점은 기본 멤버 액세스입니다 – DTSCode
컴파일러'person'은'DST' 타입이므로 컴파일러는'operator []'를 사용하여 인덱스를 생성 할 수 있기를 기대합니다. 함수 내에서'vector'를 선언한다고해서 두 변수가 같은 이름을 가지기 때문에 전역 변수의 타입을 마술처럼 변형하지는 않습니다. 그리고 헤더 파일에 변수를 정의해서는 안됩니다. 그러나 무엇보다도, 당신이해야 할 일은 좋은 책을 읽는 것입니다. – Praetorian