1,105,229 Community Members

Can't pass vector to function

Member Avatar
M4ver1k
Newbie Poster
10 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'll try to keep it brief. When I try to pass the 'list' vector to the counter function my compiler throws an error of: "error: request for member size in count, which is of non-class type std::vector<int>*"

Googling hasn't produced anything quite what I'm looking for syntax wise. If someone could enlighten me I'd be most appreciative.

My compiler is g++ version 4.6.3.

#include <iostream>
#include <vector>

using namespace std;

void counter(vector<int> count[]);

int main (){    
        vector<int> list[5];
        counter(list);

        return 0;
}

void counter(vector<int> count[]){
        int i, j=0;

        for(i = 0; i <= count.size(); i++){
                j++;
        }

        cout << "There are " << j << " elements.\n";
}
Member Avatar
phorce
Veteran Poster
1,005 posts since Jul 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 29 [?]
Featured
 
1
 

You can't pass the vector as an array.. Your problem is here:

void counter(vector<int> count[]);

Do this:

void counter(vector<int> &count); // sending reference

Also, you can't allocate memory for a vector like this:

vector<int> list[5];

Instead you can do this:

vector<int> list(5, 0); // size of 5, pushes back 0's

Here, the code:

#include <iostream>
#include <vector>

using namespace std;

void counter(vector<int> &count);

int main (){    
        vector<int> list(5, 0);
        counter(list);

        return 0;
}

void counter(vector<int> &count){
        int i, j=0;

        for(i = 0; i <= count.size(); i++){
                j++;
        }

        cout << "There are " << j << " elements.\n";
}

Hope this helps :)

Member Avatar
M4ver1k
Newbie Poster
10 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Wow, your response has been tremendously helpful. Thank you!

Member Avatar
phorce
Veteran Poster
1,005 posts since Jul 2011
Reputation Points: 105 [?]
Q&As Helped to Solve: 133 [?]
Skill Endorsements: 29 [?]
Featured
 
0
 

No problem :)!

Please mark this as solved, if you have any more questions feel free to ask / post! Good luck

Question Answered as of 1 Year Ago by phorce
Member Avatar
M4ver1k
Newbie Poster
10 posts since Dec 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Woops, done. Thanks again!

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article