开发者

Problems returning vector stack reference

I am working on an application that builds a vector of structs for items in a given directory and returns a reference of the vector for it to be read, I receive the following errors when attempting to compile the example code below:

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;
    c开发者_开发百科har* path;
};

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

#endif

What am I doing wrong here, and is there a better way going about this?


You've made 'indexItem' a vector, you probably just want it to be the type you want to put in 'indexItems'. Also, I'd create the new struct in your loop:

    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);
        }
    }


You are defining a vector called indexItem:

vector<indexStruct> indexItem;

This is just an array. So the following lines must be changed to reference a specific element of the vector:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜