I am trying to use a list for my bullets.
Whenever I run my update bullet code it gives me this error:
Error 1 error C2662: 'Bullet::detectCollision' : cannot convert 'this' pointer from 'const Bullet' to 'Bullet &'
my list:
std::list<Bullet> bullet_list;
my update code:
std::list<Bullet>::const_iterator cIter;
for ( cIter = bullet_list.begin( ); cIter != bullet_list.end( ); cIter++ )
{
if((*cIter).detectCollision(monster.position,monster.getHeight(),monster.getWidth()))
{
//Do stuff here
}
if ((*cIter).detectCollision(boss.getPosition(),boss.getHeight(),boss.getWidth()))
{
//Do stuff here
}
}
my detect collision code:
bool Bullet::detectCollision(Vector objTwo,int heightT,int widthT)
{
if(position.getVectorX() >= objTwo.getVectorX() - (widthT/2) && position.getVectorX() <= objTwo.getVectorX() + (widthT/2)
&& position.getVectorY() >= objTwo.getVectorY() - (widthT/2)&& position.getVectorY() <= objTwo.getVectorY() + (heightT/2) && alive)
{
return false;
}
return true;
}
You need to declare detectCollision as const.
bool Bullet::detectCollision(Vector objTwo,int heightT,int widthT) const
When you don't, it's trying to do a conversion so it can call a non-const function on a const reference but it's not allowed.
You are using const_iterator but trying to access a non-const member function. Either use iterator instead of the const_iterator or declare the function as const if it can be a const member function.
A const_iter does not allow the values it points at to be changed, so when you're calling detectCollision you need to promise the compiler you also won't change those values(make the function const).
Related
I have two functions in my Character class:
void Character::dropItem(const Item & item)
{
for (list<const Item*>::iterator i = playerInventory.inventory.begin(); i != playerInventory.inventory.end(); i++) {
if ((*i) == &item) {
playerInventory.inventory.erase(i);
break;
}
}
}
double Character::getTotalWeight() const
{
for (list<const Item*>::iterator i = playerInventory.inventory.begin(); i != playerInventory.inventory.end(); i++) {
}
}
However, Visual Studio gives me an error
Error C2440 'initializing': cannot convert from 'std::_List_const_iterator<std::_List_val<std::_List_simple_types<_Ty>>>' to 'std::_List_iterator<std::_List_val<std::_List_simple_types<_Ty>>>'
on playerInventory in the first statement of the for loop in getTotalWeight().
I can't figure out why this error is occurring, because it's literally the same loop as the function just above it. Any insight is appreciated.
The function is a constant member function
double Character::getTotalWeight() const
So the data member playerInventory.inventory is considered as a constant data member. As a result you may not use a non-constant iterator.
Write instead
for (list<const Item*>::const_iterator i = playerInventory.inventory.cbegin(); i != playerInventory.inventory.cend(); i++) {
That is the member function begin returna const_iterator while you are trying to assign it to iterator and there is no corresponding conversion from the first iterator type to the second iterator type.
I am trying to change the values of a two-dimensional QVector (QVector<QVector<QString>). However when attempting it, I get the following error: passing 'const QString' as 'this' argument discards qualifiers [-fpermissive].
I first thought that the problem was in the way I was accessing the data in the QVector. I therefore tried accessing it via operator []. This gave me the following error: no match for 'operator=' (operand types are 'QVector<QString>' and 'QString'). I also tried dereferencing the vector (*SecondList), as I wasn't sure whether operator [] did it and got the following error: no match for 'operator*' (operand type is 'const QVector<QString>') .
Here is the problematic code:
void Handler::changePassword(QString newPassword, int id)
{
QString nP = newPassword;
int i = 0;
bool loopBreaker = false;
while (!loopBreaker)
{
if (SecondList->at(i).at(2) == QString::number(id))
{//Changes the value for password in "secondlist"
SecondList->at(i).at(0) = nP; // ----> ERROR
loopBreaker = true;
saveList();
}
i++;
}
}
Here is the header file for Handler class
class Handler : public QWidget
{
private:
QVector<QVector<QString>> *SecondList = new QVector<QVector<QString>>;
}
(code is ommitted for readability)
QVector at() returns a const T&, try using operator [] instead, which returns non-const &
So Ben answered this question over here
But the reason is that the QVector::at method is const and the full prototype is const T &QVector::at(int i) const.
So you are going to have to use the QVector::operator[] which the full prototype is T &QVector::operator[](int i)
So I would try SecondList->operator[](i).operator[](0) = nP;. Something along these lines.
I have the following code to sort a vector of a custom type. It used to work, but after building the code on another system it gives an error at compile time.
The context the sort() call is made.
std::vector<std::vector<AssemblyObject>>* LegoAssembler::getLayers(std::vector<AssemblyObject> completeAssembly)
{
std::vector<std::vector<AssemblyObject>>* layers = new std::vector<std::vector<AssemblyObject>>();
std::vector<AssemblyObject> cLayer;
double lastZ = 0;
std::sort(completeAssembly.begin(), completeAssembly.end(), AssemblyObject::compare);
...
}
The sorting function
bool AssemblyObject::compare(AssemblyObject &a, AssemblyObject &b){
return (a.getPosition()[2] < b.getPosition()[2]) ||
((a.getPosition()[2] == b.getPosition()[2]) && (a.getPosition()[1] > b.getPosition()[1])) ||
((a.getPosition()[2] == b.getPosition()[2]) && (a.getPosition()[1] == b.getPosition()[1]) && (a.getPosition()[0] > b.getPosition()[0]));
}
The Error
/usr/include/c++/4.8/bits/stl_algo.h:2263: error: invalid initialization of reference of type ‘AssemblyObject&’ from expression of type ‘const AssemblyObject’
while (__comp(*__first, __pivot))
/usr/include/c++/4.8/bits/stl_algo.h:2263: error: invalid initialization of reference of type ‘AssemblyObject&’ from expression of type ‘const AssemblyObject’
while (__comp(*__first, __pivot))
^
^
As I said, this happened after building the code on another system. I was thinking it had something to do with changing compiler versions, but then again, I think something as simple as a sort function wouldn't break. Plus I'd like the code to compile on both compilers if that was the case.
Really would appreciate some help,
Your code is attempting to take a non-const reference to a const object, which is not permitted. The compare function doesnt modify its arguments so change:
bool AssemblyObject::compare(AssemblyObject &a, AssemblyObject &b){
To
bool AssemblyObject::compare(const AssemblyObject &a, const AssemblyObject &b){
The error is pretty clear - you need compare to accept const lvalue-references, not mutable ones:
bool AssemblyObject::compare(const AssemblyObject &a, const AssemblyObject &b)
{
/* as before */
}
I need a user defined set according to the order I want. But When I wanted to access set members I got error The object has type qualifiers that are nor compatible with member function (I get this error when I place mouse pointer on error line. The error mentioned in title is fromm Error List after build)
typedef struct tagRECT
{
long left;
long top;
long right;
long bottom;
} RECT;
struct LabelRect : public RECT
{
bool isIsolatedFrom(LabelRect* pRect)
{
if (pRect->right < left ||
pRect->left > right ||
pRect->top > bottom ||
pRect->bottom < top)
return true;
return false;
}
};
class CDrawnLabel
{ public:
LabelRect m_LabelRect;
LabelRect* getLabelRect(){ return &m_LabelRect; }
bool operator<(CDrawnLabel & rhs)
{
//This is the set ordering
return getLabelRect()->right < rhs.getLabelRect()->right;
}
}
I have a set like following
typedef std::set<CDrawnLabel> DrawnLabelSet;
DrawnLabelSet m_setDrawnLabel
I got error when I tried to access set members
DrawnLabelSet::iterator itbegin,itend;
LabelRect* pRectSecond;
itbegin=m_setDrawnLabel.begin();
itend=m_setDrawnLabel.end();
pRectSecond=(*itbegin).getLabelRect();// Here I get the error.
The reason you get this error is because keys inside std::set<T> are stored as const T.
So this expression (*itbegin) returns a const CDrawnLabel. Only const member functions can be called from a const object.
You will have to make getLableRect const. Also since const member functions can only return const pointers/references the member should be:
const LabelRect* getLabelRect() const { return &m_LabelRect; }
Not required but it would be a good idea to make your comparator const as well since it's not modifying any data. Another improvement that can be done is instead of taking a reference you should pass a const ref to the comparator.
bool operator<(const CDrawnLabel &rhs) const
{
//This is the set ordering
return getLabelRect()->right < rhs.getLabelRect()->right;
}
The problem here is that std::set<>::iterator is actually a const_iterator so (*itbegin) has the type const CDrawnLabel&. Why is this? Well, if you could change the reference in the set, you could invalidate the ordering. So you need to take the object out of the set, modify it and then put it back in. Or, if you don't want to change it you could define a const function getConstLabelRect()
I setup a class called tagToken.
It has setter functions, one is void setString(QString)
I have it defined/declared as such
.h file
void setString(QString string);
Private:
QString stringOfTagToken ;
.cpp file
void tagToken::setString(QString string)
{
stringOfTagToken = string;
}
When I try to run this code:
if (linePosition == 1)
{
QVector<tagToken> temp(0);
//errors
//temp.at(0).setString(line);
temp.at(0).setString("test");
//tags.at(0).setString(line);
//tags.push_back();
tagTokenCounter++;
}
I get this error:
C:\Dev\DiffMatchPatch\diffmatchpatch.cpp:316: error: passing 'const tagToken' as 'this' argument of 'void tagToken::setString(QString)' discards qualifiers [-fpermissive]
temp.at(0).setString("test");
QVector's at function returns data as const. Use at when you don't want to (accidentally) change the vector data, or operator[] in general.
temp[0].setString("test");
QVector::at() returns a const ref to your data, you cannot call a non-const method like setString on that
From http://qt-project.org/doc/qt-4.8/qvector.html#at
const T & QVector::at ( int i ) const
Returns the item at index position i in the vector.
i must be a valid index position in the vector (i.e., 0 <= i < size()).
Consider using operator[] instead