473,756 Members | 3,482 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

i write code but dont understand the error please help me

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;
public :
Matrix(){
cout<<"paratmer ized constructor called";
}
Matrix( int rows , int cols ) ;
void getMatrix ( ) ;
void displayMatrix ( ) ;
Matrix :: Matrix operator+();

};
Matrix :: Matrix ( int rows = 0 , int cols = 0)
{
numCols = cols ;
numRows = rows ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
elements [ i ] [ j ] = 0 ;
}
}
}
void Matrix :: getMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
cin >elements [ i ] [ j ] ;
//cout<<"Enter the 1st Matrix";
}

}
//cout<<"Enter the 2nd Matrix"<<endl;
}
void Matrix :: displayMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
cout << "| " ;
for ( int j = 0 ; j < numCols ; j ++ )
{
cout << elements [ i ] [ j ] << " " ;
}
cout << "|" << endl ;
}

cout<<'\n';
cout<<'\n';
}

Matrix :: Matrix operator + (Matrix add)
{
Matrix temp;
temp.numCols=nu mCols + add.numCols;
temp.numRows=nu mRows + add.numRows;
return temp;
}

void main ( )
{
Matrix matrix1(2, 2),matrix2(2,2) ;
matrix1.getMatr ix ( ) ;
matrix2.getMatr ix();
//Matrix = matrix1 + matrix2 ;
matrix1.display Matrix ( ) ;
matrix2.display Matrix ( ) ;
//
system ( "PAUSE" ) ;
getch();
}

Jun 23 '07 #1
8 2339
On Jun 22, 7:59 pm, mohammaditrad.. .@gmail.com wrote:
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;

public :
Matrix(){
cout<<"paratmer ized constructor called";}

Matrix( int rows , int cols ) ;
void getMatrix ( ) ;
void displayMatrix ( ) ;
Matrix :: Matrix operator+();

};

Matrix :: Matrix ( int rows = 0 , int cols = 0)
{
numCols = cols ;
numRows = rows ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
elements [ i ] [ j ] = 0 ;}
}
}

void Matrix :: getMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
cin >elements [ i ] [ j ] ;
//cout<<"Enter the 1st Matrix";

}
}

//cout<<"Enter the 2nd Matrix"<<endl;}

void Matrix :: displayMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )

{
cout << "| " ;
for ( int j = 0 ; j < numCols ; j ++ )
{
cout << elements [ i ] [ j ] << " " ;}

cout << "|" << endl ;

}

cout<<'\n';
cout<<'\n';

}

Matrix :: Matrix operator + (Matrix add)
{
Matrix temp;
temp.numCols=nu mCols + add.numCols;
temp.numRows=nu mRows + add.numRows;
return temp;

}

void main ( )
{
Matrix matrix1(2, 2),matrix2(2,2) ;
matrix1.getMatr ix ( ) ;
matrix2.getMatr ix();
//Matrix = matrix1 + matrix2 ;
matrix1.display Matrix ( ) ;
matrix2.display Matrix ( ) ;
//

system ( "PAUSE" ) ;
getch();

}- Hide quoted text -

- Show quoted text -

compile time error? run-time error? what is the program supposed to
do? what am i supposed to be looking for?

Jun 23 '07 #2
Zachary Turner wrote:

[snip scores of lines of code]
>

compile time error? run-time error? what is the program supposed to
do? what am i supposed to be looking for?
Looks like a trim-time error to me!

--
Ian Collins.
Jun 23 '07 #3

<mo************ **@gmail.comwro te in message...

My suggestions inline below:

// #include <iostream.h>
// #include <stdlib.h>
// #include <conio.h>
// #include <string.h>

#include <iostream>
#include <cstdlib>
#include <string>
class Matrix{ private :
int numRows, numCols ;
int elements [30] [30] ;
public :
Matrix(){
// cout<<"paratmer ized constructor called";
std::cout<<"par atmerized constructor called";
}
Matrix( int rows , int cols ) ;
void getMatrix ( ) ;
void displayMatrix ( ) ;
Matrix :: Matrix operator+();
};
// Matrix :: Matrix ( int rows = 0 , int cols = 0){
// numCols = cols ; // numRows = rows ;

Matrix :: Matrix ( int rows = 0 , int cols = 0) : // note that colon
numRows( rows ), numCols( cols ){

for ( int i = 0 ; i < numRows ; ++i ){
for ( int j = 0 ; j < numCols ; ++j ){
elements [ i ] [ j ] = 0 ;
}
}
}
void Matrix :: getMatrix ( ){
for ( int i = 0 ; i < numRows ; ++i ){
for ( int j = 0 ; j < numCols ; ++j ){
// cin >elements [ i ] [ j ] ;
std::cin >elements [ i ] [ j ] ;
file://cout<<"Enter the 1st Matrix";
}
}
file://cout<<"Enter the 2nd Matrix"<<endl;
}
void Matrix :: displayMatrix ( ){
for ( int i = 0 ; i < numRows ; ++i ){
// cout << "| " ;
std::cout << "| " ;
for ( int j = 0 ; j < numCols ; ++j ){
// cout << elements [ i ] [ j ] << " " ;
std::cout << elements [ i ] [ j ] << " " ;
}
// cout << "|" << endl ;
std::cout << "|" << endl ;
}
// cout<<'\n'; // cout<<'\n';
std::cout<<"\n" <<std::endl;
}

Matrix :: Matrix operator + (Matrix add){
Matrix temp;
temp.numCols=nu mCols + add.numCols;
temp.numRows=nu mRows + add.numRows;
return temp;
}

// void main ( )
int main ( ){
Matrix matrix1(2, 2), matrix2(2,2) ;
matrix1.getMatr ix ( ) ;
matrix2.getMatr ix();
file://Matrix = matrix1 + matrix2 ;
matrix1.display Matrix ( ) ;
matrix2.display Matrix ( ) ;
// system ( "PAUSE" ) ;
// getch();

std::string dummy;
std::cin >dummy;
}
Your main problems were:
1] wrong headers
2] unqualified 'std::'
3] main() returns type 'int', **always**!!
4] it's ++x or x++ ( NO space between them)

Try the corrections above, and post again if you have problems. But, post
what you expect the program to do, what you get, and the first 3 errors you
get if it doesn't compile.

--
Bob R
POVrookie
Jun 23 '07 #4

BobR wrote in message...
>
Your main problems were:
4] it's ++x or x++ ( NO space between them)
Oops. 4 does not exist. Forget I said that.

--
Bob R
POVrookie
Jun 23 '07 #5
You've made a few mistakes but they are just the usual beginner
mistakes, see below for some corrections.

You should try to write less code at once. When you write too much code
you just get errors on top of errors and it gets really confusing (you
know this). When you write code, just write a few more lines then try to
compile and run. Don't try to write thirty lines of code at once until
you get to be an expert. Don't try and write more code, when you don't
understand what's wrong with the code you've written. This is really
important advice, more important than anything else I tell you below.

mo************* *@gmail.com wrote:
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
Replace the above with

#include <iostream>
#include <conio.h>
using namespace std;

There is no header file called <iostream.hin C++. Anyone who tells you
different is wrong. *Some* compilers have it but some don't. When I
compile your code with my compielr it says

Cannot open include file: 'iostream.h': No such file or directory

which is right. The correct name for this header file is <iostream>
without the '.h'. There are header file called <stdlib.hand <string.h>
but you are not using them so you might as well get rid of them.

Finally you need 'using namespace std;' Look up namespaces in a C++
book, I can't be bothered to explain this for the umpteenth time.
class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;
public :
Matrix(){
cout<<"paratmer ized constructor called";
}
Get rid of the above constructor, add default parameters to the next
constructor (see below). The above constructor makes no sense because it
doesn't initialise anything.
Matrix( int rows , int cols ) ;
Matrix(int rows = 0, int cols = 0);
void getMatrix ( ) ;
void displayMatrix ( ) ;
Matrix :: Matrix operator+();
This is wrong, simple syntax error

Matrix operator+(Matri x add);
>
};
Matrix :: Matrix ( int rows = 0 , int cols = 0)
This is wrong

Matrix :: Matrix ( int rows , int cols)

If you want default parameters put them in the class, not in the definition.
{
numCols = cols ;
numRows = rows ;
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
elements [ i ] [ j ] = 0 ;
}
}
}
void Matrix :: getMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
for ( int j = 0 ; j < numCols ; j ++ )
{
cin >elements [ i ] [ j ] ;
//cout<<"Enter the 1st Matrix";
}

}
//cout<<"Enter the 2nd Matrix"<<endl;
}
void Matrix :: displayMatrix ( )
{
for ( int i = 0 ; i < numRows ; i ++ )
{
cout << "| " ;
for ( int j = 0 ; j < numCols ; j ++ )
{
cout << elements [ i ] [ j ] << " " ;
}
cout << "|" << endl ;
}

cout<<'\n';
cout<<'\n';
}

Matrix :: Matrix operator + (Matrix add)
Matrix Matrix :: operator + (Matrix add)

Again, simple syntax mistake.
{
Matrix temp;
temp.numCols=nu mCols + add.numCols;
temp.numRows=nu mRows + add.numRows;
The above is not bad C++, but is makes no sense mathematically. Do you
know how to add two matrixes?
return temp;
}

void main ( )
int main()

Main always returns an int, that's a rule of C++. A stupid rules, but
still a rule.
{
Matrix matrix1(2, 2),matrix2(2,2) ;
matrix1.getMatr ix ( ) ;
matrix2.getMatr ix();
//Matrix = matrix1 + matrix2 ;
matrix1.display Matrix ( ) ;
matrix2.display Matrix ( ) ;
//
system ( "PAUSE" ) ;
getch();
}
Hope this helps.

john
Jun 23 '07 #6
On 23 Jun, 06:10, John Harrison <john_androni.. .@hotmail.comwr ote:
You've made a few mistakes but they are just the usual beginner
mistakes, see below for some corrections.

You should try to write less code at once. When you write too much code
you just get errors on top of errors and it gets really confusing (you
know this). When you write code, just write a few more lines then try to
compile and run. Don't try to write thirty lines of code at once until
you get to be an expert. Don't try and write more code, when you don't
understand what's wrong with the code you've written. This is really
important advice, more important than anything else I tell you below.
To the OP: John is absolutely right here. More than anything, this is
your main problem.

<snip>
class Matrix
{
private :
int numRows, numCols ;
int elements [30] [30] ;
public :
Matrix(){
cout<<"paratmer ized constructor called";
}

Get rid of the above constructor, add default parameters to the next
constructor (see below). The above constructor makes no sense because it
doesn't initialise anything.
Matrix( int rows , int cols ) ;

Matrix(int rows = 0, int cols = 0);
That's not the right solution either. Originally the OP had
effectively this:

class Matrix
{
public:
Matrix() {}
Matrix(int rows = 0, int cols = 0) {}
};

int main()
{
Matrix m1(2, 2);
Matrix m2(2, 2);
}

I've left the constructor bodies empty becuase they aren't needed for
my example. That's definitely wrong because if you add

Matrix m3;

in main the code won't even compile because both the OP's constructors
can be called with zero arguments and so the compiler doesn't know
which one to call.

Your suggestion is to remove the one of the constructors:

class Matrix
{
public:
Matrix(int rows = 0, int cols = 0) {}
};

int main()
{
Matrix m1(2, 2);
Matrix m2(2, 2);
Matrix m3;
}

Now the above all compiles. No ambiguity. The statement Matrix m3;
calls the constructor with rows equal to 0 and cols equal to 0.
However, with default parameters, there are now *three* constructors
in class Matrix. The two argument constructor (used to construct m1
and m2 above), the zero argument constructor (used to construct m3
above) and a single argument constructor:

int main()
{
Matrix m4(2);
}

m4 is constructed with rows equal to 2 and cols equal to 0. I'd be
surprised if the OP needs this.

Worse, because the constructor is not explicit, the following compiles
as well:

int main()
{
Matrix m5(2, 2);
int foo = 42;

// ... lots of intervening code

m5 = foo; // Do you REALLY want to be able to do this?
}

After the statement m = foo; whatever was in m has gone and been
replaced with a brand new Matrix with 42 rows and no columns.

The OP needs to ditch the default parameters completely and go with
two constructors (I've only worried about the numRows and numCols
members for brevity):

class Matrix
{
public:
Matrix();
Matrix(int rows, int cols);
private:
int numRows;
int numCols;
};

Matrix::Matrix( ) : numRows(0), numCols() {}

Matrix::Matrix( int rows, int cols) : numRows(rows), numCols(cols) {}

int main()
{
Matrix m1(2, 2); // OK
Matrix m2(2, 2); // OK
Matrix m3; // OK - OP's code couldn't do this but it looked like
they wanted to be able to.
Matrix m4(2); // Won't compile - your code allowed this.

Matrix m5(2, 2);
int foo = 42;
m5 = foo; // Won't compile - your code allowed this.
}

Gavin Deane

Jun 23 '07 #7
John Harrison wrote:
You've made a few mistakes but they are just the usual beginner
mistakes, see below for some corrections.
mo************* *@gmail.com wrote:
#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

Replace the above with

#include <iostream>
#include <conio.h>
using namespace std;

There is no header file called <iostream.hin C++.
But you think there is one called <conio.h>?


Brian
Jun 23 '07 #8
On 23 Jun, 09:40, Gavin Deane <deane_ga...@ho tmail.comwrote:
Matrix::Matrix( ) : numRows(0), numCols() {}
Oops. Should be:

Matrix::Matrix( ) : numRows(0), numCols(0) {}
^

Gavin Deane

Jun 23 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5858
by: Dave | last post by:
I have a MS sql 2000 db that needs to sync with a Mysql db. When a password is changed in the MS SQL table, I need to make sure that the same value is updated in the Mysql db. I dont have any control of the gui that is initiating this event. So, I was thinking a trigger might be an alternate route. I just dont know how to get the username and password values that have just been updated from MS sql to Mysql.
0
1762
by: hari krishna | last post by:
hi all, My requirement is to generate xl reports throu Asp.Net without installing xl on web server computer. i am using Response object and wrtifile method as below. i dont know whether it is correct, but giving error says " file is accessed by other process cannot access" I have some logic inbetween and write the info using response.write in to html format Ex: Response.Write("<td>" & "client_no" & "</td>"). I am getting the error at :...
9
6339
by: msuk | last post by:
All, I have a well form block of XML that is stored in a C# string type and I just simply want to display it in the browser using Response.Write but when I try this I get the following error: The XML page cannot be displayed Cannot view XML input using XSL style sheet. Please correct the error and then click the Refresh button, or try again later.
22
1599
by: EMW | last post by:
Hi, When I use Response.Write "something" it is placed at the top op de html document above the <HTML> tag. I need it to be put in the BODY part. How can I do this? rg,
3
1924
by: Matthew Warren | last post by:
I have the following piece of code, taken from a bigger module, that even as I was writing I _knew_ there were better ways of doing it, using a parser or somesuch at least, but learning how wasn't as fun as coding it... And yes alarm bells went off when I found myself typing eval(), and I'm sure this is an 'unusual' use of for: else: . And I know it's not very robust. As an ex cuse/planation, this is what happens when you get an idea in...
10
2153
by: cjard | last post by:
I have a client and server that enjoy the following simple dialogue: Client connects Client sends request Server sends response Client disconnects This is the way it must be. The response must be wrapped in a start and end byte 0x02 and 0x03 resepctively.
6
5244
by: aagarwal8 | last post by:
Hi, I am trying to write the contents of a textbox to a file in binary format. My code looks like this... private void btnWriteToFile_Click(object sender, EventArgs e) { FileStream fs = File.Open(@"D:\test.dat", FileMode.OpenOrCreate, FileAccess.Write); BinaryWriter bw = new BinaryWriter(fs);
2
1243
by: phredator | last post by:
Hello i have a sql question, it looks like this. sQuery = "SELECT COUNT(ID) as CNT, datepart(hh,TimeStamp) FROM CS WHERE ID=? GROUP BY datepart(hh,TimeStamp)" ...... While oReader.Read() strXML = strXML & "<set name='" & oReader.GetInt32(1) & "' value='" & oReader.GetInt32(0) & "' />"
4
4435
by: Abubakar | last post by:
Hi, I'm working on an application whose code was written by some other developer and I dont completely understand its source code right now. In one of the pages where I'm writing a test code, at the end of the Page_Load event I write a simple following line: if (IsPostBack) { Response.Write("say"); }
0
9462
marktang
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...
0
9287
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,...
0
10046
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9722
tracyyun
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...
0
6542
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();...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3369
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.