views:

127

answers:

2

Hello,

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;
    char* 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?

A: 

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!
AraK
I've changed the code as you have said and it compiles with no errors but the exe runs and just dies off with no errors or anything.
David Davidson
A: 

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);
        }
    }
joshperry
I have changed the code to what you have suggested and this is the new compile error:'struct indexItem' has no member named 'clear'removing indexItem.clear(); fixes it but now I am wondering if I need to destroy the indexItem struct after it has been pushed onto the vector?
David Davidson
Yes, the clear should not be there, sorry. If you don't use 'new' you don't need to worry about deleting anything. The indexItem will just be copied anywhere that it is used. It is copied into the vector and the local is destroyed when the scope of the enclosing 'if' is left.
joshperry
Alright thanks for the help, everything is working fine now!
David Davidson