passing vector to function c++

23,877

Solution 1

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

Change your tester() function signature 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 you could pass this std::vector<T> and all have slightly different meanings:

// 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*>*);

Solution 2

  1. You should #include <string>.
  2. string name should read std::string name etc. Same goes for std::vector.
  3. You're calling tester() with a vector, yet it expects an array (the two are not interchangeable).
  4. s.sizeof() is incorrect for both an array and a vector; for the latter, use s.size() or, better yet, use an iterator.

These are just the errors that immediately jump out; there may be more.

Solution 3

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

Solution 4

A vector is not an array.

int tester(vector<Item *> &s)

(pass as a reference to avoid copying or if you need to modify)

You also need to modify your code inside the tester function to work correctly as a vector.

Share:
23,877
TT12
Author by

TT12

Updated on October 07, 2020

Comments

  • TT12
    TT12 over 3 years

    I have a main.cpp test.h and test.cpp> I am trying to pass my vector through so 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**)â
    
  • TT12
    TT12 over 12 years
    do i need to change this in my test.h and test.cpp?
  • TT12
    TT12 over 12 years
    well i have code that is going to edit s[i]-> name and s[i]-address in another function in my code that is not posted. With &vec do i need to post that in test.h and in test.cpp?
  • TT12
    TT12 over 12 years
    i have different functions. one im going to change the "const vector". im just trying to pass it so i can stop getting so many errors