How to access vectors of a class in C++

Asked

Viewed 58 times

-1

**I am familiar with pure C language because I develop embedded systems and microcontrollers. I am dedicating a lot in the studies of a new paradigm of programming that is object orientation (OOP). I got involved in a personal project, a few days ago, of starting a small development called the genetic algorithm. It is still only an outline of how to create a "population" (a large string) and separate into "chromosomes" ("pieces of that string" or substrings for each "individual").

So far, so good. The program works when I separate it into parts. But now I want to call a member variable from a function I created. When I compile, it does not appear although the compilation does not show any fault. I have tried to create another way to instantiate the variable in a class. I wonder if there is someone with good experience in OOP to analyze this problem and suggest new revisions. I thank you already for the manifestations. ** Look at line 187: cpp.sh/45jda

  • From C++11 vc you can iterate through the elements of an STL container with a for (auto& item : lista_itens) { }.

2 answers

1

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?

0

  • @arfnet

"This construction suggests that strnbinars() is like a constructor of the class Transformstrstodec."*

I intended to make strnbinars() the name of the Transformstrstodec class method. I put a lot into this method and maybe I shouldn’t have done it. Within this method, strings that are within storageRangeStr are transformed to decimal. Then, within this same method, I chose the first two values that are inside the dec_storageRangeStr[0] vector to convert again to string. The output is Select0[0] (as can be seen in the result of the program, called "chromosome"). And it’s this output that I’d like to "use" again in the other class called by me Segment_crss. I am studying your suggestions and especially perfecting my knowledge about builders.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.