How to access elements of type std :: vector

my program has a function to calculate a certain amount, for this I need to access the attributes of the objects in the vector:

Vector Declaration:

class trilateration {
public:
    ...
    std::vector<tip> *potential;
    ...
};

then in the constructor it is initialized:

trilateration::trilateration()
{
...
potential = new std::vector<tip>();
...
}

the class is as follows:

class tip {
public:

double sum;
Point2d *pt;
tip();
tip(double x, double y);
virtual ~tip();
};

hint constructor:

tip::tip(double x, double y)
{
pt = new Point2d(x,y);
sum=0;
}

objects are added to the vector in some functions as follows:

potential->push_back(tip1);

then I want to access some objects in the vector:

void trilateration::get3points()
{

for(int i=0; i<potential->size(); ++i)
{
    for(int j=0; j<potential->size(); ++j)
    {
        potential[i].sum=potential[i].sum+normalize(potential[i].pt,potential[j].pt);
    }

}
}

when compiling im getting the following error:

error: ‘class std::vector<tip>’ has no member named ‘sum’
error: ‘class std::vector<tip>’ has no member named ‘pt’

How can I use these attributes from a vector?

EDIT:

after changing the potential to be a member of trilateration and pt to be a member of the tip, the program compiled, but when it occurs

potential.push_back(tip1);

throws:

*** glibc detected *** ./loktest: malloc(): memory corruption: 0x00792f10 ***
+4
source share
2

, new, .

- Java-, ++, Java. ++ ++. .

trilateration vector<tip>* vector<tip>:

class trilateration {
public:
    ...
    // WAS std::vector<tip> *potential;
    std::vector<tip> potential;
    ...
};

trilateration ; , new:

trilateration::trilateration()
{
    ...
    // REMOVED:
    // potential = new std::vector<tip>();
    ...
}

, , new, delete ( ) , =delete.

, , .

tip.
Point2d*, Point2d (non-pointer):

class tip {
public:

  double sum;

  // WAS: Point2d *pt;
  Point2d pt; // <-- note: no pointers here

  tip();
  tip(double x, double y);

  // NOTE: Do you really need a virtual destructor here?? 
  virtual ~tip();
};

:

tip::tip(double x, double y)
    : pt(x, y), sum(0)
{
  // REMOVE:
  // pt = new Point2d(x,y);
  //sum=0;
}

, .

+4

trilateration::potential std::vector<tip>*, potential[i] std::vector<tip>, trilateration::potential + i. , .

for(int j=0; j<potential->size(); ++j)
{
    (*potential)[i].sum=potential[i].sum+normalize((*potential)[i].pt,(*potential)[j].pt);
}

- . . - new , .

+3

Source: https://habr.com/ru/post/1617227/


All Articles