C++ 重载问题
一个sequence.h文件
- C/C++ code
//in file sequence.h-----------------------------#include<iostream>typedef int ItemType;using namespace std;class Sequence{friend ostream &operator<<( ostream & out, const Sequence &aSeq);public: /*Default constructor: constructs an empty Sequence object where: capacityS == maxSize (default value 1024); itemPtr points to a dynamic array with capacityS as its capacity and sizeS is set to 0. ------------*/ Sequence(int maxSize = 1024); /*Destructor: the memory dynamically allocated by the constructor for the array pointed to by itemPtr has been returned to the heap.-----*/ ~Sequence(); /* Copy constructor: constructs a copy of origSeq.-----------------------*/ Sequence(const Sequence & origSeq); /* Assignment operator: assigns a copy of the rightSeq to the current object. A const reference to this Sequence is returned.----*/ const Sequence & operator=(const Sequence& rightSeq); /* Returns true if the sequence is empty, false if not.----------------*/ bool empty() const; /* The value item is inserted into the sequence at the position determined by pos, provided there is room (i.e. sizeS < capacityS) and pos is a legal position (i.e. 0 <= pos <= sizeS).159.234-2012 Semester 22 When pos is illegal a message is display and the sequence is left unchanged.When there is no room for a new element the program terminates.---------*/ void insert(ItemType item, int pos); /* The value at the position determined by pos is removed, provided pos is a legal position. Otherwise a message is display and the sequence is left unchanged.--------------------------- */ void erase(int pos);/* The sequence represented by this Sequence object has been inserted into out.----------- */ void display(ostream & out) const;private: int sizeS; // current size of Sequence int capacityS; // current capacity of dynamic array ItemType * itemPtr; // pointer to dynamic array};//info about the authors of sequence.cpp codevoid info();下面是sequence.cpp文件,是完成他的头文件的。
- C/C++ code
//Secquence main function#include <iostream>#include <cstdlib>#include "sequence.h"using namespace std;//Sequence seq;Sequence::Sequence(int maxSize){ capacityS = maxSize; sizeS=0;// seq.itemPtr=(ItemType*) malloc(itemPtr, maxSize * sizeof(int)); }Sequence::~Sequence(){}Sequence::Sequence(const Sequence & origSeq){ }const Sequence & operator=(const Sequence& rightSeq){ }void Sequence::insert(ItemType item, int pos){ if(item>capacityS){ cout<<"Illegal location to insert -- "<<item<<". Sequence unchanged. ***"<<endl; } else{ sizeS++; itemPtr[sizeS - 1] = item; }}bool Sequence::empty() const{ if(itemPtr==NULL){ return true; } else {return false;}}//delete the element in the current positionvoid Sequence::erase (int pos){ if(pos>capacityS){ cout<<"Illegal location. Sequence unchanged."<<endl; } else{ for(int i=pos; i<sizeS-1; i++){ itemPtr[i]=itemPtr[i+1]; sizeS--; } }}问题出现在了重载里面,他的返还值是:
const Sequence& operator=(const Sequence&)' must be a nonstatic member function
还有一个是:
const Sequence& operator=(const Sequence&)' must take exactly two arguments
求哪位大师可以帮我看看小弟那里写错了,而且重载这里面怎么写呢??谢谢大家啦~~~
[解决办法]
在sequence.cpp中,将
const Sequence & operator=(const Sequence& rightSeq)
改为
const Sequence& Sequence::operator=(const Sequence& rightSeq)