2016-11-08 1 views
-2

벡터에 대해 push_back 함수를 사용하여이 코드 세그먼트를 컴파일하면 오류가 발생합니다.컴파일시 벡터 푸시 백 오류

for (int i=0; i<=n; i++) 
{ 
    if(i==0) 
    { 
     Profit[i].push_back(0); 
     Weight[i].push_back(0); 
     Name[i].push_back(""); 
    } 
    else 
    { 
     Profit[i].push_back(tosteal[i-1].getProfit()); 
     Weight[i].push_back(tosteal[i-1].getWeight()); 
     Name[i].push_back(tosteal[i-1].getName()); 
    } 
} 

Weight 및 Profit은 int 데이터 형식의 벡터로 선언되며 Name은 문자열 데이터 형식의 벡터입니다. tosteal은 항목 객체의 배열입니다. getProfit() 및 getWeight()는 int를 반환하고 getName()은 문자열을 반환합니다.

joulethiefdynamicrefined.cpp : 167 : 오류 : 부재 Profit.std '에서'와 push_back ': 벡터 < _Tp, _Alloc> :: 운영자 요청

컴파일러 준다 오류이며, 일부는 반복되어 [int] 클래스가 아닌 'int'형식의 _ (int), _Alloc = std :: allocator ((long unsigned int) i)) ') [오류 : 168 weight.std :: vector의 'push_back'을 사용합니다. < _Tp, _Alloc> :: operator [] (_Tp = int, _Alloc = std :: allocator ((long unsigned int) i)) ' 클래스 유형 'int' joulethiefdynamicrefined.cpp : 169 : 오류 : 'const char *'에서 'char'로의 변환이 잘못되었습니다char_report = std :: char_traits, _Alloc = std :: _CharT, _Traits, _Alloc> :: push_back (_CharT) [_CharT = char, _Traits = std :: char_traits와 함께 _CharT, _Traits, _Alloc :: std :: 기본 _string < 첫 번째 인수를 초기화하는 중 오류가 발생했습니다. 할당 자] ' joulethiefdynamicrefined.cpp : 173 : 오류 :'Profit.std :: vector '의'push_back '멤버에 대한 요청 < _Tp, _Alloc> :: operator [] (_Tp = int, _Alloc = std :: allocator) 174 : 오류 : 'Weight.std :: vector'의 'push_back'멤버에 대한 요청 < _Tp, _Alloc> : 오류가 발생했습니다. : 연산자 [] [_Tp = int, _Alloc = std :: allocator ((long unsigned int) i)) ','클래스 유형이 'int'가 아닌 joulethiefdynamicrefined.cpp : 175 : 오류 : 일치하지 않음 'std :: basic_string, std :: allocator> :: push_back (std :: st) 호출에 대한 함수 링) ' /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/basic_string.h:914 : note : 후보들은 : 보이드 표준 : < _CharT, _Traits, _Alloc>를 basic_string [_CharT = CHAR, _Traits = 표준 : char_traits, _Alloc = 성병 :: 할당] ::와 push_back (_CharT)

+1

당신이 물어 보지는 않지만, for (int i = 0; i <= n; i ++)의'i'의 범위는 옳지 않다고 생각합니다. –

+0

_Weight 및 Profit은 int 데이터 유형의 벡터로 선언됩니다. _... ok 'Weight [i] .push_back'을 수행해야하는 이유는 무엇입니까? – smac89

답변

5
Profit[i].push_back(0); 

해야하는 be

Profit.push_back(0); 

등등. Profit은 벡터 자체입니다. Profit[i].push_back(0)이라고 말하면 벡터에 무엇인가를 밀어 넣기보다는 이미 벡터에있는 요소 중 하나에 무언가를 넣으려고합니다. request for member ‘push_back’ in [...] which is of non-class type ‘int’ :

요소의 형태가 int 때문에

, Profit[i] 당신이 오류가 왜 형 int,이다.