Hi!

I try to implement a matrix template class which inherits the vector of vectors.
The message I get: "error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const Vector<T> *' (or there is no acceptable conversion)" (When I call the alternate matrix constructor, at the line "this = &h;".)
Any thoughts...? Thanks a lot in advance.

vectorTemplate.h:

#ifndef _VECTOR_TEMPLATE_H
#define _VECTOR_TEMPLATE_H

#include <iostream>
#include <fstream>

//#include "complex.h"

template<class T>
class Vector 
{ private:
	int num; // Number of elements
   	T* pdata; // Pointer to the data
	void Init(int Num); // private function since user should not call it
					// only for the member functions to call
 public:
   	Vector(); // default constructor
   	Vector(int Num); // alternate constructor
   	Vector(const Vector& v); // copy constructor
   	~Vector(); // destructor
	int GetNum() const; // access function
   	Vector<T>& operator= (const Vector<T>& v); //  overloaded assignment operator
   	T& operator[] (int i) const; // overloaded array access operator
   	template<class T> friend std::istream& operator>>(std::istream& is, Vector<T>& v);// keyboard input
   	template<class T> friend std::ostream& operator<<(std::ostream& os, Vector<T>& v);// screen output
   	template<class T> friend std::ifstream& operator>>(std::ifstream& ifs, Vector<T>& v);// file input
   	template<class T> friend std::ofstream& operator<<(std::ofstream& ofs, Vector<T>& v);// file output
 };

// default constructor
template<class T>
Vector<T>::Vector() : num(0), pdata(0) {}

// initialise data, called by the constructors
template<class T>
void Vector<T>::Init(int Num)
{
	num = Num;
  	if (num <= 0)
     		pdata = 0;  // Object construction failed!
   	else
  		pdata = new T[num];  // Allocate memory for vector
}

// alternate constructor 
template<class T>
Vector<T>::Vector(int Num)
{
	Init(Num);
}

// copy constructor
template<class T>
Vector<T>::Vector(const Vector& copy) {
Init(copy.GetNum()); // allocate the memory 

// copy the data members
if (pdata) for (int i=0; i<num; i++) pdata[i]=copy.pdata[i]; 
}



// destructor
template<class T>
Vector<T>::~Vector()
{
   	delete [] pdata; // free the dynamic memory 
}



// assignment operator 
template<class T>
Vector<T>& Vector<T>::operator=(const Vector& copy)
{
if (this == &copy) return *this; // Can't copy self to self (that is v = v 
// in main is dealt with)
   	delete [] pdata; // delete existing memory
   	Init(copy.GetNum()); // create new memory then copy data
  	if (pdata) for (int i=0; i<copy.GetNum(); i++) pdata[i] = copy.pdata[i]; 

  	return *this;
}



// array access operator
template<class T>
T& Vector<T>::operator[](int i) const
{
   	if (i < 0) 
     		i = 0;  // Range error causes index to equal 0
			// should really throw an exception
   	return pdata[i];
}



// return the size of the vector
template<class T>
int Vector<T>::GetNum() const
{
  	return num;
}

 

// keyboard input 
template<class T>
std::istream& operator>>(std::istream& is, Vector<T>& v) {
  	int Num;

   	std::cout << "input the size for the vector\n";
     	is >> Num;

     	// create a temporary Vector object of correct size
     	Vector<T> temp(Num);

	// input the elements
     	std::cout << "input the vector elements\n";
  	for (int i=0; i<Num; i++) is >> temp[i];

	// copy temp into v
	v = temp;

	// return the stream object
  	return is;
}

// file input
template<class T>
std::ifstream& operator>>(std::ifstream& ifs, Vector<T>& v)
{
     	int Num;

	// read size from the file
     	ifs >> Num;

	// create a temporary Vector object of correct size
     	Vector<T> temp(Num);

	// input the values
     	for (int i=0; i<Num; i++) ifs >> temp[i];

	// copy temp into v
	v = temp;

	// return the file stream object
  	return ifs;
}
// screen output
template<class T>
std::ostream& operator<<(std::ostream& os, Vector<T>& v)
{
   	if (v.pdata) {
     		os << "The vector elements are\n";
     		for (int i=0; i<v.GetNum(); i++) os << v[i]  << "\n";
   	}
  	return os;
}
// file output
template<class T>
std::ofstream& operator<<(std::ofstream& ofs, Vector<T>& v)
{
   	if (v.pdata) {
     		ofs << "The vector elements are\n";
     		for (int i=0; i<v.GetNum(); i++) ofs << v[i]  << "\n";
   	}
  	return ofs;
}

#endif

matrixTemplate.h:

#ifndef _MATRIX_TEMPLATE_H
#define _MATRIX_TEMPLATE_H

#include <iostream>//
#include <fstream>//
#include "vectorTemplate.h"

//#include "complex.h"

template<class T>
class Matrix : public Vector< Vector<T> >{
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
};

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)/*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)));//this[i] = *(new Vector<T>(Ncols));
	}
}

template<class T>
Matrix<T> operator*(const Matrix<T>& m1, const Matrix<T>& m2) 
// overload * for matrix multiplication
{
	
	if (m1[0].GetNum() == m2.GetNum())
	{	
		Matrix<T> m3(m1.GetNum() , m2[0].GetNum());
		for (int i = 0; i < m1.GetNum(); i++)
		{
			for (int j = 0; j < m2[0].GetNum(); j++)
			{
				
				for (int k = 0; k < m1[0].GetNum(); k++)
				{
					if (k == 0) m3[i][j] = m1[i][k] * m2[k][j];
					else m3[i][j] = m3[i][j] + m1[i][k] * m2[k][j];
				}
			}
		}
		return m3;
	}
	else
	{
		std::cout << "input matrices are not multicable\n";
		return m1;
	}
}

//screen input
template<class T>
std::istream& operator>>(std::istream& is, Matrix<T>& m) {
	int Nrows, Ncols;

	// input the size of the matrix
     	std::cout << "input num of rows and columns\n";
     	is >> Nrows >> Ncols;
     	
// create a temporary matrix of the correct size
     	Matrix<T> temp(Nrows, Ncols);
	
      std::cout << "input the matrix elements\n";
  	for (int i=0; i<Nrows; i++) 
	{
		for (int j=0 ; j<Ncols; j++) 
		{std::cout << "hi";
			is >> temp[i][j];std::cout << "ha";
		}
	}
	
     	// copy temp to m
     	m = temp;
	return is;
}

// file input
template<class T>
std::ifstream& operator>>(std::ifstream& ifs, Matrix<T>& m)
{
     	int Nrows, Ncols;

	// read size from the file
     	ifs >> Nrows >> Ncols;

    // create a temporary Matrix object of correct size
     	Matrix<T> temp(Nrows, Ncols);

	// input the values

	for (int i=0; i<Nrows; i++) 
for (int j=0 ; j<Ncols; j++) ifs >> temp[i][j];

     	// copy temp to m
     	m = temp;

  	// return the file stream object
		return ifs;
}

// screen output
template<class T>
std::ostream& operator<<(std::ostream& os, Matrix<T>& m)
{
   	if (m.GetNum() > 0) {
     		os << "The matrix elements are\n";
     		for (int i=0; i<m.GetNum(); i++) 
			{
				if (m[0].GetNum() > 0) 
				{
					for (int j=0; j<m[0].GetNum(); j++) 
					{
						os << m[i][j]  << "\n";
					}
				}
			}
   	}
  	return os;
}
// file output
template<class T>
std::ofstream& operator<<(std::ofstream& ofs, Matrix<T>& m)
{
   	if (m.GetNum() > 0) {
     		ofs << "The matrix elements are\n";
     		for (int i=0; i<m.GetNum(); i++) 
			{
				if (m[0].GetNum() > 0) 
				{
					for (int j=0; j<m[0].GetNum(); j++) 
					{
						ofs << m[i][j]  << "\n";
					}
				}
			}
   	}
  	return ofs;
}

#endif

Can you explain why did you wrote this absolutely senseless expression: this[i] = &h ? If you want inherited operator[] , write (*this)[i] ...

Can you explain why did you wrote this absolutely senseless expression: this[i] = &h ? If you want inherited operator[] , write (*this)[i] ...

I tried many combinations (some of them are still there as comments), among them this[i] = new Vector<T>(Ncols); too. (*this)[i] = new Vector<T>(Ncols); is not working either.

It's of no importance. What did you want to do there - that is a question.

Why post 500 lines of code? Can you simplify the code to < 20 lines so we can look at it?

how did you solve the problem?

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.