I seem to be stuck on a vector assignment that I can't quite solve using the documentation on this site (which btw I usually find extremely helpful for reference).
#include <iostream.h>
usingnamespace std;
#include <iomanip.h>
usingnamespace std;
#include <vector.h>
using std::vector;
// This program will take a number and find its divisors, then store them in an interger vector.
void findDivisors(int, int, vector<int> *);
int main()
{
int number;
int vectorindex = 0;
vector <int> divisors(10);
cout << "Please enter the number for which you would like to find the divisors: " << endl;
cin >> number;
findDivisors (number, vectorindex, &divisors);
cin >> number; //Delay program exit
}
void findDivisors(int num, int index, vector<int> *integers )
{
for(int counter = 1; counter <= num; counter ++)
{
if ( num % counter == 0)
{
integers[index] = counter; ///ERROR HERE///
index ++;
}
}
}
The error says:
no match for 'operator=' in '*((+(((unsigned int)index) * 12u)) +integers) = counter'
I would very much appreciate a short explanation why this doesn't work and how to correct it. Thanks in advance
integers is a pointer to a vector. If you want it to behave correctly, change the * to an & on line 28. Or if you still want to deal with pointers, you need to dereference integers before using the [] operator, as currently it is offsetting the pointer.
And you need to add new elements to a vector using push_back.
It's pointless using vectors if you're going to set them to a fixed size and writing out of its bounds anyway.
Please explain Lines 2 and 4 to me. I'm very interested to know what I'm looking at here.
Your instructor may want the funtion at Line 28 to keep the template functionality of a vector. That's only if you've covered that sort of thing yet, otherwise don't worry about it.
Thanks for taking a look at this. L B , however when I change the * on line 28 to a & i get a different error message: int function main:
[Linked error]undefined refernce to 'findDivisors(int, int, std::vector<int, std::allocator<int> > *)'
ld returned 1 exit status
--OR--
if i try to deference line 35 as such:
*integers[index] = counter;
I get yet another different error message:
no match for 'operator*' in '**((+(((unsigned int)index) * 12u)) +integers) = counter'
--THEN--
I took Athar's advice (thanks also for your help). I tried to add an element to my vector as such:
integers.push_back(counter)
and i get yet ANOTHER different error message:
'push_back' has not been declared
request for member of non-aggregate type before '(' token
....
Any further advice or comments on correcting my program would be again greatly appreciated.
Computergeek, I'm no pro but as far as I know, if you specify using namespace std then you don't have to put using std::anything, it will know to look for whatever object in the std library. However, I dont believe this to be good coding practice because you are essentially polluting the global namespace with names you wont use which may or may not cause conflict else where, I'm really not too sure.