Hi!

I wrote two functions in order to access two private data members of a template class and I got the messages in the title every time I call these functions(getNcols(), getNrows()). I'd be very grateful for your help, guys...
(Without the "template<class T>" before the friends, I got linker errors and a simpler code worked with them, so I suppose they are needed...) Here comes the code. Thx, gyagyus.

template<class T>
class Matrix : public Vector< Vector<T> >{
 private:
//Vector<Vector<T>> v; 	// Vector used to store the matrix elements
int nrows;  	// number of rows of the matrix
int ncols;	// number of columns of the matrix

public:
Matrix(); // default constructor, uses default constructor for v
Matrix(int Nrows, int Ncols);  // alternate constructor
//T& operator() (int i, int j) const; //  function call overload (-,-)
template<class T> friend Matrix<T> operator*(const Matrix<T>& m1, const Matrix<T>& m2); // overload * for matrix multiplication
template<class T> friend std::istream& operator>>(std::istream& is, Matrix<T>& m);// keyboard input
template<class T> friend std::ostream& operator<<(std::ostream& os, Matrix<T>& m);// screen output
template<class T> friend std::ifstream& operator>>(std::ifstream& ifs, Matrix<T>& m);// file input
template<class T> friend std::ofstream& operator<<(std::ofstream& ofs, Matrix<T>& m);// file output
int GetNrows() const; // access function
int GetNcols() const; // access function
};

template<class T>
Matrix<T>::Matrix() : Vector< Vector<T> >(), nrows(0), ncols(0)/*, Vector() */{}// default constructor, uses default constructor for v

template<class T>
Matrix<T>::Matrix(int Nrows, int Ncols) : Vector< Vector<T> >(), nrows(Nrows), ncols(Ncols)/*, Vector(Nrows)*/  // alternate constructor
{
	for (int i = 0; i < Nrows; i++)
	{
		/*const Vector<T>* d = new Vector<T>(Ncols);
		this[i] = d;*/
		Vector<T>* d = new Vector<T>(Ncols);
		const Vector<T> &h = *d;
		this[i] = h;
		//this[i] = *(new Vector<T>(Ncols));//this[i] = &(*(new Vector<T>(Ncols)));//this[i] = *(new Vector<T>(Ncols));
	}
}

// return the number of rows
template<class T>
int Matrix<T>::GetNrows() const
{
  	return nrows;
}

// return the number of columns
template<class T>
int Matrix<T>::GetNcols() const
{
  	return ncols;
}
Salem commented: Congrats on using code tags on your first post. +29

Dani AI

Generated

Short answer: is right — C++ is case sensitive, so calling getNcols() will not find GetNcols(). That exact mismatch is the compile error C2039 you saw.

A couple of extra checks that often hide behind that same message:

  • Make sure the call site uses the exact identifier and the same scope (including const where appropriate).
  • Ensure the compiler actually sees the member declaration (for templates that usually means definitions must be in the header).
  • If you call the member through a pointer/reference, confirm the expression type really is Matrix<...> and not some other type or base class.

Some constructive cleanup you can apply to the constructor and layout (these avoid other common runtime/maintenance bugs): prefer initializing the base container to the right size and avoid manual new. Also, index the object, not the pointer. Example pattern you can use inside the constructor body:

for (int i = 0; i < Nrows; ++i)
    (*this)[i] = Vector<T>(Ncols);

Or initialize the base in the initializer list so each row exists already.

One more note on the friend declarations you added: when declaring a friend template inside a class template, use a different template parameter name to avoid shadowing the outer T, for example template<class U> friend Matrix<U> operator*(const Matrix<U>&, const Matrix<U>&);.

If the error persists after fixing the name, post the exact line that calls the accessor and the surrounding declarations (and your compiler version). That will make it easy to spot any remaining scope/name or template-instantiation issues.

Recommended Answers

All 2 Replies

Case matters.

Get != get

Case matters.

Get != get

Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.