a function that returns vector
I'd like to write a function which returns a vector contains integer values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
|
#include <iostream>
#include <vector>
using namespace std;
vector<int>* myVector(int);
int main()
{
int n;
vector<int>* myConversionVector;
cout<<"Please Enter A Number:"<<endl;
cin>>n;
cout<<endl;
myConversionVector=myVector(n);
for (int i=0; i<myConversionVector.size();i++)
{
cout<<myConversionVector[i]<<endl;
}
return 0;
}
vector<int>* myVector(int number)
{
unsigned int remainder=0;
unsigned int division=10;
vector<int>* residual;
while (division!=0)
{
division=number/10;
remainder=number%10;
residual .push_back(remainder);
number=division;
}
return residual;
}
|
But this code does not work and I get the following error message:
|
error: request for member ‘push_back’ in ‘residual’, which is of pointer type ‘std::vector<int>*’ (maybe you meant to use ‘->’ ?)
|
any suggestion how to fix the problem?
Last edited on
> I'd like to write a function which returns a vector
¿then why are you using pointers?
1 2 3 4 5 6 7 8
|
vector<int> myConversionVector = myVector(n);
vector<int> myVector(int number){
//...
vector<int> residual;
//...
return residual;
}
|
Why did you define your vector instance as a pointer? I'd start by just getting rid of the pointer (everywhere) and see if that works.
Your function does not have any vector nor return a vector. It has and returns a
pointer to vector.
Similar function:
1 2 3 4
|
T * foo() {
T * uninitialized_pointer;
return uninitialized_pointer;
}
|
This is what you want:
1 2 3 4
|
T foo() {
T object;
return object;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <iostream>
#include <vector>
std::vector<int> makeVector(int);
int main()
{
int number = 5;
std::vector<int> vect = makeVector(number);
for (std::vector<int>::iterator it = vect.begin(); it != vect.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
std::vector<int> makeVector(int aNumber)
{
std::vector<int> aVector;
for (int i = 0; i < aNumber; i++)
aVector.push_back(i * 3);
return aVector;
}
|
Thanks @jlb it works after
getting rid of pointers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
vector<int> myVector(int);
int main()
{
int n;
vector<int> vect= myVector(n);
for (std::vector<int>::iterator it = vect.begin(); it != vect.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
vector<int> myVector(int number ){
unsigned int remainder=0;
unsigned int division=10;
vector<int> residual;
while (division!=0)
{
division=number/10;
remainder=number%10;
cout <<"division:\n"<<number<<endl<<remainder<<endl;
residual.push_back(remainder);
number=division;
}
return residual;
}
|
Please Enter A Number:
895632
division:
895632
2
division:
89563
3
division:
8956
6
division:
895
5
division:
89
9
division:
8
8
2 3 6 5 9 8 |
Topic archived. No new replies allowed.