PDA

View Full Version : C++ 1 Error (no one can find it !! )


codeffect
11-13-2007, 06:32 PM
#include<iostream>
using namespace std;


class vect
{
float v[3];

public:
vect()
{
for(int i=0;i<3;i++)
v[i]=0;
}

vect(float t[])
{
for(int i=0;i<3;i++)
v[i]=t[i];
}

void affiche()
{
for(int i=0;i<3;i++)
cout<<v[i]<<" ";
cout<<endl;
}

friend vect mat::pmv(vect);
}; //fin class vect


class mat
{
float m[3][3];

public :
mat(){
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
m[i][j]=0;
}

mat(float t[3][3]){
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
m[i][j]=t[i][j];
}

void affiche()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<m[i][j]<<" ";
cout<<endl;

}
}

vect pmv(vect p)
{
vect res;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
res.v[i]+=(m[i][j])*(p.v[j]);
return res;
}

};//end class Mat

void main()
{
float t[]={1,2,3};
vect x(t);
float b[3][3]={{1,2,3},{4,5,6},{7,8,9}};
mat y(b);

x.affiche();
y.affiche();

vect r=y.pmv(x);
r.affiche();
}



/* i keep getting this error : error C2653: 'mat' : is not a class or namespace name*/

oracleguy
11-13-2007, 06:54 PM
That is probably because you are trying to use mat before you ever declare it.

In the future, please put your code in the code tags and highlight the line that the error is referring to.

Before you declare class vect add:
class mat;

codeffect
11-13-2007, 07:27 PM
Thanks, but now i have another error ,now its giving me :

see declaration of 'mat'
: error C2027: use of undefined type 'mat'

ralph l mayo
11-13-2007, 07:55 PM
You need to rearrange things so that nothing is named as a parameter or return type before it is declared, and so that nothing is used before it is defined completely. There are several ways to accomplish this, here's one:


#include<iostream>
using namespace std;

class vect;

class mat
{
float m[3][3];

public :
mat(){
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
m[i][j]=0;
}

mat(float t[3][3]){
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
m[i][j]=t[i][j];
}

void affiche()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout<<m[i][j]<<" ";
cout<<endl;

}
}

inline vect pmv(vect p);
};//end class Mat

class vect
{
float v[3];
public:
vect()
{
for(int i=0;i<3;i++)
v[i]=0;
}

vect(float t[])
{
for(int i=0;i<3;i++)
v[i]=t[i];
}

void affiche()
{
for(int i=0;i<3;i++)
cout<<v[i]<<" ";
cout<<endl;
}

friend vect mat::pmv(vect);
}; //fin class vect

inline vect mat::pmv(vect p)
{
vect res;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
res.v[i]+=(m[i][j])*(p.v[j]);
return res;
}

int main()
{
float t[]={1,2,3};
vect x(t);
float b[3][3]={{1,2,3},{4,5,6},{7,8,9}};
mat y(b);

x.affiche();
y.affiche();

vect r=y.pmv(x);
r.affiche();

return 0;
}


I moved mat above vect for no particular reason apart from that it seemed neater to me and added a declaration for vect above it so the compiler knows that it's a class name (and nothing else, but it's sufficient to declare the vect pmv(vect) member). Since vect mat::pmv(vect) needs a full definition of vect to actually use it, I moved it outside of mat where it can come below the definition. Adding 'inline' makes it compile as if it were listed in the class like you had it, but you'll have to decide for yourself whether or not that's what you really want.

Main should return an int, even if your compiler doesn't complain about the void declaration.

codeffect
11-13-2007, 08:13 PM
Thank you, you helped me alot.
I wanted to make it work without an inline and just with friend member function.