2009-11-01 5 views
1

주어진 디렉토리에있는 항목에 대한 구조체 벡터를 작성하고 읽을 벡터에 대한 참조를 반환하는 응용 프로그램을 작성 중이므로 예제를 컴파일 할 때 다음 오류가 발생합니다. 아래 코드 :벡터 스택 참조를 반환하는 문제

1. 'class std::vector<indexStruct, std::allocator<indexStruct> >' has no member named 'name' 
2. no matching function for call to `std::vector<indexStruct, std::allocator<indexStruct> >::push_back(std::vector<indexStruct, std::allocator<indexStruct> >&)' 

exampleApp.cpp

#include "exampleApp.h" 

exampleApp::exampleApp() 
{ 
    this->makeCatalog(); 
} 

char* findCWD() 
{ 
    char* buffer = new char[_MAX_PATH]; 
    return getcwd(buffer, _MAX_PATH); 
} 

void exampleApp::makeCatalog() 
{ 
    char* cwd = this->findCWD(); 
    vector<indexStruct> indexItems; 

    this->indexDir(cwd, indexItems); 
} 

void exampleApp:indexDir(char* dirPath, vector<indexStruct>& indexRef) 
{ 
    DIR *dirPointer = NULL; 
    struct dirent *dirItem = NULL; 
    vector<indexStruct> indexItems; 
    vector<indexStruct> indexItem; 

    try 
    { 
     if ((dirPointer = opendir(dirPath)) == NULL) throw 1; 
     while (dirItem = readdir(dirPointer)) 
     { 
      if (dirItem == NULL) throw 2; 
      if (dirItem->d_name[0] != '.') 
      { 
       indexItem.name = dirItem->d_name; 
       indexItem.path = dirPath; 
       indexItems.push_back(indexItem); 
       indexItem.clear(); 
      } 
     } 

     indexRef.swap(indexItems); 
     closedir(dirPointer); 
    } 
    catch(int errorNo) 
    { 
     //cout << "Caught Error #" << errorNo; 
    } 
} 

exampleApp.h

#ifndef EXAMPLEAPP_H 
#define EXAMPLEAPP_H 

#include <iostream.h> 
#include <dirent.h> 
#include <stdlib.h> 
#include <vector.h> 
using namespace std; 

struct indexStruct 
{ 
    char* name; 
    char* path; 
}; 

class exampleApp 
{ 
public: 
    exampleApp(); 
private: 
    char* findCWD(); 
    void makeCatalog(); 
    void indexDir(char* dirPath, vector<indexStruct>& indexRef); 
}; 

#endif 
,

나는 여기서 무엇을 잘못하고 있으며 이것에 관해 더 좋은 방법이 있습니까?

+0

어떤 줄이 오류가 있습니까? – Mark

답변

0

을, 당신은 아마 당신이 'indexItems'를 넣어 원하는 형태가되고 싶어요 . 또한 루프에 새 구조체를 만들 수 있습니다.

while (dirItem = readdir(dirPointer)) 
    { 
     if (dirItem == NULL) throw 2; 
     if (dirItem->d_name[0] != '.') 
     { 
      indexStruct indexItem; 

      indexItem.name = dirItem->d_name; 
      indexItem.path = dirPath; 
      indexItems.push_back(indexItem); 
     } 
    } 
+0

제안한 코드가 바뀌 었습니다. 새로운 컴파일 오류입니다. 'struct indexItem'에 'clear'라는 멤버가 없습니다. indexItem.clear(); 그것을 고칠 수 있지만 이제 벡터에 밀어 넣은 후 indexItem 구조체를 파괴해야하는지 궁금해? –

+0

예, 분명히 있으면 안됩니다. 죄송합니다. '새'를 사용하지 않으면 아무 것도 삭제할 필요가 없습니다. indexItem은 사용되는 모든 위치에 복사됩니다. 그것은 벡터에 복사되고 'if'의 범위가 남아있을 때 지역이 파괴됩니다. – joshperry

+0

도움을 주신 것에 대해 감사드립니다. 모든 것이 이제는 잘되고 있습니다! –

0

당신은 vectorindexItem라고 정의됩니다

vector<indexStruct> indexItem; 

이것은 단지 배열입니다. 그래서 다음과 같은 라인이 벡터의 특정 요소 참조하도록 변경해야합니다 : 당신은 'indexItem'벡터했습니다

indexItem.name = dirItem->d_name;// should be indexItem[..].name or indexItem.at(..).name 
indexItem.path = dirPath;  // same as above! 
+0

내가 말했듯이 코드를 변경했는데 오류없이 컴파일하지만 exe는 실행되고 오류나 아무것도없이 죽습니다. –