Passing a vector to a C ++ function

I have main.cpp test.h and test.cpp> I'm trying to pass my vector so that I can use it in test.cpp, but I keep getting errors.

   //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**
+5
source share
6 answers

A std::vector<T>and T* []are not compatible types.

Change your function signature tester()as follows:

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

There are several ways to convey this std::vector<T>, and they all have slightly different meanings:

// This would create a COPY of the vector
// that would be local to this function 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*>*);
+13
source

Pass it as std::vector<Item *> &(vector reference) and use an iterator to iterate it.

+2
source
  • #include <string>.
  • string name std::string name .. std::vector.
  • tester() vector, ( ).
  • s.sizeof() , ; s.size() , , .

, ; .

+2

A vector .

int tester(vector<Item *> &s)

( , )

tester, .

+1

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

, , using namespace std; #include <string>

0

#include <string>
#include <vector>

and you need to use std::stringand std::vector<>. A is std::vectornot an array, so you must pass the vector as a reference

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

or even as const std::vector<Item*> &if you are not going to change the transferred vector.

Also, are you sure you will need a pointer vector? What are you trying to achieve?

0
source

All Articles