Question

#ifndef __MATRIX__ #define __MATRIX__ #include "complex.h" namespace gtmath { class matrix { public: matrix(int , int...

#ifndef __MATRIX__
#define __MATRIX__

#include "complex.h"

namespace gtmath {
class matrix {
public:

matrix(int , int );

   //TODO: Add implementation here

   //TODO: Implement parenthesis operator

complex& operator()(int, int) {
return m_vals[rows][cols];
}
friend std::ostream& operator<<(std::ostream& os, gtmath::matrix& m);
  
int get_num_cols();
int get_num_rows();
  
  
  

private:
complex** m_vals;
int rows;
int cols;

};


}

#include "matrix.h"
#include <limits>


namespace gtmath {
//constructor & inlitilize dynamic array
matrix::matrix(int rows, int cols) {

this->cols = cols;
this->rows = rows;
m_vals = new complex * [rows];
for (int i = 0; i < rows; ++i) {
m_vals[i] = new complex[cols];
}
  
}
int matrix::get_num_cols() { return cols; }
int matrix::get_num_rows() { return rows; }
  
  

//constructor & inlitilize dynamic array
matrix::matrix(int rows, int cols) {
// don't define m_vals as local variable as it is private member of the class matrix (complex **m_vals)
m_vals = new complex * [rows];
for (int i = 0; i < rows; ++i) {
m_vals[i] = new complex[cols];
}

// no need to deallocate the matrix allocated
}

  

  

std::ostream& operator<<(std::ostream& os, gtmath::matrix& m) {
for (int i = 0; i < m.get_num_rows(); ++i) {
for (int j = 0; j < m.get_num_cols(); ++i) {
os << m.m_vals;
}
  
}
return os;

}
}

error C2084: function 'gtmath::matrix::matrix(int,int)' already has a body.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*

Actually you define same Constructor two times in Matrix.cpp file. I have highlighted the code to be removed in red Color. Please remove that part.

*/

#ifndef __MATRIX__
#define __MATRIX__

#include "complex.h"

namespace gtmath {
class matrix {
public:

matrix(int , int );

   //TODO: Add implementation here

   //TODO: Implement parenthesis operator

complex& operator()(int, int) {
return m_vals[rows][cols];
}
friend std::ostream& operator<<(std::ostream& os, gtmath::matrix& m);
  
int get_num_cols();
int get_num_rows();
  
  
  

private:
complex** m_vals;
int rows;
int cols;

};


}

#include "matrix.h"
#include <limits>


namespace gtmath {
//constructor & inlitilize dynamic array
matrix::matrix(int rows, int cols) {

this->cols = cols;
this->rows = rows;
m_vals = new complex * [rows];
for (int i = 0; i < rows; ++i) {
m_vals[i] = new complex[cols];
}
  
}

int matrix::get_num_cols() { return cols; }
int matrix::get_num_rows() { return rows; }
  
  
/**Remove this */
//constructor & inlitilize dynamic array
matrix::matrix(int rows, int cols) {
// don't define m_vals as local variable as it is private member of the class matrix (complex **m_vals)
m_vals = new complex * [rows];
for (int i = 0; i < rows; ++i) {
m_vals[i] = new complex[cols];
}

// no need to deallocate the matrix allocated
}

  

  

std::ostream& operator<<(std::ostream& os, gtmath::matrix& m) {
for (int i = 0; i < m.get_num_rows(); ++i) {
for (int j = 0; j < m.get_num_cols(); ++i) {
os << m.m_vals;
}
  
}
return os;

}
}

//If you need any help regarding this solution...... please leave a comment....... thanks

Add a comment
Know the answer?
Add Answer to:
#ifndef __MATRIX__ #define __MATRIX__ #include "complex.h" namespace gtmath { class matrix { public: matrix(int , int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • i created a two dimensional matrix class which holds my object complex number. I am trying...

    i created a two dimensional matrix class which holds my object complex number. I am trying to overload the parenthesis () so that I can access and modify the value at specified location of the matrix. say matrix[a][b] = 5; and my_vals is a private member of the matrix class. What is the problem with my code //constructor & inlitilize dynamic array matrix::matrix(int rows, int cols) { complex** m_vals = new complex * [rows]; for (int i = 0; i...

  • #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: //...

    #ifndef PROCESSREQUESTRECORD_CLASS #define PROCESSREQUESTRECORD_CLASS #include <iostream> #include <string> using namespace std; class procReqRec { public: // default constructor procReqRec() {} // constructor procReqRec(const string& nm, int p); // access functions int getPriority(); string getName(); // update functions void setPriority(int p); void setName(const string& nm); // for maintenance of a minimum priority queue friend bool operator< (const procReqRec& left, const procReqRec& right); // output a process request record in the format // name: priority friend ostream& operator<< (ostream& ostr, const procReqRec&...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &);...

    Finish the class Matrix.h: class Matrix { public: Matrix(); //default constructor ~Matrix(); //destructor Matrix(const Matrix &); //copy constror Matrix(int row, int col); Matrix operator+(const Matrix &)const; //overload operator“+” Matrix& operator=(const Matrix &); //overload operator“=” Matrix transpose()const; //matrix transposition void display()const; //display the data private: int row; //row int col; //column int** mat; //to save the matrix }; //main.cpp #include <iostream> #include"Matrix.h" using namespace std; int main() { int row, col; cout << "input the row and the col for Matrix...

  • Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std;...

    Question 1: Fix the 2D dynamic array initialization in following code #include <iostream> using namespace std; int main(){    int rows = 5; int cols = 5; int x;    int** arr = new int[rows][cols]    cin >> x; arr[x][x] = x; cout << "arr[x][x] = " << arr[x][x];    return 0; } Question 2: Fix the code to initialize the 2D array elements to x #include <iostream> using namespace std; int main(){    int rows; int cols; int x;...

  • Write loop(s) that print ALL the content of the following 2D array #include <iostream> using namespace...

    Write loop(s) that print ALL the content of the following 2D array #include <iostream> using namespace std; int main(){    int rows; int cols; int offset; cin >> rows; cin >> cols; cin >> offset;    int** arr = new int*[rows]; for(int i = 0; i < rows; ++i) arr[i] = new int[cols];    for(int i = 0; i < rows; i++) // initialization for(int j = 0; j < cols; j++) arr[i][j] = offset + i;    // printing, your code goes here   ...

  • This program illustrates dynamic variables #include <iostream> using namespace std; int main () {    int...

    This program illustrates dynamic variables #include <iostream> using namespace std; int main () {    int *p1;    p1 = new int;             // Variables created using the new operator are called dynamic variables    cout << "Enter an integer \n";    cin >> *p1;    *p1 = *p1 + 7;    cout << << "Your input + 7 = " << *p1 << endl;    delete p1;                // Delete the dynamic variable p1 and return the memory occupied by p1 to...

  • Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include...

    Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include <iostream> using namespace std; template <typename T> struct Int_Node {    T value;    Int_Node<T> *pre, *next; }; template <typename T> class Link_List {    template <typename U>    friend ostream &operator<<(ostream &, const Link_List<U> &);// print all integers in the list    template <typename U>    friend istream &operator>>(istream &, Link_List<U> &);// input a value at the back of the list, like insert_node(val);...

  • I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string>...

    I need help with those two functions c++ #ifndef TRIPLE_H #define TRIPLE_H #include <iostream> #include <string> using namespace std; class Triple { private: int a, b, c; public: Triple(); // all elements have value 0 Triple(int k); // all elements have value k Triple(int x, int y, int z); // specifies all three elements Triple(string s); // string representation is "(a,b,c)" string toString(); // create a string representation of the vector void fromString(string s); // change the vector to equal...

  • #ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> using namespace std; const double DEFAULT_SCORE = 0.0; class StudentTestScores...

    #ifndef STUDENTTESTSCORES_H #define STUDENTTESTSCORES_H #include <string> using namespace std; const double DEFAULT_SCORE = 0.0; class StudentTestScores { private: string studentName; // The student's name double *testScores; // Points to array of test scores int numTestScores; // Number of test scores // Private member function to create an // array of test scores. void createTestScoresArray(int size) { numTestScores = size; testScores = new double[size]; for (int i = 0; i < size; i++) testScores[i] = DEFAULT_SCORE; } public: // Constructor StudentTestScores(string...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT