簡體   English   中英

將向量傳遞給函數c ++

[英]passing vector to function c++

我有一個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<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*>*);

將其作為std::vector<Item *> & (引用向量)傳遞,並使用迭代器迭代它。

  1. 你應該#include <string>
  2. string name應該讀取std::string name等。對於std::vector
  3. 你用一個vector調用tester() ,但是它需要一個數組(這兩個數組不可互換)。
  4. s.sizeof()對於數組和向量都是不正確的; 對於后者,使用s.size()或者更好的是使用迭代器。

這些只是立即跳出的錯誤; 可能會有更多。

vector不是數組。

int tester(vector<Item *> &s)

(作為參考傳遞以避免復制或者如果您需要修改)

您還需要修改tester功能中的代碼才能正常工作。

你應該修復

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

首先,可能是通過using namespace std; #include <string>

你缺少包括

#include <string>
#include <vector>

你需要使用std::stringstd::vector<> std::vector不是數組,因此您應該將向量作為引用傳遞

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

或者甚至作為const std::vector<Item*> &如果你不打算修改傳遞的向量。

另外,你確定,你需要一個指針向量嗎? 你想要實現什么目標?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM