i hav coded a matrix class and its member functions (many of them are overloaded ones)...now in a function..for eg. function of matrix addition.. after the addition i want to return the local object "result" through a pointer (and not call by value)..however i aint getting d values of the result in main()...instead i am getting garbage values when i print it...
here is my code : - #include <iostream.h>
-
#include <stdio.h>
-
#include <conio.h>
-
#include <stdlib.h>
-
-
// general function declarations
-
-
void list (int *choice);
-
-
-
// class declaration
- class matrix
-
{
-
private :
-
int rows;
-
int columns;
-
int * element;
-
-
public :
-
matrix();
-
matrix (int, int);
-
void setdata (int, int, int *);
-
void showptrdata();
-
matrix* operator+(matrix &);
-
-
friend istream& operator >> (istream &, matrix &);
-
friend ostream& operator << (ostream &, matrix &);
-
// function overloading -- for printing a pointer object
-
friend ostream& operator << (ostream &, matrix *&);
-
-
};
-
-
-
matrix :: matrix ()
-
{
-
rows = 0;
-
columns = 0;
-
*element = 0;
-
}
-
-
matrix :: matrix (int r ,int c)
-
{
-
rows = r;
-
columns = c;
-
element = new int [rows * columns];
-
}
-
-
istream& operator >> (istream &read, matrix &mat)
-
{
-
for (int i = 0; i < mat.rows * mat.columns; ++i)
-
read >> *(mat.element+i);
-
return read;
-
}
-
- ostream& operator << (ostream &print, matrix &mat)
-
{
-
for (int i = 0; i < mat.rows; ++i)
-
{
-
for (int j = 0; j < mat.columns; ++j)
-
{
-
print << *mat.element << " ";
-
++mat.element;
-
}
-
print << endl;
-
}
-
mat.element -= mat.rows * mat.columns;
-
return print;
-
}
-
-
ostream& operator << (ostream &print, matrix *&mat)
-
{
-
-
for (int i = 0; i < mat->rows; ++i)
-
{
-
for (int j = 0; j < mat->columns; ++j)
-
{
-
print << *mat->element << " ";
-
++mat->element;
-
}
-
print << endl;
-
}
-
mat->element -= mat->rows * mat->columns;
-
return print;
-
}
-
-
- matrix* matrix :: operator + (matrix &matrix2)
-
{
-
matrix result (rows, columns);
-
for (int i=0; i< rows * columns; ++i)
-
*(result.element+i) = *(element+i) + *(matrix2.element+i);
-
//printf ("%d %d\n", result.rows, result.columns);
-
//printf ("%u %u %u \n", &result, &result.rows, &result.columns);
-
return &result;
-
}
-
-
-
void main()
-
{
-
clrscr();
-
-
int r, c;
-
int choice;
-
-
cout << "Enter the rows of the matrix : ";
-
cin >> r;
-
cout << "Enter the columns of the matrix : ";
-
cin >> c;
-
-
matrix matrix1 (r, c);
-
-
cout << "Enter the elements rowwise : " << endl;
-
cin >> matrix1;
-
-
-
cout << "You have created the following matrix : " << endl;
-
cout << matrix1;
-
cin.ignore();
-
cin.get();
-
-
do
-
{
-
-
list (&choice);
-
-
switch (choice)
-
{
-
case 1 :
-
cout << endl << endl << "The matrix is as follows : " << endl;
-
cout << matrix1;
-
break;
-
-
case 2 :
-
cout << "Enter the new values of the matrix : " << endl;
-
cin >> matrix1;
-
cout << "The modified matrix is : " << endl;
-
cout << matrix1;
-
-
break;
-
-
case 3 :
- matrix matrix2(r,c);
-
cout << "Enter the elements of the second matrix : " << endl;
-
cin >> matrix2;
-
matrix *result;
-
result = matrix1 + matrix2;
-
//printf ("%u\n", result);
-
//printf ("%u %u\n", &result->rows, &result->columns);
-
//printf ("%d %d\n", result->rows, result->columns);
-
cout << "The resultant matrix is : " << endl;
-
cout << result;
-
break;
-
-
case 4 :
-
break;
-
-
case 5 :
-
break;
-
-
case 6 :
-
break;
-
-
case 0 :
-
exit(0);
-
}
-
cin.ignore();
-
cin.get();
-
} while (choice != 0);
-
-
}
-
-
void list (int *choice)
-
{
-
clrscr();
-
cout << "1. Display the matrix." << endl << "2. Modify the matrix." << endl
-
<< "3. Matrix Addition." << endl << "4. Matrix subtraction." << endl
-
<< "5. Matrix multiplication." << endl
-
<< "6. Transpose of matrix." << endl << "0.Exit." << endl;
-
-
cin >> *choice;
-
cin.ignore();
-
if (!(*choice >= 0 && *choice <= 6))
-
{
-
cout << "Invalid choice." << endl;
-
cin.get();
-
list (choice);
-
}
-
}
i have given the bold effect to the code that i felt is related to my problem..so..pls help me with this problem..
and yes..one more question.. is there any way to return this "result" by a reference ??? i know it may seem absurd...bcoz of the scope of the local variable...still i would like to learn a way if any... :)
4 1803
You're returning the address (a pointer to) a local variable. When the function
terminates the memory taken by the local variable (which was stored on the
stack) is lost for the posterity. Don't use locals there, 'new' your new matrix.
kind regards,
Jos
well...if u watch the code carefully...i have used new to allocate memory for that particular variable "result"...i have done that in the copy constructor which i have used while declaring "result" :
matrix result (rows, columns);
still i am facing the problem...
however i think that even allocating might not be helping "result" because after all..its scope is local to the function..hence it might be getting destroyed..
nevertheless any new suggestions are welcome...
well...if u watch the code carefully...i have used new to allocate memory for that particular variable "result"...i have done that in the copy constructor which i have used while declaring "result" :
matrix result (rows, columns);
still i am facing the problem...
however i think that even allocating might not be helping "result" because after all..its scope is local to the function..hence it might be getting destroyed..
nevertheless any new suggestions are welcome...
I did watch your code carefully and all you allocate is memory for the data
elements; the rest of the matrix data (the variables rows and columns and the
pointer to the elements) is still lost after the function terminates.
There's no need for new suggestions, first you have to fix this bug.
kind regards,
Jos
You've got the right idea, but your code doesn't even come close to compiling. Start simple and go from there. A good approach is to get one aspect of your code working and then move on to the next thing, don't do it all at once. This should get you started: - #include <iostream>
-
-
// general function declarations
-
// class declaration
-
class matrix
-
{
-
private :
-
int rows;
-
int columns;
-
int * element;
-
-
public :
-
matrix();
-
matrix (int, int);
-
-
void set_data(int row, int column, int data){
-
*(element + (row * columns + column)) = data;
-
}
-
-
matrix operator+=(const matrix &);
-
-
friend std::istream& operator >> (std::istream &, matrix &);
-
friend std::ostream& operator << (std::ostream &, matrix &);
-
};
-
-
-
matrix::matrix() : rows(0), columns(0), element(0) {}
-
-
matrix :: matrix (int r ,int c) : rows(r), columns(c), element( new int [rows * columns] ){ }
-
-
std::istream& operator >> (std::istream &read, matrix &mat)
-
{
-
for (int i = 0; i < mat.rows * mat.columns; ++i)
-
read >> *(mat.element+i);
-
return read;
-
}
-
-
std::ostream& operator << (std::ostream &print, matrix &mat)
-
{
-
for (int i = 0; i < mat.rows; ++i)
-
{
-
for (int j = 0; j < mat.columns; ++j)
-
{
-
print << *mat.element << " ";
-
++mat.element;
-
}
-
print << std::endl;
-
}
-
mat.element -= mat.rows * mat.columns;
-
return print;
-
}
-
-
-
matrix matrix :: operator+= (const matrix& that)
-
{
-
-
for (int i=0; i != rows * columns; ++i)
-
*(element+i) += *(that.element+i);
-
return matrix( *this );
-
}
-
-
int main()
-
{
-
matrix m(2,2), n(2,2);
-
m.set_data(0,0,1);
-
m.set_data(1,0,2);
-
m.set_data(0,1,3);
-
m.set_data(1,1,4);
-
-
n += m;
-
n += m;
-
-
std::cout << m;
-
std::cout << n;
-
-
matrix p(n);
-
std::cout << p;
-
-
p.set_data(0,0,0);
-
std::cout << n;
-
-
return 0;
-
}
You need to deal with copy construction and assignment (oh and destruction to clean up that pointer), because at the moment the pointer is copied, not the underlying data.
Hope that helps.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: mjm |
last post by:
Folks,
Stroustrup indicates that returning by value can be faster than
returning by reference but gives no details as to the size of the
returned object up to which this holds.
My question is...
|
by: Gent |
last post by:
I have two questions which are very similar:
Is it possible to return an object in C++. Below is part of my code
for reference however I am more concerned about the concept. It seems
like the...
|
by: Alfonso Morra |
last post by:
I'm in the process of implementing an abstract factory design pattern
for an application. I would like to know, which is the recommended way
of returning objects from the factory - by reference or...
|
by: Protoman |
last post by:
When would you return a const reference/pointer from a function instead
of either the object itself, or a non constreference/pointer? And when
would you need to return a reference or pointer at...
|
by: Dan |
last post by:
I have class B and C which inherit from class A.
I have a static method:
A* aRequest(unsigned char *byte_buffer, size_t length)
{
A *foo;
if(something == true)
{
foo = new B;
|
by: I.M. !Knuth |
last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until
I started goofing around with this:
...
|
by: student1976 |
last post by:
All
Beginner/Intermediate level question. I understand that returning
ptr to local stack vars is bad.
Is returning foo_p_B from fnB() reliable all the time, so that using
foo_p_A does not...
|
by: pauldepstein |
last post by:
Below is posted from a link for Stanford students in computer
science.
QUOTE BEGINS HERE
Because of the risk of misuse, some experts recommend never returning
a
reference from a function or...
|
by: darren |
last post by:
Hi everybody, have a quick look at this code:
=====
=====
int main(void) {
string msg;
makeString(msg);
cout << "back in main, result = " << msg << endl;
|
by: ctj951 |
last post by:
I have a very specific question about a language issue that I was
hoping to get an answer to. If you allocate a structure that
contains
an array as a local variable inside a function and return...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |