By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,909 Members | 2,016 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,909 IT Pros & Developers. It's quick & easy.

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

P: n/a
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>::operator()(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!

Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
<ba****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...

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

const T& at(int x, int y) const
{
return m_data.at(x * cols + y);
Same here...
} [...] int m_rows, m_cols;


Ali

Jul 23 '05 #2

P: n/a
Ian
ba****@gmail.com wrote:
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
Jul 23 '05 #3

P: n/a
Oops -- typo. It's really just

Matrix2<int> m_data;

Jul 23 '05 #4

P: n/a
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"

Jul 23 '05 #5

P: n/a
"cols" is undefined in those functions. It should be m_cols

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

Jul 23 '05 #6

P: n/a
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

Jul 23 '05 #7

P: n/a
<bh*******@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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

Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.