PDA

View Full Version : no match for 'operator=' in...



toss
13th April 2010, 16:31
Hi

I'm trying to make a version of minesweeper in Qt4. In order to do this i have subclassed QPushButton to make a new class, MinesveiperButton. This seems to work fine, however when i try to make a new grid, using a 2-dimentional array and fill it with MinesveiperButtons i get this error:

C:/C++ Qt/Minesveiper/v3/minesveiper.cpp:52: error: no match for 'operator=' in '*((*(((Minesveiper*)this)->Minesveiper::mask + ((unsigned int)(((unsigned int)i) * 4u)))) + ((unsigned int)(((unsigned int)j) * 28u))) = (operator new(28u), (<statement>, ((MinesveiperButton*)<anonymous>)))'

This is Minesveiper.h:

#ifndef MINESVEIPER_H
#define MINESVEIPER_H

#include <QMainWindow>
#include <QGridLayout>
#include "MinesveiperButton.h"

namespace Ui {
class Minesveiper;
}

class Minesveiper : public QMainWindow {
Q_OBJECT
public:
Minesveiper(QWidget *parent = 0);
~Minesveiper();

void makeBoard();
void startSpill(int h, int w, int m);

public slots:
void startEnkeltSpill();

protected:
void changeEvent(QEvent *e);

private:
Ui::Minesveiper *ui;

int mHeight;
int mWidth;
int mines;
int antOpen;
int **board;
MinesveiperButton **mask;
QGridLayout grid;
};

#endif // MINESVEIPER_H

and this is MinesveiperButton.h:

#ifndef MINESVEIPERBUTTON_H
#define MINESVEIPERBUTTON_H

#include <QtGui/qpushbutton.h>
#include "minesveiper.h"

class MinesveiperButton : public QPushButton {
Q_OBJECT
public:

MinesveiperButton(int x, int y);
MinesveiperButton();
~MinesveiperButton();

void open();
void flag();
void setIcon();

//MinesveiperButton & operator=(const MinesveiperButton &rhs);

private:
int xpos;
int ypos;

};

#endif // MINESVEIPERBUTTON_H


and finally implementation of Minesveiper::makeBoard() from Minesveiper.cpp, which is where i get the error:

void Minesveiper::makeBoard() {
/*
Deletes board and mask, and makes new arrays.
Empties grid and adds new MinesveiperButtons to grid.
*/
delete [] board;
delete [] mask;

// Empty grid
int ant = grid.count();
for(int i = 0; i < ant; i++) {
delete grid.itemAt(i);
}

// Make new arrays
board = new int *[mHeight];
mask = new MinesveiperButton *[mHeight];
for(int i = 0; i < mHeight; i++) {
board[i] = new int[mWidth];
mask[i] = new MinesveiperButton[mWidth];
for(int j = 0; j < mWidth; j++) {
board[i][j] = 0;
mask[i][j] = new MinesveiperButton(j, i);
grid.addWidget(&mask[i][j], i, j);
}
}
}

line 52 is numbered 23 here..

Can someone help me with this? I thought =operator didn't need overloading. Does it?
Just tell me if you want to view more of the code..

JohannesMunk
13th April 2010, 17:53
Hi there!

MinesveiperButton **mask will give you a pointer to a pointer of type MinesveiperButton. If you use twodimensional-array indexing you end up just with MinesveiperButton, not MinesveiperButton*!

You should use a higherlevel approach like QList<QList<MinesveiperButton* >> mask; to avoid such problems!

But why do you want to keep the extra list anyway? You can use the Gridlayout:

MinesveiperButton* button = qobject_cast<MinesveiperButton*>(grid.itemAtPosition(i,j).widget());

HIH

Johannes

toss
14th April 2010, 00:08
Thanks, that is a great idea! Worked good!
:)