I could explain something else about these things that I don’t understand?
The program works when I separate into parts
And what would that separation look like? You can post a code like this?
But now I want to call a member variable from a function I created
Which means "call variable member of a function?" Classes have members. Functions have a name, arguments and a return type. And they usually belong to a class. I could have told you which variable and which function you’re talking about.
When I compile, it does not appear although the compilation does not show any fault
What does it mean? Variables do not appear in the compilation.
I’ve tried to create another way to instantiate the variable in a class
And what shape would that be?
About your program
TransformStrsToDec* acess11;
acess11 = new TransformStrsToDec;
acess11->TransformStrsToDec::strnbinars();
This construction suggests that strnbinars()
is like a class builder TransformStrsToDec
.
And the class name suggests that it transforms Strs
to Dec
. Reading the code looks like the Str
are in a vector of string
storageRangeStr
and the Dec
are in dec_storageRangeStr
, a vector of long int
.
If this is the case, perhaps a simpler and more descriptive implementation of the class was
something like that
class Trf
{
private:
vector <string> Str;
vector <long int> Dec;
public:
long int sum;
long int N, n;
std::string rangeStr;
std::string strc0;
public:
long int strnbinars(void) {};
Trf(int n, int N) : n(n) , N(N) {};
Trf() : n(6), N(1800) {};
protected:
int k, j, index1, r, index2;
};
See this snippet with the original statement and an example using Trf
TransformStrsToDec* acess;
acess = new TransformStrsToDec;
Trf* access = new Trf();
Trf* outro = new Trf(12, 3600);
Compare. Trf
has two constructors (polymorphism) so if you declare no parameters as access
in the example, it will call the constructor without parameters that supposes n = 6
and N = 1800
. But in the statement of outro
will create the class with n=12
and N = 3600
. Maybe you’ll find it more comfortable this way.
And within the constructors you can generate the vectors Str
and Dec
and then divide the code from strbinars()
that manipulates the vectors using a function for each manipulation. So it is easier to test, probably.
I didn’t go through with your program because you didn’t post the specification and I don’t understand the algorithms, so I hope these observations help with something.
I didn’t understand those statements:
char b1n[1] = { '1' };
char b0n[1] = { '0' };
That are vectors of a single char
and that are never changed? They could not be
const char b1n = '1';
for example?
From C++11 vc you can iterate through the elements of an STL container with a
for (auto& item : lista_itens) { }
.– aviana