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()...instea d 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..pl s 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...stil l i would like to learn a way if any... :)
4 1820 JosAH 11,448
Recognized Expert MVP
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...
JosAH 11,448
Recognized Expert MVP
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 up to which size m would you expect
vector<double> returns_by_value()
{
|
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 function below is returning a pointer to pointers who are
GUID. I am trying to write a wrapper to use in my VB code and what I
would prefer to do is be able to return an array of GUID. I remember
(not sure) that the concept of arrays does not really...
|
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 pointer?
Returning pointers is a no brainer, but I'm not sure how to return a
reference to a newly created object, in a method call. (Yes, I know
about RAII).
Pseudocode:
|
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 all, instead of the
object itself?
|
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:
================================================================================
/* A function that returns a pointer-of-arrays to the calling function. */
#include <stdio.h>
int *pfunc(void);
|
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 break?
Thanks
Josh
|
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 method.
QUOTE ENDS HERE
I have never heard anyone else say that it is a problem for a function
|
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 that
structure, is this valid?
As shown in the code below I am allocating the structure in the
function and then returning the structure. I know if the structure
contained only simple types (int, float) this will work without
problems as you...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |