将向量传递给函数C++

5

我有一个main.cpp、test.h和test.cpp文件,我试图将我的向量传递到test.cpp中,但总是出现错误。

   //file: main.cpp
    int main(){
        vector <Item *> s;
         //loading my file and assign s[i]->name and s[i]-address
         tester(s);
    }

    //file: test.h
    #ifndef TEST_H
    #define TEST_H
    struct Item{
        string name;
        string address;
    };
    #endif

    //file: test.cpp
    int tester(Item *s[]){
        for (i=0; i<s.sizeof();i++){
            cout<< s[i]->name<<"  "<< s[i]->address<<endl;
        }
        return 0;
    }



    ---------------errors--------
    In file included from main.cpp:13:
    test.h:5: error: âstringâ does not name a type
    test.h:6: error: âstringâ does not name a type
    main.cpp: In function âint main()â:
    main.cpp:28: error: cannot convert âstd::vector<Item*, std::allocator<Item*> >â to âItem**â for argument â1â to âint tester(Item**)â

std::vector类型不是数组,它只是像数组一样运作。您需要将Item *s[]更改为std::vector<Item *>。 - Peter Smith
6个回答

13

std::vector<T>T* [] 不是兼容的类型。

请按以下方式更改您的 tester() 函数签名:

//file: test.cpp
int tester(const std::vector<Item>& s)   // take a const-reference to the std::vector
                                         // since you don't need to change the values 
                                         // in this function
{
    for (size_t i = 0; i < s.size(); ++i){
        cout<< s[i]->name<<"  "<< s[i]->address<<endl;
    }
    return 0;
}

有几种方法可以传递 std::vector<T>,每种方法都略微不同:

// This would create a COPY of the vector
// that would be local to this function's scope
void tester(std::vector<Item*>); 

// This would use a reference to the vector
// this reference could be modified in the
// tester function
// This does NOT involve a second copy of the vector
void tester(std::vector<Item*>&);

// This would use a const-reference to the vector
// this reference could NOT be modified in the
// tester function
// This does NOT involve a second copy of the vector
void tester(const std::vector<Item*>&);

// This would use a pointer to the vector
// This does NOT involve a second copy of the vector
// caveat:  use of raw pointers can be dangerous and 
// should be avoided for non-trivial cases if possible
void tester(std::vector<Item*>*);

我有不同的函数。其中一个我要改变“const vector”。我只是想传递它,这样我就可以停止得到那么多错误了。 - TT12

2
将其作为std::vector<Item *> &(向量的引用)传递,并使用迭代器遍历。

2
  1. 你需要包含#include <string>
  2. string name应该改为std::string name,同样适用于std::vector
  3. 你正在使用vector调用tester(),但它期望一个数组(两者不能互换)。
  4. s.sizeof()对于数组和向量都是不正确的;对于后者,请使用s.size()或更好地使用迭代器。

这些只是立即显现的错误,可能还有其他错误。


1

向量并不是数组。

int tester(vector<Item *> &s)

(通过引用传递来避免复制或者当你需要进行修改时)

你还需要修改 tester 函数内的代码,使其正确地作为一个向量工作。


我需要在我的test.h和test.cpp文件中进行更改吗? - TT12

0

你应该修复

test.h:5: error: âstringâ does not name a type

首先,可能是通过 using namespace std;#include <string> 来实现的。


0

你缺少了包含文件

#include <string>
#include <vector>

你需要使用 std::stringstd::vector<>。一个 std::vector 不是一个数组,所以你应该将向量作为引用传递。

int tester(std::vector<Item*> & vec) { //... }

如果你不打算修改传递的向量,甚至可以使用const std::vector<Item*> &

另外,你确定需要一个指针向量吗?你想要实现什么目标?


我有一个代码将在我的程序中的另一个未发布的函数中编辑s[i]-> name和s[i]->address。使用&vec,我需要在test.h和test.cpp中发布吗? - TT12

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接