g++: error: invalid use of member (did you forget the '&' ?)

B

bazley

I've been tearing my hair out over this:

#ifndef MATRIX2_H
#define MATRIX2_H

#include <QVector>
template<class T>
class Matrix2
{
public:
Matrix2() : m_rows(0), m_cols(0) {}
Matrix2(int rows,int cols) : m_rows(rows), m_cols(cols), m_data(rows *
cols) {}
Matrix2(int rows, int cols, QVector<T> data) : m_rows(rows),
m_cols(cols), m_data(data) {}

void resize(int rows, int cols)
{
m_data.resize(rows * cols);
m_rows = rows;
m_cols = cols;
}

T& operator()(int x, int y)
{
return m_data[x * cols + y];
}

const T& at(int x, int y) const
{
return m_data.at(x * cols + y);
}

int rows() const
{
return m_rows;
}

int cols() const
{
return m_cols;
}
private:
int m_rows, m_cols;
QVector< T > m_data;

};
#endif

offending line:
class DataTable {
..
..
Matrix2<int, int> m_data;
..
..
}

void DataTable::readData() {
..
..
m_data(row, varIndex) = DataTable::MISSING;
..
..
}

Error:
Matrix2.h: In member function 'T& Matrix2<T>::eek:perator()(int, int)
[with T = int]':
DataTable.cpp:85: instantiated from here
Matrix2.h:22: error: invalid use of member (did you forget the '&' ?)
Matrix2.h: In member function 'const T& Matrix2<T>::at(int, int) const
[with T = int]':
DataTable.cpp:147: instantiated from here
Matrix2.h:27: error: invalid use of member (did you forget the '&' ?)

Thanks in advance!
 
I

Ian

I've been tearing my hair out over this:

#ifndef MATRIX2_H
#define MATRIX2_H

#include <QVector>
template<class T>
class Matrix2
offending line:
class DataTable {
.
.
Matrix2<int, int> m_data;
.
Why two template arguments when Matrix2 only has one?

Ian
 
B

bhaberman

Figured it out: I used cols instead of m_cols. Of course, the compiler
helpfully gives me an "Invalid use of member" error which has no actual
meaning instead of saying the obvious "cols not defined"
 
E

Earl Purple

"cols" is undefined in those functions. It should be m_cols

And what is QVector? Is it the same as std::vector?
 
S

stephen.webb

Did you really mean

T& operator()(int x, int y)
{
return m_data[x * cols + y];
}

since there is no "cols" in scope?

In the future you might want to post the code you're having a problem
with, and add line number so we don't have to guess.

-- smw
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

Figured it out: I used cols instead of m_cols. Of course, the compiler
helpfully gives me an "Invalid use of member" error which has no actual
meaning instead of saying the obvious "cols not defined"

You probably have figured this one out too, but for the sake of
completeness; cols was defined in your original code:

template<class T>
class Matrix2
{
[...]

T& operator()(int x, int y)
{

Invalid use of member is right here:

return m_data[x * cols + y];
}

[...]

Here is the member that is being used above in an invalid way:

int cols() const
{
return m_cols;
}
};

Ali
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top