Assigning a vector in structure to the content of QVector in principal
source link: https://www.codesd.com/item/assigning-a-vector-in-structure-to-the-content-of-qvector-in-principal.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Assigning a vector in structure to the content of QVector in principal
I have a structure :
struct node
{
QPoint position;
QVector<node> neighbours;
void add(QPoint);
void addNeighbours(QVector<node>);
...
};
With methods :
void node::add(QPoint p)
{
position = p;
}
void node::addNeighbours(QVector<node> n)
{
neighbours = n;
}
However when I try to use addNeighbours I get the following error :
error: C2662: 'node::addNeighbours' : cannot convert 'this' pointer from 'const node' to 'node &'
Conversion loses qualifiers
From looking online I think the solution comes from; using the correct pointers and possibly by the QVectors::Iterator()
. Although I cannot come up with a solution, any pointer in the right direction or an explanation on why this is happening is greatly appreciated.
Main:
int main(int argc, char *argv[])
{
QVector<node> map;
QVector<node> tmp;
node n;
//Populate map
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
n.add(QPoint(i,j));
map.append(n);
}
//Add required nodes to tmp
tmp.append(map.at(1));
tmp.append(map.at(3));
//Set the neighbour nodes of map(0) using tmp vector
map.at(0).addNeighbours(tmp);
}
Change
map.at(0).addNeighbours(tmp); // at() : returns a const reference
map[0].addNeighbours(tmp); // [] : returns a non-const reference
Also, it's better to change addNeighbours
to this:
void node::addNeighbours(const QVector<node> &n)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK