【问题标题】:Problems returning vector stack reference返回向量堆栈引用的问题
【发布时间】:2009-11-01 17:54:43
【问题描述】:

我正在开发一个应用程序,它为给定目录中的项目构建结构向量并返回要读取的向量的引用,在尝试编译下面的示例代码时收到以下错误:

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

我在这里做错了什么,有没有更好的方法来解决这个问题?

【问题讨论】:

  • 错误在哪几行?

标签: c++ vector struct reference


【解决方案1】:

您已将“indexItem”设为向量,您可能只是希望它成为您想要放入“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);
        }
    }

【讨论】:

  • 我已将代码更改为您建议的代码,这是新的编译错误:“struct indexItem”没有名为“clear”的成员删除 indexItem.clear();修复了它,但现在我想知道在将 indexItem 结构推到向量上后是否需要销毁它?
  • 是的,不应该在那里,对不起。如果您不使用“新”,则无需担心删除任何内容。 indexItem 将被复制到任何使用它的地方。它被复制到向量中,当封闭的“if”范围离开时,本地被销毁。
【解决方案2】:

您正在定义一个名为 indexItem 的 vector:

vector<indexStruct> indexItem;

这只是一个数组。因此,必须更改以下行以引用向量的特定元素:

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

【讨论】:

  • 我已经按照你说的修改了代码,它编译时没有错误,但是 exe 运行并且没有错误或任何东西就消失了。
猜你喜欢
  • 2020-01-03
  • 2013-09-27
  • 1970-01-01
  • 1970-01-01
  • 2022-07-22
  • 1970-01-01
  • 1970-01-01
  • 2021-04-26
  • 2011-03-16
相关资源
最近更新 更多