c++新手求教 运行出错
在头文件中:#ifndef_MYVECTOR_H_#define_MYVECTOR_H_#include<iostream>#include<string>usingna...
在头文件中:
#ifndef _MYVECTOR_H_
#define _MYVECTOR_H_
#include <iostream>
#include <string>
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 <string>
#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 myv1(1,2,3);
myVector myv2(2,3,4);
myVector myv3;
myv3 = myv1 + myv2;
cout<<myv3;
cin.get();
return 0;
}
运行后出现错误提示:
main.cpp
vector.cpp
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.cpp(27) : error C2248: 'x' : cannot access private member declared in class 'myVector'
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.h(9) : see declaration of 'x'
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.cpp(27) : error C2248: 'y' : cannot access private member declared in class 'myVector'
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.h(10) : see declaration of 'y'
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.cpp(27) : error C2248: 'z' : cannot access private member declared in class 'myVector'
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.h(11) : see declaration of 'z'
执行 cl.exe 时出错.
vectortest.exe - 1 error(s), 0 warning(s) 展开
#ifndef _MYVECTOR_H_
#define _MYVECTOR_H_
#include <iostream>
#include <string>
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 <string>
#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 myv1(1,2,3);
myVector myv2(2,3,4);
myVector myv3;
myv3 = myv1 + myv2;
cout<<myv3;
cin.get();
return 0;
}
运行后出现错误提示:
main.cpp
vector.cpp
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.cpp(27) : error C2248: 'x' : cannot access private member declared in class 'myVector'
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.h(9) : see declaration of 'x'
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.cpp(27) : error C2248: 'y' : cannot access private member declared in class 'myVector'
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.h(10) : see declaration of 'y'
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.cpp(27) : error C2248: 'z' : cannot access private member declared in class 'myVector'
c:\program files\microsoft visual studio\myprojects\progrem1\vectortest\vector.h(11) : see declaration of 'z'
执行 cl.exe 时出错.
vectortest.exe - 1 error(s), 0 warning(s) 展开
展开全部
temp.x = this->x + V.x;
temp.y = this->y + V.y;
temp.z = this->z + V.z;
xyz是类的私有数据成员,对象不能直接访问
可以将xyz改为public,但是这种方法不推荐,因为要保证类的封装性。
你可以写三个public方法来获得类的私有数据成员,如下:
public:
int GetX(){return x;}
int GetY(){return y;}
int GetY(){return z;}
如果想要修改数据成员的话,可以写自己写set方法。
temp.y = this->y + V.y;
temp.z = this->z + V.z;
xyz是类的私有数据成员,对象不能直接访问
可以将xyz改为public,但是这种方法不推荐,因为要保证类的封装性。
你可以写三个public方法来获得类的私有数据成员,如下:
public:
int GetX(){return x;}
int GetY(){return y;}
int GetY(){return z;}
如果想要修改数据成员的话,可以写自己写set方法。
展开全部
ostream& operator <<(ostream& out,myVector V)
你把这个输出流重载运算符这样写试试,std::ostream& operator<<(std::ostream&out,myVector V);注意声明部分和定义部分都要改
如果还不行,你就定义成友元重载
你把这个输出流重载运算符这样写试试,std::ostream& operator<<(std::ostream&out,myVector V);注意声明部分和定义部分都要改
如果还不行,你就定义成友元重载
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
根号(a+b+c)的平方+根号(a-b-c)的平方+根号(b-c-a)的平方+根号(c-a-b)的平方
=a+b+c+b+c-a+c+a-b+a+b-c
=2a+2b+2c
=a+b+c+b+c-a+c+a-b+a+b-c
=2a+2b+2c
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
private :
int x;
int y;
int z;
这里定义为私有成员了,所以外部不可以访问:
out<<"( "<<V.x<<","<<V.y<<","<<V.z<<" )"<<endl;
int x;
int y;
int z;
这里定义为私有成员了,所以外部不可以访问:
out<<"( "<<V.x<<","<<V.y<<","<<V.z<<" )"<<endl;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
temp.x = this->x + V.x;
temp.y = this->y + V.y;
temp.z = this->z + V.z;
x, y ,z,是私有变量,不能使用 V.x这种方式
temp.y = this->y + V.y;
temp.z = this->z + V.z;
x, y ,z,是私有变量,不能使用 V.x这种方式
追问
想要获得同样结果要怎么改
追答
定义一个get和set函数吧,public的
int Get(){
teturn x;
}
就可以了
ostream& operator <<(ostream& out,myVector V)
{
out<<"( "<<V.x<<","<<V.y<<","<<V.z<<" )"<<endl;
return out;
}
这个函数也是同样的问题~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询