473,466 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

class matrix

This program is compiled, but it is not wanted to start up. Why?
#include <iostream>
using namespace std;

class matrix2x2{
private:
double M[2][2];
public:
matrix2x2 (const double tab[][2]);
matrix2x2(double,double,double,double);
matrix2x2();
matrix2x2 operator + (matrix2x2);
matrix2x2 operator = (matrix2x2);
matrix2x2 operator - () {
cout <<"operator -" << endl;
M[0][0]=-M[0][0];
M[0][1]=-M[0][1];
M[1][0]=-M[1][0];
M[1][1]=-M[1][1];
return *this;
}

friend ostream& operator << (ostream os, const matrix2x2 X);
double det() {return M[0][0]*M[1][1]-M[1][0]*M[0][1];}
};

matrix2x2::matrix2x2 (const double tab[][2]){
int i,j;
for (i=1;i<2;i++)
for (j=0;j<2;j++)
M[i][j]=tab[i][j];
}

matrix2x2::matrix2x2(double a, double b, double c, double d) {
M[0][0]=a;
M[0][1]=b;
M[1][0]=c;
M[1][1]=d;
}

matrix2x2::matrix2x2(){
M[0][0]=0;
M[0][1]=0;
M[1][0]=0;
M[1][1]=0;
}

matrix2x2 matrix2x2::operator + (matrix2x2 X){
matrix2x2 T;
T.M[0][0]=M[0][0] + X.M[0][0];
T.M[0][1]=M[0][1] + X.M[0][1];
T.M[1][0]=M[1][0] + X.M[1][0];
T.M[1][1]=M[1][1] + X.M[1][1];
return T;
}

matrix2x2 matrix2x2::operator = (matrix2x2 X){
matrix2x2 T;
T.M[0][0]=X.M[0][0];
T.M[0][1]=X.M[0][1];
T.M[1][0]=X.M[1][0];
T.M[1][1]=X.M[1][1];
return T;
}

/*matrix2x2 matrix2x2::operator- (){
matrix2x2 T;
T.M[0][0]=-X.M[0][0];
T.M[0][1]=-X.M[0][1];
T.M[1][0]=-X.M[1][0];
T.M[1][1]=-X.M[1][1];
return T;
}
*/
//int main()
int main(int argc, char **argv)
{
const double tab[][2]= {{1.5, 2.5}, {3.5, 4.5}};

matrix2x2 A(tab), B(1.0, 0.0, 0.0, -1.0), C = A + B, D;

D = -C;

cout << "\nMacierz A: " << A
<< ", macierz B: " << B
<< ", macierz C: " << C
<< ", macierz D: " << D
<< ", wyznacznik macierzy A: " << A.det() //-2.0
<< ", wyznacznik macierzy B: " << B.det() //-1.0
<< ", wyznacznik macierzy C: " << C.det() // 0.0
<< ", wyznacznik macierzy D: " << D.det(); // 0.0

return 0;
}

May 21 '06 #1
12 2637
wo********@gmail.com wrote:
This program is compiled, but it is not wanted to start up. Why?


I don't know. You seem to have a very strange compiler. The code contains
quite a lot of errors that would prevent it from being accepted by any C++
compiler I know.

May 21 '06 #2
The code contains quite a lot of errors(...)

I use Microsoft Visual C++ and I see only two error:

a.obj : error LNK2001: unresolved external symbol "class
std::basic_ostream<char,struct std::char_traits<char> > & __cdecl
operator<<(class std::basic_ostream<char,struct std::char_traits<char>,class matrix2x2)" (??6@YAAAV?$basic_ostream@DU?$char_

traits@D@std@@@std@@V01@Vmatrix2x2@@@Z)
Debug/a.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

May 21 '06 #3
wo********@gmail.com wrote:
This program is compiled, but it is not wanted to start up. Why?
#include <iostream>
using namespace std;

class matrix2x2{
private:
double M[2][2];
public:
matrix2x2 (const double tab[][2]);
matrix2x2(double,double,double,double);
matrix2x2();
matrix2x2 operator + (matrix2x2);
matrix2x2 operator = (matrix2x2);
matrix2x2 operator - () {
cout <<"operator -" << endl;
M[0][0]=-M[0][0];
M[0][1]=-M[0][1];
M[1][0]=-M[1][0];
M[1][1]=-M[1][1];
return *this;
}

friend ostream& operator << (ostream os, const matrix2x2 X);
You need to define the above friend function somewhere.
double det() {return M[0][0]*M[1][1]-M[1][0]*M[0][1];}
};

matrix2x2::matrix2x2 (const double tab[][2]){
int i,j;
for (i=1;i<2;i++)
for (j=0;j<2;j++)
M[i][j]=tab[i][j];
}

matrix2x2::matrix2x2(double a, double b, double c, double d) {
M[0][0]=a;
M[0][1]=b;
M[1][0]=c;
M[1][1]=d;
}

matrix2x2::matrix2x2(){
M[0][0]=0;
M[0][1]=0;
M[1][0]=0;
M[1][1]=0;
}

matrix2x2 matrix2x2::operator + (matrix2x2 X){
matrix2x2 T;
T.M[0][0]=M[0][0] + X.M[0][0];
T.M[0][1]=M[0][1] + X.M[0][1];
T.M[1][0]=M[1][0] + X.M[1][0];
T.M[1][1]=M[1][1] + X.M[1][1];
return T;
}

matrix2x2 matrix2x2::operator = (matrix2x2 X){
matrix2x2 T;
T.M[0][0]=X.M[0][0];
T.M[0][1]=X.M[0][1];
T.M[1][0]=X.M[1][0];
T.M[1][1]=X.M[1][1];
return T;
}

/*matrix2x2 matrix2x2::operator- (){
matrix2x2 T;
T.M[0][0]=-X.M[0][0];
T.M[0][1]=-X.M[0][1];
T.M[1][0]=-X.M[1][0];
T.M[1][1]=-X.M[1][1];
return T;
}
*/
//int main()
int main(int argc, char **argv)
{
const double tab[][2]= {{1.5, 2.5}, {3.5, 4.5}};

matrix2x2 A(tab), B(1.0, 0.0, 0.0, -1.0), C = A + B, D;

D = -C;

cout << "\nMacierz A: " << A
<< ", macierz B: " << B
<< ", macierz C: " << C
<< ", macierz D: " << D
<< ", wyznacznik macierzy A: " << A.det() //-2.0
<< ", wyznacznik macierzy B: " << B.det() //-1.0
<< ", wyznacznik macierzy C: " << C.det() // 0.0
<< ", wyznacznik macierzy D: " << D.det(); // 0.0

return 0;
}

May 21 '06 #4
<wo********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com
The code contains quite a lot of errors(...)


I use Microsoft Visual C++ and I see only two error:

a.obj : error LNK2001: unresolved external symbol "class
std::basic_ostream<char,struct std::char_traits<char> > & __cdecl
operator<<(class std::basic_ostream<char,struct std::char_traits<char>
,class matrix2x2)" (??6@YAAAV?$basic_ostream@DU?$char_

traits@D@std@@@std@@V01@Vmatrix2x2@@@Z)
Debug/a.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.


I only see one, and one is all it takes.

I ran your code on three different versions of VC++ and got three different
sets of errors. Judging by your errors, you are using VC++ 6, which predates
the current standard and should not be used for anything except the
maintenance of old applications.

You can find a free version of Microsoft's current compiler here:

http://msdn.microsoft.com/vstudio/express/visualc/

In any event, the problem with your code is that you have declared the
friend operator << but not defined it. You also should change the signature
of that friend to:

friend ostream &operator << (ostream & os, const matrix2x2 & X);

i.e., make both arguments references along with the return type (the
reference for the ostream argument is essential; the reference for the
matrix2x2 is merely preferable).

--
John Carson
May 21 '06 #5
> You need to define the above friend function somewhere.

friend ostream& operator << (ostream os, const matrix2x2 X);
???????????????

May 21 '06 #6
<wo********@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com
You need to define the above friend function somewhere.


friend ostream& operator << (ostream os, const matrix2x2 X);
???????????????


That declares it, it doesn't define it. You have to give the operator a body
where it does something, e.g.,

ostream & operator<<(ostream & os, const matrix2x2 X)
{
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
cout << X.M[i][j] << ' ';
cout << '\n';
}
return os;
}
--
John Carson
May 21 '06 #7
ostream & operator<<(ostream & os, const matrix2x2 X)
{
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
cout << X.M[i][j] << ' ';
cout << '\n';
}
return os;

}
where it paste? It doesn't act.

May 21 '06 #8
<wo********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com
ostream & operator<<(ostream & os, const matrix2x2 X)
{
for (int i=0;i<2;i++)
{
for (int j=0;j<2;j++)
cout << X.M[i][j] << ' ';
cout << '\n';
}
return os;

}
where it paste? It doesn't act.


If you really need to ask these sorts of questions, then you need to be
working your way through a C++ textbook. You aren't going to be able to
learn this stuff through newsgroup questions.

You put it after the declaration of the class, e.g.,

class matrix2x2{
private:
double M[2][2];
public:
matrix2x2 (const double tab[][2]);
matrix2x2(double,double,double,double);
matrix2x2();
matrix2x2 operator + (matrix2x2);
matrix2x2 operator = (matrix2x2);
matrix2x2 operator-()
{
cout <<"operator -" << endl;
M[0][0]=-M[0][0];
M[0][1]=-M[0][1];
M[1][0]=-M[1][0];
M[1][1]=-M[1][1];
return *this;
}
friend ostream &operator << (ostream & os, const matrix2x2 &X);
double det() {return M[0][0]*M[1][1]-M[1][0]*M[0][1];}
};
ostream &operator<<(ostream & os, const matrix2x2 & X)
{
int i,j;
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
cout << X.M[i][j] << ' ';
cout << '\n';
}
return os;
}
--
John Carson
May 21 '06 #9
errors:

c:\documents and settings\ag\c.cpp(33) : error C2248: 'M' : cannot
access private member declared in class 'matrix2x2'
c:\documents and settings\ag\c.cpp(6) : see declaration of 'M'
c:\documents and settings\ag\c.cpp(105) : error C2593: 'operator <<' is
ambiguous
Error executing cl.exe.

May 21 '06 #10
wo********@gmail.com wrote:
This program is compiled, but it is not wanted to start up. Why?
#include <iostream>
using namespace std;

class matrix2x2{
If your compiler complains about access to 'M' (mine does not),
then change the next line to "protected:".
private:
double M[2][2];
public:
matrix2x2 (const double tab[][2]);
matrix2x2(double,double,double,double);
matrix2x2();
matrix2x2 operator + (matrix2x2);
matrix2x2 operator = (matrix2x2);
matrix2x2 operator - () {
cout <<"operator -" << endl;
M[0][0]=-M[0][0];
M[0][1]=-M[0][1];
M[1][0]=-M[1][0];
M[1][1]=-M[1][1];
return *this;
}

Use references for 'os' and 'X' in the 'friend' declaration,
like this:

friend ostream& operator << (ostream& os, const matrix2x2& X);
friend ostream& operator << (ostream os, const matrix2x2 X);
double det() {return M[0][0]*M[1][1]-M[1][0]*M[0][1];}
};

Then add the function body for the 'ostream friend' (using
John Carson's code as an example):

ostream& operator<<(ostream& os, const matrix2x2& X)
{
int i,j;
for (i=0;i<2;i++)
{
for (j=0;j<2;j++)
cout << X.M[i][j] << ' ';
cout << '\n';
}
return os;
}

matrix2x2::matrix2x2 (const double tab[][2]){
int i,j;
for (i=1;i<2;i++)
for (j=0;j<2;j++)
M[i][j]=tab[i][j];
}

matrix2x2::matrix2x2(double a, double b, double c, double d) {
M[0][0]=a;
M[0][1]=b;
M[1][0]=c;
M[1][1]=d;
}

matrix2x2::matrix2x2(){
M[0][0]=0;
M[0][1]=0;
M[1][0]=0;
M[1][1]=0;
}

matrix2x2 matrix2x2::operator + (matrix2x2 X){
matrix2x2 T;
T.M[0][0]=M[0][0] + X.M[0][0];
T.M[0][1]=M[0][1] + X.M[0][1];
T.M[1][0]=M[1][0] + X.M[1][0];
T.M[1][1]=M[1][1] + X.M[1][1];
return T;
}

matrix2x2 matrix2x2::operator = (matrix2x2 X){
matrix2x2 T;
T.M[0][0]=X.M[0][0];
T.M[0][1]=X.M[0][1];
T.M[1][0]=X.M[1][0];
T.M[1][1]=X.M[1][1];
return T;
}

/*matrix2x2 matrix2x2::operator- (){
matrix2x2 T;
T.M[0][0]=-X.M[0][0];
T.M[0][1]=-X.M[0][1];
T.M[1][0]=-X.M[1][0];
T.M[1][1]=-X.M[1][1];
return T;
}
*/
//int main()
int main(int argc, char **argv)
{
const double tab[][2]= {{1.5, 2.5}, {3.5, 4.5}};

matrix2x2 A(tab), B(1.0, 0.0, 0.0, -1.0), C = A + B, D;

D = -C;

cout << "\nMacierz A: " << A
<< ", macierz B: " << B
<< ", macierz C: " << C
<< ", macierz D: " << D
<< ", wyznacznik macierzy A: " << A.det() //-2.0
<< ", wyznacznik macierzy B: " << B.det() //-1.0
<< ", wyznacznik macierzy C: " << C.det() // 0.0
<< ", wyznacznik macierzy D: " << D.det(); // 0.0

return 0;
}


Regards,
Larry
May 21 '06 #11
wo********@gmail.com wrote:
errors:

c:\documents and settings\ag\c.cpp(33) : error C2248: 'M' : cannot
access private member declared in class 'matrix2x2'


Make it a friend inside the class or just put the definition of
"operator <<" inside the class. Like:

friend ostream &operator << (ostream & os, const matrix2x2 & X)
Best regards / Med venlig hilsen
Martin Jørgensen

--
---------------------------------------------------------------------------
Home of Martin Jørgensen - http://www.martinjoergensen.dk
May 21 '06 #12
<wo********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com
errors:

c:\documents and settings\ag\c.cpp(33) : error C2248: 'M' : cannot
access private member declared in class 'matrix2x2'
c:\documents and settings\ag\c.cpp(6) : see declaration of 'M'
c:\documents and settings\ag\c.cpp(105) : error C2593: 'operator <<'
is ambiguous
Error executing cl.exe.

I'm guessing that your friend declaration and operator definitions don't
exactly match (probably in terms of whether pass by value or pass by
reference are used). If you copy and paste the code I supplied (for both the
class declaration and the operator definition), then it will work.
--
John Carson
May 22 '06 #13

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

Similar topics

8
by: Andrew Morgan | last post by:
Hi I have a matrix class: template<class Type> class TMatrix { //... TMatrix& T(); //... }
4
by: Zenon | last post by:
Folks, I have been trying for a week but I cannot debug the following error: Error E2285 ex5.cpp 141: Could not find a match for 'matrix<complex<double>>::operator =(complex<double>)' in...
13
by: Charulatha Kalluri | last post by:
Hi, I'm implementing a Matrix class, as part of a project. This is the interface I've designed: class Matrix( )
7
by: check.checkta | last post by:
Hi, I'd like to implement a simple matrix class. I'd like to overload operator so that it returns as a vector (either the stl vector or some other Vector class of my own). The reason I want...
7
by: woessner | last post by:
Hi all, I whipped up a quick class to represent a matrix for use with LAPACK. It's a template class so it can support the 4 data types supported by LAPACK (single/double x complex/real). I...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
6
by: Mark | last post by:
I have a problem with a template class defined: // start matrix.h file template <class Tclass Matrix { public: Matrix() { // default constructor } Matrix(const Subscript rows, const...
2
by: DarrenWeber | last post by:
Below is a module (matrix.py) with a class to implement some basic matrix operations on a 2D list. Some things puzzle me about the best way to do this (please don't refer to scipy, numpy and...
1
by: madshov | last post by:
Hi, I have made a matrix class, that I wish to make an instiation of in another class. Matrix class: #include <vector> #include <iostream> using namespace std; // So the program can see cout...
2
by: rijaalu | last post by:
I am designing a matrix class that performs addition, multicpication, substraction and division. When ever i complie the code it shows an error. include <iostream> using namespace std; class...
0
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...
0
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,...
0
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...
1
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...
0
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...
0
agi2029
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,...
0
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...
0
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...
0
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 ...

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.