C++超载算子[]
我想为我的deque类实现一个带[]的索引。我为此写了自己的课。。但是,我得到一个错误:在“[”标记之前出现意外的限定符id 也许我不太明白操作符[]是如何工作的,我不能对数组使用常用的[]索引,因为我为我的deque类实现了一个指针结构 我将我的类分为一个.cpp文件和一个.h文件-这里是完整的文件,但可能只有 string&Deque::operator[]int i{method在.cpp文件中很有趣 .cpp: .h文件:C++超载算子[],c++,operator-overloading,C++,Operator Overloading,我想为我的deque类实现一个带[]的索引。我为此写了自己的课。。但是,我得到一个错误:在“[”标记之前出现意外的限定符id 也许我不太明白操作符[]是如何工作的,我不能对数组使用常用的[]索引,因为我为我的deque类实现了一个指针结构 我将我的类分为一个.cpp文件和一个.h文件-这里是完整的文件,但可能只有 string&Deque::operator[]int i{method在.cpp文件中很有趣 .cpp: .h文件: #include <iostream> #inclu
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Node
{
private:
string data="\0";
Node* next=0;
Node* prev=0;
public:
Node(){};
Node(string p){
this->data = p;
}
//KOPIERKONSTRUKTOR MIT POINTER
Node(Node * n) {
this->data = n->getData();
this->next = n->getNext();
this->prev = n->getPrev();
}
//KOPIERKONSTRUKTOR MIT REFERENZPARAMETER
Node(Node& n) {
Node * tmp = &n;
this->data = tmp->getData();
this->next = tmp->getNext();
this->prev = tmp->getPrev();
delete tmp;
}
//ZUWEISUNGSOPERATOR(=)
Node& operator=(Node& n) {
Node * tmp = &n;
this->data = tmp->getData();
this->next = tmp->getNext();
this->prev = tmp->getPrev();
return *this;
}
string getData(){
return this->data;
}
string getData() const{
return this->data;
}
void setData(string v) {
this->data = v;
}
Node * getNext() {
return this->next;
}
Node * getNext() const{
return this->next;
}
Node * getPrev() {
return this->prev;
}
Node * getPrev() const {
return this->prev;
}
void setNext(Node * n) {
this->next = n;
}
void setPrev(Node * n) {
this->prev = n;
}
};
class Deque
{
private:
Node* front;
Node* rear;
int count;
public:
Deque()
{
front =0 ;
rear =0;
count = 0;
}
typedef Node* iterator;
//ELEMENT AM ANFANG HINZUFUEGEN
void push_front(string element)
{
// Create a new node
Node* tmp = new Node(element);
//tmp->data = element;
//tmp->next = 0;
//tmp->prev = 0;
if ( isEmpty() ) {
// erstes Element hinzufuegen
front = rear = tmp;
}
else {
// Ganz vorne anhaengen und Pointer umbiegen
//tmp->next = front;
tmp->setNext(front);
//front->prev = tmp;
front->setPrev(tmp);
front = tmp;
}
count++;
}
//ERSTES ELEMENT ENTFERNEN
string pop_front()
{
if ( isEmpty() ) {
cout << "Deque ist leer" <<endl;
}
// Wert aus erstem Knoten holen
string ret = front->getData();
// Ersten Knoten loeschen und Wert holen
Node* tmp = front;
if ( front->getNext() != 0 )
{
front = front->getNext();
front->setPrev(0) ;
}
else
{
front = 0;
}
count--;
delete tmp;
return ret;
}
//ELEMENT AM ENDE HINZUFUEGEN
void push_back(string element)
{
// neuen Tmp Knoten erzeugen
Node* tmp = new Node();
//tmp->data = element;
//tmp->next = 0;
//tmp->prev = 0;
tmp->setData(element);
tmp->setNext(0);
tmp->setPrev(0);
if ( isEmpty() ) {
front = rear = tmp;
}
else {
// hinten an Liste anhaengen, Pointer umbiegen
//rear->next = tmp;
rear->setNext(tmp);
//tmp->prev = rear;
tmp->setPrev(rear);
rear = tmp;
}
count++;
}
void append_Node(Node * firstNode)
{
if (isEmpty()) {
front = rear = firstNode;
}
else
{
rear->setNext(firstNode);
firstNode->setPrev(rear);
}
}
//LETZTES ELEMENT ENTFERNEN
string pop_back()
{
if ( isEmpty() ) {
throw "Deque ist leer";
}
string ret = rear->getData();
// letzten Knoten loeschen und Pointer umsetzen
Node* tmp = rear;
if ( rear->getPrev() != 0 )
{
rear = rear->getPrev();
rear->setNext(0);
}
else
{
rear = 0;
}
count--;
delete tmp;
return ret;
}
//PEEK ERSTES ELEMENT
string getFirst()
{
if ( isEmpty() )
cout << "Deque ist leer"<<endl;
return front->getData();
}
//PEEK LETZTES ELEMENT
string getLast()
{
if ( isEmpty() )
cout << "Deque ist leer"<<endl;
return rear->getData();
}
//ANZAHL ELEMENTE IM DECK ZURUECKGEBEN
int Size()
{
return count;
}
void setSize(int size){
this->count = size;
}
//DECK LEER?
bool isEmpty();
Node * getFront() {
return front;
}
Node * getRear() {
return rear;
}
//"ITERATOR" DEQUE EINMAL DURCHLAUFEN VORNE BIS HINTEN
//in der ueb3.cpp Datei implementiert
void ausgeben();
Deque& operator+=(Deque& a);
Deque& operator+(Deque& a);
void iterieren(Deque& d);
//string& operator[]( int i);
};
下面是编译的运算符[]的代码:
string& Deque::operator[](int i) {
// You need to check whether i is in dequeue range, and, if not in range, throw an exception
assert( i>=0 && i< this->Size()-1);
int start = 0;
string found ;
if (this->isEmpty()){
cout<<"Deque ist leer!" <<endl;
}
Node * tmp = this->getFront();
bool gefunden = false;
while (!gefunden && start < i)
{
// In this loop we will always find appropriate element
if (start!=i) {
tmp = tmp->getNext();
if (tmp == 0) {
cout <<"Es existiert kein " << i<<".tes Element" <<endl;
}
start++;
}
if ( start == i) {
break; // OK, we found one
//string * found = new String(tmp->getData()); // It won't be compiled
string * found = new string(tmp->getData());
cout <<i << ".ter Wert: " <<found<<endl;
gefunden = true;
return * found;
}
}
return tmp->getDataRef();
//return 0; This string is buggy
}
我很确定错误消息中包含行号。您应该向我们显示错误来自哪一行。代码太多。请将示例简化为一点大小。使用此代码,您不应该在运算符[]的定义方面遇到任何错误…您的声明和实现都已注释掉。您的运算符[]动态分配它找到的字符串的副本并返回它?这是个坏主意。为什么不返回一个对你找到的字符串的常量引用。至少,如果你想返回一个副本,去掉新的并返回你通过值找到的字符串。+1在Praetorian注释中-实际使用运算符的那一行已经创建了内存泄漏:q[o];它甚至不会真正编译,因为语法应该是q[o];这可能就是你的编译器错误。返回的引用从未存储在任何地方,并且会丢失。我看到太多明显的错误,甚至没有尝试编译它。我仍然有一个问题..在cout@user2883596之后,应该有类似于在cout之后抛出std::runtime_errorEmpty deque的内容
string& Deque::operator[](int i) {
// You need to check whether i is in dequeue range, and, if not in range, throw an exception
assert( i>=0 && i< this->Size()-1);
int start = 0;
string found ;
if (this->isEmpty()){
cout<<"Deque ist leer!" <<endl;
}
Node * tmp = this->getFront();
bool gefunden = false;
while (!gefunden && start < i)
{
// In this loop we will always find appropriate element
if (start!=i) {
tmp = tmp->getNext();
if (tmp == 0) {
cout <<"Es existiert kein " << i<<".tes Element" <<endl;
}
start++;
}
if ( start == i) {
break; // OK, we found one
//string * found = new String(tmp->getData()); // It won't be compiled
string * found = new string(tmp->getData());
cout <<i << ".ter Wert: " <<found<<endl;
gefunden = true;
return * found;
}
}
return tmp->getDataRef();
//return 0; This string is buggy
}
class Node {
...
std::string& getDataRef() {
return this->data;
}
...
}