Reputation: 15
I have a code like this: Forgive me Polish names for stuff, I hope it isn't a problem. I can change them if you wanna.
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
//a few unimportant functions
void zamianaNaDziesietny(int podstawa, int liczba, int reszta[])
{
vector < int > cyfra;
cout <<"\n";
for(int a = reszta.size(); a > 0; a--)
{
cyfra.push_back(reszta[a - 1] * pow(podstawa, a - 1));
}
for(int a = 1; a < cyfra.size(); a++)
{
cyfra[0] += cyfra[a];
}
cout << cyfra[0];
}
void zamianaNaDziesietny(int podstawa, int liczba, int reszta[])
{
vector < int > cyfra;
cout <<"\n";
for(int a = reszta.size(); a > 0; a--) //request for member 'size' in 'reszta', which is of non-class type 'int*'
{
cyfra.push_back(reszta[a - 1] * pow(podstawa, a - 1));
}
for(int a = 1; a < cyfra.size(); a++)
{
cyfra[0] += cyfra[a];
}
cout << cyfra[0];
}
//another unimportant part of code
int main()
{
//unimportant stuff
zamianaZDziesietnego(podstawa, liczba);
zamianaNaDziesietny(podstawa, liczba, reszta);
return 0;
}
I dunno how to use vector 'reszta' as an argument in function "zamiananaDziesietny". What should I type to let this program compile?
I'm getting 2 errors: One of them is: 'reszta' was not declared in this scope
It's an array... Oh well, vector is a kind of array... I think... I'm noob so I can always be wrong.
I can work on this vector in main, adding "cout << reszta[0];" (just to write on the screen an element of this vector) in main, just between inductions of those two functions worked well. But I think this problem could be solved by setting this vector in main and then it will be as an argument in zamianaZDziesietnego() function too, but...
I dunno how to use a vector as an argument in function.
The second error I'm getting is: request for member 'size' in 'reszta', which is of non-class type 'int*' It's in the 32nd line.
So this is my problem. I have tried typing this argument in a few different ways.
When I tried with: void zamianaNaDziesietny(int podstawa, int liczba, vector reszta[])
The second error changed to: request for member 'size' in 'reszta', which is of pointer type 'std::vector*' (maybe you meant to use '->' ?)|
And also got a new error a few lines below:
no match for 'operator*' in '*(reszta + ((((sizetype)a) + -1u) * 12u)) * pow((double)podstawa, (double)(a + -1))'|
Well, I am very a big noob and I dunno how to solve this. Could anyone help?
Btw, if anyone was interested - I'm making a program that converts binear counts to decimal and vice versa.
Upvotes: 0
Views: 2892
Reputation: 4349
Vectors are different than arrays, as vectors are classes and can have methods called upon them (ex. .size()
. Arrays are from c which are really just pointers in memory. Passing in the reference as stated would be the best way to pass in the vector, but make sure to be careful about modification.
Upvotes: 1
Reputation: 4452
Just declare the argument like this:
void zamianaNaDziesietny(int podstawa, int liczba, std::vector<int> reszta)
{
vector < int > cyfra;
cout <<"\n";
for(int a = reszta.size(); a > 0; a--)
{
cyfra.push_back(reszta[a - 1] * pow(podstawa, a - 1));
}
for(int a = 1; a < cyfra.size(); a++)
{
cyfra[0] += cyfra[a];
}
cout << cyfra[0];
}
Or better still, pass the vector by reference to avoid it being copied:
void zamianaNaDziesietny(int podstawa, int liczba, std::vector<int>& reszta)
{
vector < int > cyfra;
cout <<"\n";
for(int a = reszta.size(); a > 0; a--)
{
cyfra.push_back(reszta[a - 1] * pow(podstawa, a - 1));
}
for(int a = 1; a < cyfra.size(); a++)
{
cyfra[0] += cyfra[a];
}
cout << cyfra[0];
}
What you have done is use an int array (int reszta[]
) as the argument rather than a vector - they are different types.
Upvotes: 3