简体   繁体   中英

How to access members of type std::vector

my program has a function to calculate some sum, for this i need to access attributes of objects in vector:

declaration of vector:

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

then in constructor its initialized:

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

class tip looks like this:

class tip {
public:

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

tip constructor:

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

objects are added to vector in some function like this:

potential->push_back(tip1);

then i want to access some objects in vector like this:

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);
    }

}
}

while compilation im getting follwoing error:

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

how can i acess these attributes from vector?

EDIT:

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

potential.push_back(tip1);

throws:

*** glibc detected *** ./loktest: malloc(): memory corruption: 0x00792f10 ***

Unless there are strong reasons to have pointer data members and to allocate your objects on the heap using new , don't do that.

Your code seems to use a kind of Java-style, but this is C++ , not Java. Enjoy the automatic resource management of C++, and the power of C++ destructors. Just define data members without using pointers and dynamic allocations.

In class trilateration , change from vector<tip>* to vector<tip> :

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

In trilateration 's constructor, you don't need to create the vector dynamically; just delete the line allocating the vector with new :

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

Note that in your previous code, when you allocated the vector with new , you had to properly delete it in the destructor, and provide proper copy operations (copy constructor and copy assignment), or ban copies marking the aforementioned operations =delete .

Instead, you don't need all this complicated stuff if you have an ordinary simple non-pointer data member.

The same goes for your tip class.
Unless there is a strong reason to have a Point2d* data member, just use a Point2d (non-pointer) data member:

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();
};

Change the constructor as well:

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

Your code gets simplified, and you'll avoid some bugs and headaches.

Since trilateration::potential is a std::vector<tip>* , potential[i] is a std::vector<tip> located at trilateration::potential + i . This is not what you want.

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

is a possible hack. But what you really want is to get rid of all those useless pointers. Make the vector a member variable of your class and stop using new where it is not needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM