We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,039 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Can't pass vector to function

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";
}
2
Contributors
4
Replies
1 Hour
Discussion Span
5 Months Ago
Last Updated
5
Views
Question
Answered
M4ver1k
Newbie Poster
10 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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 :)

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16

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

M4ver1k
Newbie Poster
10 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

No problem :)!

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

phorce
Master Poster
738 posts since Jul 2011
Reputation Points: 63
Solved Threads: 91
Skill Endorsements: 16
Question Answered as of 5 Months Ago by phorce

Woops, done. Thanks again!

M4ver1k
Newbie Poster
10 posts since Dec 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0757 seconds using 2.78MB