Hi again, I have a question about classes again. The book that I have been going through has finally expounded a bit about classes and we've been discussing constructors/deconstructors, etc The following is an exercise from the book. I have to implement the class Matrix (most of which was provided for me) and then create member functions to use with this class. When I am all said and done, I should be able to initialize an object to represent a matrix with r number of rows and c number of columns and the initial value of the elements should be v. I also know that in my matrix.cpp I have not created member functions for RowSize, ColumnSize and NumberRows as of yet. I wasn't completely clear on what to include in those, so I have not completed that portion yet. The book says that RowSize(int i) should return the size of row i, ColumnSize(int i) should return the size of column i and NumberRows(int i) should return the number of rows in the matrix.
I will paste my code below and am open to any suggestions or ideas. I will also paste the errors that I am getting (bear with me though, since there are many of them). On a sidenote: I have dealt with other issues in this book where (in VC ++ 6.0) it has required that a .h file is in the "External Dependcies" under the Fileview project tab. Perhaps that is part of my issue here as well? (Guessing).
Here's the code:
ex_1155.cpp:
Code:
#include <iostream>
#include <string>
#include "matrix.h"
#include "intlist.h"
using namespace std;
int main (){
Matrix A(3,4,1);
cout << A ;
return 0;
}
matrix.h (intlist is another part of this which interfaces with the code):
Code:
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <string>
#include "intlist.h"
class Matrix {
public:
// default constructor
Matrix(int r=10, int c=10, int v=0);
// copy constructor
Matrix(const Matrix &M);
// destructor
~Matrix();
// assignment operator
Matrix& operator=(const Matrix &M);
// inspector for row of constant matrix
const IntList& operator[](int i) const;
// inspector for row of non-constant matrix
IntList& operator[](int i);
// inspector for a size of a given row
int RowSize(int i) const;
// inspector for a size of a given column
int ColumnSize(int i) const;
// inspector for a number of row
int NumberRows(int i) const;
private:
// data members
int NumRows; // Row size of matrix
IntList *Values; // pointer to rows
};
#endif
matrix.cpp:
Code:
#include <assert.h>
#include "matrix.h"
// Constructor
Matrix::Matrix(int r, int c, int v){
assert (r>0);
NumRows=r;
Values = new int [r];
assert(Values);
}
// Copy constructor
Matrix::Matrix(const Matrix &M){
int NumRows=M.NumberRows;
Values=new int [NumberRows];
assert(Values);
}
// Destructor
Matrix::~Matrix(){
delete [] Values;
}
// Assignment operator
Matrix::operator =(const Matrix &M){
int NumRows=M.NumberRows;
Values=new int [NumberRows];
assert(Values);
return Values;
}
// Inspector for row of constant matrix
Matrix::operator[](int i) const{
assert((i>=0) && (i < RowSize()));
return Values[i];
}
// Inspector for row of non-constant matrix
Matrix::operator[](int i) {
assert((i>=0) && (i < RowSize()));
return Values[i];
}
And here are the error messages:
Compiling...
ex_1155.cpp
c:\program files\microsoft visual studio\myprojects\ex_1155\intlist.h(10) : error C2011: 'IntList' : 'class' type redefinition
c:\program files\microsoft visual studio\myprojects\ex_1155\ex_1155.cpp(12) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Matrix' (or there is no acceptable conversion)
matrix.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(9) : error C2440: '=' : cannot convert from 'int *' to 'class IntList *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(15) : error C2440: 'initializing' : cannot convert from 'int (__thiscall Matrix::*)(int) const' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(16) : warning C4551: function call missing argument list
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(16) : error C2440: 'initializing' : cannot convert from 'int (__thiscall Matrix::*)(int) const' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(16) : error C2440: '=' : cannot convert from 'int *' to 'class IntList *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(26) : error C2556: 'int __thiscall Matrix:perator =(const class Matrix &)' : overloaded function differs only by return type from 'class Matrix &__thiscall Matrix:perator =(c
onst class Matrix &)'
c:\program files\microsoft visual studio\myprojects\ex_1155\matrix.h(19) : see declaration of '='
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(26) : error C2040: '=' : 'int (const class Matrix &)' differs in levels of indirection from 'class Matrix &(const class Matrix &)'
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(27) : error C2440: 'initializing' : cannot convert from 'int (__thiscall Matrix::*)(int) const' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(28) : warning C4551: function call missing argument list
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(28) : error C2440: 'initializing' : cannot convert from 'int (__thiscall Matrix::*)(int) const' to 'int'
Conversion is a valid standard conversion, which can be performed implicitly or by use of static_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(28) : error C2440: '=' : cannot convert from 'int *' to 'class IntList *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(34) : error C2556: 'int __thiscall Matrix:perator [](int) const' : overloaded function differs only by return type from 'const class IntList &__thiscall Matrix:perator [](int
) const'
c:\program files\microsoft visual studio\myprojects\ex_1155\matrix.h(21) : see declaration of '[]'
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(34) : error C2040: '[]' : 'int (int) const' differs in levels of indirection from 'const class IntList &(int) const'
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(35) : error C2660: 'RowSize' : function does not take 0 parameters
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(40) : error C2556: 'int __thiscall Matrix:perator [](int)' : overloaded function differs only by return type from 'class IntList &__thiscall Matrix:perator [](int)'
c:\program files\microsoft visual studio\myprojects\ex_1155\matrix.h(23) : see declaration of '[]'
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(40) : error C2040: '[]' : 'int (int)' differs in levels of indirection from 'class IntList &(int)'
C:\Program Files\Microsoft Visual Studio\MyProjects\ex_1155\matrix.cpp(41) : error C2660: 'RowSize' : function does not take 0 parameters
Error executing cl.exe.