c++ 运算符重载问题求大神

class point
{
private:
int x,int y;
public:
point &operator =(point &p); //这里有问题
point operator +(point &p);
point operator +=(point &P);

3个回答

carina_ssy 谢谢大神！
3 年多之前 回复

nuaaysh 我表示很无语啊
3 年多之前 回复

c++ 运算符重载问题求大神

c++刚学 运算符重载 求大神帮忙指点 感激不尽
#ifndef DECIMAL_H #define DECIMAL_H #include <iostream> using std::ostream; using std::istream; class Decimal { public: friend istream &operator>>( istream &, Decimal & ); friend ostream &operator<<(ostream &, const Decimal & ); Decimal( double = 0.0 ); void setInteger( double ); void setDecimal( double ); Decimal &operator=( const Decimal ); const Decimal operator+( const Decimal ); Decimal &operator+=( Decimal ); Decimal operator++(); //Decimal &operator++( double ); Decimal &operator++( int ); bool operator==( const Decimal ); bool operator!=( const Decimal&right ) { return ! (*this==right); } private: // friend ostream &operator<<( const Decimal & ); double integer; double decimal; }; #endif #include <iostream> using std::cout; using std::cin; #include <cmath> #include "decimal.h" Decimal::Decimal( double num ) { decimal = modf( num, &integer ); } ostream & operator<<( ostream & output,const Decimal & d ) { double dec = 0; dec = floor( d.decimal * 100 ); if ( dec < 0 ) dec = 0 - dec; if ( d.decimal != 0 ) { output << floor( d.integer ) << "."; if ( dec > 10 ) output << dec; else output << "0" << dec; } else output << d.integer; return output; } istream& operator>>( istream & input, Decimal & d ) { double num; cout << "Enter a number: "; input >> num; d.decimal = modf( num, &d.integer ); return input; } Decimal &Decimal::operator=( const Decimal d ) { integer = d.integer; decimal = d.decimal; return *this; } void Decimal::setDecimal( double d ) { decimal = d; } void Decimal::setInteger( double i ) { integer = i; } const Decimal Decimal::operator+(const Decimal d ) { Decimal result; result.setDecimal( decimal + d.decimal ); result.setInteger( integer + d.integer ); if ( result.decimal >= 1 ) { result.decimal = result.decimal - 1.0; result.integer++; } else if ( result.decimal <= -1 ) { result.decimal = result.decimal + 1.0; result.integer--; } return result; } Decimal& Decimal::operator+=( Decimal d ) { *this += d; return *this; } Decimal Decimal::operator++() { integer++; return *this; } Decimal &Decimal::operator ++(int ) { Decimal temp = *this; integer++; return *this; } bool Decimal::operator==( const Decimal d ) { return ( integer == d.integer && decimal == d.decimal ); } #include <iostream> using std::cout; using std::endl; using std::cin; #include "decimal.h" int main() { Decimal test1, test2, test3( 1.234 ); cout << "Initial values:\n"; cout << test1 << endl << test2 << endl << test3 << endl << endl; cin >> test1 >> test2; cout << "The sum of test1 and test2 is: " << test1 + test2 << endl; test3 += ++test2; cout << "\nfinal values:\n" << "test1 = " << test1 << endl << "test2 = " << test2 << endl << "test3 = " << test3 << endl; if ( test1 != test3) cout << "test1 and test3 are not equal to each other\n"; return 0; }
C++ 运算符重载问题，求大神们帮忙，一直找不到解决办法
#include <iostream> #include <iomanip> using namespace std; template<class T> class TripleList; template<class T> class Triple { private: int row,col; T val; public: friend class TripleList<T>; friend istream& operator >> (istream& i,TripleList<T> &M); friend ostream& operator << (ostream& o,TripleList<T> &M); Triple(){} Triple(int r, int c, T v):row(r),col(c),val(v){} }; template<class T> class TripleList { private: Triple<T> *data; int rows,cols,nums;//nums表示非零元素的个数 int maxSize; public: TripleList(int size=40); ~TripleList(){delete[] data;} bool Find(Triple<T> value); bool Insert(Triple<T> value); friend istream& operator >> (istream& i, TripleList<T> &M); friend ostream& operator << (ostream& o,TripleList<T> &M); bool Add(TripleList<T> &M, TripleList<T> &result); bool Multiply(TripleList<T> &M, TripleList<T> &result); void Transpose(TripleList<T> &M);//矩阵转置 void FastTranspose(TripleList<T> &M); }; template<class T> TripleList<T>::TripleList(int size) { nums=0; maxSize=size; data=new Triple<T>[maxSize]; } template<class T> bool TripleList<T>::Insert( Triple<T> val)//const { if(nums<=maxSize) { cout<<"fulled!"<<endl; return false; } if(val.row>rows || val.col>cols || val.row<0 || val.row<0) { cout<<"行标或列标越界！"<<endl; return false; } data[nums++]=val; return true; } template<class T> bool TripleList<T>::Find(Triple<T> value) { for(int i=0;i<nums;i++) { if(data[i]==value) { cout<<"OK"<<endl; return true; } } cout<<"can not find!"<<endl; return false; } template<class T> istream& operator >> (istream &i, TripleList<T> &M) { int j=0; cout<<"输入矩阵的行数 "; i>>M.rows; cout<<"输入矩阵的列数 "; i>>M.cols; cout<<"输入矩阵的非零元素的个数 "; i>>M.nums; cout<<"输入"<<M.nums<<"个元素的三元组数据值：(行标从零开始）"<<endl; while(i<M.nums) { i>>M.data[j].row>>M.data[j].col>>M.data[j].val; if(M.data[j].row>=M.rows || M.data[j].col>=M.cols || M.data[j].row<0 || M.data[j].col<0) { cout<<" 行标或列标越界，请重新输入 "<<endl; continue; } j++; } return i; } template<class T> ostream& operator << (ostream &o, TripleList<T> &M) { o<<"这是一个"<<M.rows<<"X"<<M.cols<<" 的稀疏矩阵，它共有"<<M.nums<<"个非零元素！"<<endl; o<<setw(10)<<"行数"<<setw(10)<<"列数"<<setw(10)<<"值"<<endl; for(int i=0;i<M.nums;i++) { o<<M.data[i].row<<setw(10)<<M.data[i].col<<setw(10)<<M.data[i].val<<endl; } return o; } template<class T> void TripleList<T>::Transpose(TripleList<T> &M)//矩阵的转置 { if(!nums){return;} M.data=new Triple<T>[nums]; M.rows=cols,M.cols=rows; for(int i=0;i<nums;i++) { M.data[i].row=data[i].col; M.data[i].col=data[i].row; M.data[i].val=data[i].val; } } int main() { TripleList<int> T,Arr; cin>>T; cout<<T; T.Transpose(Arr); cout<<Arr; return 0; } ``` ``` Undefined symbols for architecture x86_64: "operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, TripleList<int>&)", referenced from: _main in main.o "operator>>(std::__1::basic_istream<char, std::__1::char_traits<char> >&, TripleList<int>&)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

C++运算符重载还有类的默认构造函数的问题

c++运算符重载和模板，求大神指教
``` #include<iostream> using namespace std; /* API: (1)push (2)pop (3)isEmpty (4)Length */ //////////////////////////////////////////////////////////////////////// /* 顺序结构实现栈 */ /* 顺序实现的栈的结构 */ #define MAXSIZE 1000 template <class T> class Stack_array{ private: T data[MAXSIZE]; int top; public: Stack_array(){ top = -1; } //是否为空 bool isEmpty(){ if (top == -1) return true; return false; } //插入一个数据 bool push(T item){ if (top > MAXSIZE - 1) { cout << "Overflow the size of the stack" << endl; return false; } ++top; data[top] = item; return true; } //删除一个元素 bool pop(T & item){ if (top == -1){ cout << "The stack is Empty" << endl; return false; } item=data[top]; --top; return true; } //得打栈的长度 int Length(){ return top + 1; } void print(){ for (int i = 0; i <= top; i++) { cout << data[i] << " "; } cout << endl; } friend ostream &operator <<(ostream &, const Stack_array<T>); }; template <class T> ostream & operator <<(ostream & out, const Stack_array<T> & a){ for (int i = 0; i <= a.top; i++) { out << a.data[i] << " "; } out << endl; return out; } void main(){ Stack_array<int> s; s.push(1); s.push(2); int item = 0; s.pop(item); cout << item << endl; cout << s << endl; s.print(); system("pause"); } ``` ![图片说明](https://img-ask.csdn.net/upload/201612/31/1483195276_738019.png)

![图片说明](https://img-ask.csdn.net/upload/201905/09/1557394505_482810.png) 以下是老师上课给的例子，将其按图片要求改动即可 ``` #include<iostream.h> //using namespace std; class Tpoint{ private:int x,y; public: /*Tpoint(){ x=1; y=1; cout<<"default constructor"<<endl; }*/ Tpoint(int a=0,int b=0){ x=a;y=b; cout<<"constructor,x="<<x<<"y="<<y<<endl; } Tpoint(const Tpoint&c ) { x = c.x; y = c.y; cout<<"copy constructor,x="<<x<<"y="<<y<<endl; } ~Tpoint(){ cout<<"delete,x="<<x<<"y="<<y<<endl; } Tpoint operator+(const Tpoint& p){ Tpoint temp; temp.x=x+p.x; temp.y=y+p.y; return temp; } Tpoint operator++(int){ Tpoint before(x,y); x++; y++; return before; } Tpoint operator++(){ x++; y++; return *this; } bool operator==(const Tpoint& p){ if((x==p.x)&&(y==p.y)) return 1; else return 0; } friend Tpoint operator-(Tpoint &p,Tpoint &q); friend Tpoint operator--(Tpoint &p); friend Tpoint operator--(Tpoint &p,int); friend ostream& operator<<(ostream &out,Tpoint &p); friend istream operator>>(istream &in,Tpoint &p); }; Tpoint operator-(Tpoint &p,Tpoint &q) { Tpoint temp; temp.x=p.x-q.x; temp.y=p.y-q.y; return temp; } Tpoint operator--(Tpoint &p) { p.x--; p.y--; return p; } Tpoint operator--(Tpoint &p,int) { Tpoint before(p.x,p.y); p.x--; p.y--; return before; } ostream& operator<<(ostream &out,Tpoint &p) { out<<"("<<p.x<<","<<p.y<<")"<<endl; return out; } void main(){ Tpoint p1(1,1),p2(3); Tpoint p3=p1+p2; cout<<(p1==p2)<<endl; cout<<p1<<endl; ++p1; --p1; Tpoint p4=p1-p2; } ```

#include<iostream> #include<string> using namespace std; #define max 10010; template<class type> class list; template<class type> class list { private: type *data; int size; public: list() { data = new type[max]; size = 0; } ~list() { delete[] data; } /*list(const list<type> &other) { size = other.size; data = new type[max]; for (int i = 0; i < size; i++) data[i] = other.data[i]; }*/ friend list &operator+(const list<type>& x, const list<type>& y) { list<type> tmp; for (int i = 0; i < x.length(); i++) tmp.push_pack(x.visit(i)); for (int i = x.length(); i < x.length() + y.length(); i++) tmp.push_pack(y.visit(i - x.length())); return tmp; } int length() { return size; } void insert(int i, const type &x); void push_pack(const type &x); type visit(int i); }; template<class type> void list<type>::insert(int i, const type &x) { if (i<0 || i>size) return false; for (int j = size; j > i; j--) data[j] = data[j - 1]; data[i] = x; ++size; } template<class type> void list<type>::push_pack(const type &x) { data[size] = x; ++size; } template<class type> type list<type>::visit(int i) { if (i<0 || i>size) return false; return data[i]; } char str[10]; int n, m; int main() { while (cin>>str) { cin >> n >> m; if (str == "int") { list<int> l1; list<int> l2; int a, b; for (int i = 0; i < n; i++) { cin >> a; l1.push_pack(a); } for (int i = 0; i < m; i++) { cin >> b; l2.push_pack(b); } list<int> l3; l3 = l1 + l2; for (int i = 0; i < l3.length(); i++) cout << l3.visit(i) << ' '; } else if (str == "char") { list<char> l1; list<char> l2; char a, b; for (int i = 0; i < n; i++) { cin >> a; l1.push_pack(a); } for (int i = 0; i < m; i++) { cin >> b; l2.push_pack(b); } list<char> l3; l3 = l1 + l2; for (int i = 0; i < l3.length(); i++) cout << l3.visit(i) << ' '; } else if (str == "double") { list<double> l1; list<double> l2; double a, b; for (int i = 0; i < n; i++) { cin >> a; l1.push_pack(a); } for (int i = 0; i < m; i++) { cin >> b; l2.push_pack(b); } list<double> l3; l3 = l1 + l2; for (int i = 0; i < l3.length(); i++) cout << l3.visit(i) << ' '; } } return 0; }
C++关于运算符重载的问题

C++类中包含数组，如何重载运算符，第一次进入，还没有c币，求大神帮忙了，感激不尽

c＋＋重载二维数组运算符，即两个括号

#include"iostream" #include"fstream" #include"string" using namespace std; class coStu { protected: string name,num; double score; public: coStu(string na,string nu,double s): name(na),num(nu),score(s){} friend ostream & operator<<(ostream &out, coStu &obj); }; ostream & operator<<(ostream &out, coStu &obj) { out<<obj.name<<"\t"<<obj.num<<"\t"<<obj.score<<"\t"; return out; } class master:public coStu { private: string teacherName; public: master(string na,string nu,double s,string tn): teacherName(tn),coStu(na,nu,s){} }; void main() { coStu c("张三","134679",89); master m("李四","316497",79,"丁老师"); }
c++ 运算符的重载 运行时出现 debug 请各位大神帮忙找错

#include<iostream> using namespace std; class RMB{ int yuan,jiao,fen; public: RMB(int y=0,int j=0,int f=0) { yuan=y;jiao=j;fen=f; if(jiao>=10) //让分，角逢十进一 { jiao=jiao-10;yuan=yuan+1; } if(fen>=10) { fen=fen-10; jiao=jiao+1; } } RMB operator+(RMB b); RMB operator-(RMB b); void display(); }; RMB RMB::operator +(RMB b) { yuan=yuan+b.yuan; jiao=jiao+b.jiao; fen=fen+b.fen; if(fen>=10) { fen=fen-10; jiao=jiao+1; if(jiao>=10) { jiao=jiao-10; yuan=yuan+1; } } return RMB(yuan,jiao,fen); } RMB RMB::operator -(RMB b) { if(fen<b.fen) { if(jiao>0) { fen=fen+10; jiao=jiao-1; } else if(yuan>0) { yuan=yuan-1; jiao=jiao+9; fen=fen+10; } } fen=fen-b.fen; if(jiao<b.jiao) if(yuan>0) { yuan=yuan-1; jiao=jiao+10; } jiao=jiao-b.jiao; yuan=yuan-b.yuan; return RMB(yuan,jiao,fen); } void RMB::display() { // if(yuan>=0) cout<<yuan<<"元"<<jiao<<"角"<<fen<<"分"<<endl; // else cout<<"输入有误！"<<endl; } void main() { RMB c(1,2,1);RMB d(3,3,3); RMB e;RMB f; c.display (); d.display (); e=c+d; e.display (); f=d-c; // RMB c1(1,2,1);RMB d1(5,3,3); // f=d1-c1; f.display (); } 如果把f=d-c注释掉，然后后两行不要注释，得出的结果又是对的？
C++等号运算符‘=’重载问题，显示乱码。
c++中定义两个数组类，重载‘=’运算符，令其按位赋值而非直接地址赋值，调用重载操作符后，显示乱码， 刚开始学习c++，求大神帮忙 class ARRAY{ //数组 类 int N; int *arr; public: ARRAY(); ARRAY(int *b,int m); ~ARRAY(){delete []arr;} void setfirst(int a){ *arr=a;} void show(); ARRAY operator=(ARRAY b); }; ARRAY ARRAY::operator=(ARRAY b){ // 重载 = N=b.N; arr=new int[N]; for(int i=0;i<N;i++) arr[i]=b.arr[i]; return *this; } ARRAY::ARRAY(){ //构造函数 N=10; arr=new int[N]; for(int i=0;i<N;i++){ arr[i]=0; } } ARRAY::ARRAY(int *b,int m){ //构造函数 N=m; arr=new int[N]; for(int i=0;i<N;i++){ arr[i]=b[i];} } void ARRAY::show(){ //显示 for(int i=0;i<N;i++){cout<<arr[i]<<' ';} cout<<endl; } void main(){ int d[10]={1,2,3,4,5,6,7,8,9,10}; int N=9; ARRAY a(d,N); ARRAY b; a.show(); b.show(); b=a; //赋值 ？？？？？？？？ cout<<"*************\n"; a.show(); //显示出错 b.show(); cout<<"*************\n"; a.setfirst(10); a.show(); b.show(); cout<<"*************\n"; while(1); } 重载=后显示如下 ![图片说明](https://img-ask.csdn.net/upload/201810/31/1540975585_86538.png)
c++友元函数重载<<运算符后还是无法访问类私有变量。求大神解答
#ifndef A_H_ #define A_H_ class Stonewt { private: enum { Lbs_per_stn = 14 }; enum Mode { Stone, IntPounds, FloatPounds }; Mode m_mode; int mm_stone; double m_left_pbs; double m_pounds; public: Stonewt(); Stonewt(double weight, Mode mode); ~Stonewt(); friend Stonewt operator +(Stonewt& temp1, Stonewt& temp2); friend ostream& operator <<(ostream& os, Stonewt& temp); }; #endif //以上为头文件 #include<iostream> #include"标头.h" using namespace std; Stonewt::Stonewt() { mm_stone = 0; m_left_pbs = 0; m_pounds = 0; } Stonewt::Stonewt(double weight, Mode mode) { m_mode = mode; if (m_mode == Stone) { mm_stone = weight; m_left_pbs = 0; m_pounds = weight*Lbs_per_stn; } else if (m_mode == IntPounds) { mm_stone = (int)weight / Lbs_per_stn; m_left_pbs = (int)weight%Lbs_per_stn; m_pounds = (int)weight*Lbs_per_stn; } else if (m_mode == FloatPounds) { mm_stone = weight / Lbs_per_stn; m_left_pbs = (int)weight%Lbs_per_stn + weight - int(weight); m_pounds = weight*Lbs_per_stn; } else { cout << "模式错误\n"; mm_stone = 0; m_left_pbs = 0; m_pounds = 0; } } Stonewt::~Stonewt() { } Stonewt operator +(Stonewt& temp1, Stonewt& temp2) { Stonewt temp; temp.mm_stone = temp1.mm_stone + temp2.mm_stone; return temp; } ostream& operator<<(ostream& os, Stonewt& temp) { os << "英石：" << temp.mm_stone << " 磅：" << temp.m_left_pbs << endl << "总英石：" << temp.m_pounds << endl; return os; } //以上为类实现文件 在最后一个operator函数中无法访问 temp.mm_stone；temp.m_left_pbs； temp.m_pounds 。但是把类的定义和类方法写在同一个文件上却不抱错不知道为什么，我用的VA2015![图片说明](https://img-ask.csdn.net/upload/201710/01/1506833609_421846.png)
C++ 操作符重载的内存释放问题
=操作符重载，给复构造函数进行赋值时的代码如下： MyString & MyString::operator =(const MyString &str) { if(this == &str) return *this; delete []m_pData; //**???????????????????????????? ** m_pData = NULL; m_pData = new char(strlen(str.m_pData) + 1); strcpy (m_pData, str.m_pData); return *this; } class MyString { public: .... private: char *m_pData; } 在函数中为什么还要是对原有的内存进行释放，即delete []m_pData，直接赋值 m_pData = str.m_pData， 这样不行吗？ 请大神指点。

<textarea readonly="readonly" name="code" class="c++"> ``` #include<iostream.h> #include<iomanip.h> class operation {public: static void rank(); //输入阶数 void array(); //输入矩阵 void display(); friend operation operator +(operation &,operation &); friend operation operator -(operation &,operation &); friend operation operator *(operation &,operation &); private: int a[50][50]; static int n; }; void operation::rank() {cin>>n;} void operation::array() { for(int i=0;i<n;i++) for(int j=0;j<n;j++) cin>>a[i][j]; } operation operator +(operation &a1,operation &a2) { operation a3; for(int i=0;i<operation::n;i++) for(int j=0;j<operation::n;j++) a3.a[i][j]=a1.a[i][j]+a2.a[i][j]; return a3; } operation operator -(operation &a1,operation &a2) { operation a3; for(int i=0;i<operation::n;i++) for(int j=0;j<operation::n;j++) a3.a[i][j]=a1.a[i][j]-a2.a[i][j]; return a3; } operation operator *(operation &a1,operation &a2) { operation a3; for(int k=0;k<operation::n;k++) { for(int i=0;i<operation::n;i++) { for(int j=0;j<operation::n;j++) a3.a[k][i]+=a1.a[k][j]*a2.a[j][i]; } } return a3; } void operation::display() { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) cout<<setw(10)<<operation::a[i][j]; cout<<endl; } } int operation::n; #include"operation.h" int main() { operation a1,a2,a3; cout<<"piease input rank:"; operation::rank(); cout<<"please input a1:"<<endl; a1.array(); cout<<"please input a2:"<<endl; a2.array(); cout<<"a1+a2:"<<endl; a3=a1+a2; a3.display(); cout<<"a1-a2:"<<endl; a3=a1-a2; a3.display(); cout<<"a1*a2:"<<endl; a3=a1*a2; a3.display(); return 0; } ``` </textarea>

《MySQL 性能优化》之理解 MySQL 体系结构

python自动下载图片

【前言】 　　收到一封来信，赶上各种事情拖了几日，利用今天要放下工作的时机，做个回复。 　　2020年到了，就以这一封信，作为开年标志吧。 【正文】 　　您好，我是一名现在有很多困惑的大二学生。有一些问题想要向您请教。 　　先说一下我的基本情况，高考失利，不想复读，来到广州一所大专读计算机应用技术专业。学校是偏艺术类的，计算机专业没有实验室更不用说工作室了。而且学校的学风也不好。但我很想在计算机领...

【CSDN编者按】1月2日，阿里巴巴发布《达摩院2020十大科技趋势》，十大科技趋势分别是：人工智能从感知智能向认知智能演进；计算存储一体化突破AI算力瓶颈；工业互联网的超融合；机器间大规模协作成为可能；模块化降低芯片设计门槛；规模化生产级区块链应用将走入大众；量子计算进入攻坚期；新材料推动半导体器件革新；保护数据隐私的AI技术将加速落地；云成为IT技术创新的中心 。 新的画卷，正在徐徐展开。...

Python+OpenCV实时图像处理

2020年一线城市程序员工资大调查

python爬取百部电影数据，我分析出了一个残酷的真相
2019年就这么匆匆过去了，就在前几天国家电影局发布了2019年中国电影市场数据，数据显示去年总票房为642.66亿元，同比增长5.4%；国产电影总票房411.75亿元，同比增长8.65%，市场占比 64.07%；城市院线观影人次17.27亿，同比增长0.64%。 看上去似乎是一片大好对不对？不过作为一名严谨求实的数据分析师，我从官方数据中看出了一点端倪：国产票房增幅都已经高达8.65%了，为什...

Windows可谓是大多数人的生产力工具，集娱乐办公于一体，虽然在程序员这个群体中都说苹果是信仰，但是大部分不都是从Windows过来的，而且现在依然有很多的程序员用Windows。 所以，今天我就把我私藏的Windows必装的软件分享给大家，如果有一个你没有用过甚至没有听过，那你就赚了????，这可都是提升你幸福感的高效率生产力工具哦！ 走起！???? NO、1 ScreenToGif 屏幕，摄像头和白板...

C++(数据结构与算法):62---搜索树（二叉搜索树、索引二叉搜索树）

AI 没让人类失业，搞 AI 的人先失业了

2020年，冯唐49岁：我给20、30岁IT职场年轻人的建议

B站是个宝，谁用谁知道???? 作为一名大学生，你必须掌握的一项能力就是自学能力，很多看起来很牛X的人，你可以了解下，人家私底下一定是花大量的时间自学的，你可能会说，我也想学习啊，可是嘞，该学习啥嘞，不怕告诉你，互联网时代，最不缺的就是学习资源，最宝贵的是啥？ 你可能会说是时间，不，不是时间，而是你的注意力，懂了吧！ 那么，你说学习资源多，我咋不知道，那今天我就告诉你一个你必须知道的学习的地方，人称...