求高手指点:我想将<<符重载后使它能输出一个类的内容,但是使用时系统报错,提示如下:
ompiling...main.cppVector.cppC:\Users\Administrator\Desktop\VectorTest\Vector.cpp(26)...
ompiling...
main.cpp
Vector.cpp
C:\Users\Administrator\Desktop\VectorTest\Vector.cpp(26) : error C2248: 'x' : cannot access private member declared in class 'myVector'
c:\users\administrator\desktop\vectortest\vector.h(9) : see declaration of 'x'
C:\Users\Administrator\Desktop\VectorTest\Vector.cpp(26) : error C2248: 'y' : cannot access private member declared in class 'myVector'
c:\users\administrator\desktop\vectortest\vector.h(10) : see declaration of 'y'
C:\Users\Administrator\Desktop\VectorTest\Vector.cpp(26) : error C2248: 'z' : cannot access private member declared in class 'myVector'
c:\users\administrator\desktop\vectortest\vector.h(11) : see declaration of 'z'
执行 cl.exe 时出错.
VectorTest.exe - 1 error(s), 0 warning(s)
好像是提示不能直接访问私有成员x,y,z?怎么回事呢
源程序如下:
头文件:Vector.h
#ifndef _MYVECTOR_H_
#define _MYVECTOR_H_
#include <string>
#include <iostream>
using namespace std;
class myVector
{
private:
int x;
int y;
int z;
public:
myVector();
myVector(int X,int Y,int Z);
myVector operator+(myVector V); //重载加号运算符声明
friend ostream& operator<<(ostream& out,myVector V); //重载输出流运算符声明
};
#endif
Vector.cpp:
#include <iostream>
#include "Vector.h"
using namespace std;
myVector::myVector()
{
x=0;
y=0;
z=0;
}
myVector::myVector(int X,int Y,int Z)
{
x=X;
y=Y;
z=Z;
}
myVector myVector::operator+(myVector V)
{
myVector temp;
temp.x=this->x+V.x;
temp.y=this->y+V.y;
temp.z=this->z+V.z;
return temp;
}
ostream& operator<<(ostream& out,myVector V)
{
out<<"("<<V.x<<","<<V.y<<","<<V.z<<")"<<endl;
return out;
}
main.cpp:
#include <iostream>
#include "Vector.h"
using namespace std;
int main()
{
myVector vec1(4,5,8);
myVector vec2(8,9,6);
myVector vec3;
vec3=vec1+vec2;
cout<<vec3;
cin.get();
return 0;
}
我是初学者,这都是书本的原程序 ,我敲上去怎么就是编译出错呢???? 展开
main.cpp
Vector.cpp
C:\Users\Administrator\Desktop\VectorTest\Vector.cpp(26) : error C2248: 'x' : cannot access private member declared in class 'myVector'
c:\users\administrator\desktop\vectortest\vector.h(9) : see declaration of 'x'
C:\Users\Administrator\Desktop\VectorTest\Vector.cpp(26) : error C2248: 'y' : cannot access private member declared in class 'myVector'
c:\users\administrator\desktop\vectortest\vector.h(10) : see declaration of 'y'
C:\Users\Administrator\Desktop\VectorTest\Vector.cpp(26) : error C2248: 'z' : cannot access private member declared in class 'myVector'
c:\users\administrator\desktop\vectortest\vector.h(11) : see declaration of 'z'
执行 cl.exe 时出错.
VectorTest.exe - 1 error(s), 0 warning(s)
好像是提示不能直接访问私有成员x,y,z?怎么回事呢
源程序如下:
头文件:Vector.h
#ifndef _MYVECTOR_H_
#define _MYVECTOR_H_
#include <string>
#include <iostream>
using namespace std;
class myVector
{
private:
int x;
int y;
int z;
public:
myVector();
myVector(int X,int Y,int Z);
myVector operator+(myVector V); //重载加号运算符声明
friend ostream& operator<<(ostream& out,myVector V); //重载输出流运算符声明
};
#endif
Vector.cpp:
#include <iostream>
#include "Vector.h"
using namespace std;
myVector::myVector()
{
x=0;
y=0;
z=0;
}
myVector::myVector(int X,int Y,int Z)
{
x=X;
y=Y;
z=Z;
}
myVector myVector::operator+(myVector V)
{
myVector temp;
temp.x=this->x+V.x;
temp.y=this->y+V.y;
temp.z=this->z+V.z;
return temp;
}
ostream& operator<<(ostream& out,myVector V)
{
out<<"("<<V.x<<","<<V.y<<","<<V.z<<")"<<endl;
return out;
}
main.cpp:
#include <iostream>
#include "Vector.h"
using namespace std;
int main()
{
myVector vec1(4,5,8);
myVector vec2(8,9,6);
myVector vec3;
vec3=vec1+vec2;
cout<<vec3;
cin.get();
return 0;
}
我是初学者,这都是书本的原程序 ,我敲上去怎么就是编译出错呢???? 展开
2个回答
展开全部
这是vc的一个经典的bug...按照标准友元函数是可以访问的,是VC的BUG..按照我这样改下就行了.
#include <string>
#include <iostream>
using std::cin;
using std::endl;
using std::cout;
using std::ostream;
using std::istream;
class myVector
{
private:
int x;
int y;
int z;
public:
myVector();
myVector(int X,int Y,int Z);
myVector operator+(myVector V); //重载加号运算符声明
friend ostream& operator<<(ostream& out,myVector V); //重载输出流运算符声明
};
myVector::myVector()
{
x=0;
y=0;
z=0;
}
myVector::myVector(int X,int Y,int Z)
{
x=X;
y=Y;
z=Z;
}
myVector myVector::operator+(myVector V)
{
myVector temp;
temp.x=this->x+V.x;
temp.y=this->y+V.y;
temp.z=this->z+V.z;
return temp;
}
ostream& operator<<(ostream& out,myVector V)
{
out<<"("<<V.x<<","<<V.y<<","<<V.z<<")"<<endl;
return out;
}
int main()
{
myVector vec1(4,5,8);
myVector vec2(8,9,6);
myVector vec3;
vec3=vec1+vec2;
cout<<vec3;
cin.get();
return 0;
}
#include <string>
#include <iostream>
using std::cin;
using std::endl;
using std::cout;
using std::ostream;
using std::istream;
class myVector
{
private:
int x;
int y;
int z;
public:
myVector();
myVector(int X,int Y,int Z);
myVector operator+(myVector V); //重载加号运算符声明
friend ostream& operator<<(ostream& out,myVector V); //重载输出流运算符声明
};
myVector::myVector()
{
x=0;
y=0;
z=0;
}
myVector::myVector(int X,int Y,int Z)
{
x=X;
y=Y;
z=Z;
}
myVector myVector::operator+(myVector V)
{
myVector temp;
temp.x=this->x+V.x;
temp.y=this->y+V.y;
temp.z=this->z+V.z;
return temp;
}
ostream& operator<<(ostream& out,myVector V)
{
out<<"("<<V.x<<","<<V.y<<","<<V.z<<")"<<endl;
return out;
}
int main()
{
myVector vec1(4,5,8);
myVector vec2(8,9,6);
myVector vec3;
vec3=vec1+vec2;
cout<<vec3;
cin.get();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询