2016-06-11 7 views
0

내부 'CompaniesMap.h': 'CompaniesMap.cpp'에서더 적합한 사용자 정의 변환

class CompaniesMap 
{ 
public: 
... 
    // The companies map 
    map<string, CompanyP> companies; 
}; 

typedef map<string, CompanyP>::iterator map_it; 

는 :

string* CompaniesMap::displayCompaniesList() const 
{ 
    string* compList = new string[companies.size() + 1];  // We add 1 so we can start with the [1] index for simplicity 

    // Check if 'companies' is empty 
    if (companies.empty()) 
    { 
     cout << "No companies in the database." << endl; 
     return nullptr; 
    } 

    for (map_it it = companies.begin(), int i = 1; it != companies.end(); ++it, i++) 
    { 
     cout << " " << i << ") " << it->first << endl; 
     compList[i] = it->first; 
    } 
} 

Visual Studio에서 다음과 같은 오류 메시지가 companies.begin() 아래에 빨간색 선을 보여줍니다 : enter image description here

코드를 map_it it =에서 map<string, CompanyP>::iterator으로 변경하려고 시도했지만이 오류가 여전히 발생합니다.

나는 동일한 코드를 main.cpp에 가지고 있었지만 별도의 클래스로 옮기기로 결정 했으므로 동일한 헤더를 포함하지만이 오류는 계속 표시됩니다. 내가 구축 할 때 나는이 파일에서 다른 오류를 얻을 :

1>d:\asaf\c\vs\hw5\hw5\hw5\companiesmap.cpp(66): error C2062: type 'int' unexpected 
1>d:\asaf\c\vs\hw5\hw5\hw5\companiesmap.cpp(66): error C2065: 'i' : undeclared identifier 
1>d:\asaf\c\vs\hw5\hw5\hw5\companiesmap.cpp(66): error C2143: syntax error : missing ';' before ')' 
1>d:\asaf\c\vs\hw5\hw5\hw5\companiesmap.cpp(67): error C2143: syntax error : missing ';' before '{' 
1>d:\asaf\c\vs\hw5\hw5\hw5\companiesmap.cpp(68): error C2065: 'i' : undeclared identifier 
1>d:\asaf\c\vs\hw5\hw5\hw5\companiesmap.cpp(69): error C2065: 'i' : undeclared identifier 
+3

을이 "우리는 1 그래서 우리는 단순함에 대한 [1] 인덱스로 시작할 수 있습니다 추가 "C/C++를 프로그래밍하는 데 사용 된 모든 사람을 속일 수 있습니다. (단순함이 의심 스럽다.) –

+1

확실하지는 않지만 ...'CompaniesMap :: displayCompaniesList()'는'const'이다. 이는 모든 클래스 멤버가 함수에서'const'를 효과적으로 의미한다는 것을 의미한다. 즉,'companies'는' 'iterator' 대신에'const_iterator'를 사용합니다. – Dialecticus

+0

@ DieterLücking 메뉴에는 "1)", "2)"등의 접두사가있는 회사 목록이 표시됩니다. 회사가 메뉴 번호와 관련되어 있다는 것이 더 간단하지 않습니까? – PanthersFan92

답변

2

displayCompaniesListconst 기능, 당신은 클래스에 정의 된 변수에 대한 변경을 할 수 없습니다 것을 의미한다.

companies는 따라서 const std::map<std::string, CompanyP>, 아닌 std::map<std::string, CompanyP> 될 것이며, 그래서 당신은 그에 따라 반복자를 변경해야합니다 :

std::map<std::string, CompanyP>::const_iterator it = companies.begin(); 

//Or even better if you use C++11 
auto it = companies.begin(); 
+0

코드를'typedef map :: iterator map_it;'으로 변경 했는데도 여전히 동일한 문제가 발생합니다. – PanthersFan92

+0

그리고'auto'를 사용할 때'자동 유형을 추론 할 수 없습니다 '오류가 발생합니다 – PanthersFan92

+0

는 다음을 추가했습니다 'typedef map :: iterator map_const_it;'그리고 그것을''map_const_it it = companies.begin()'으로 변경했습니다. 오류가 계속 발생합니다. – PanthersFan92

관련 문제