【发布时间】:2023-03-24 10:58:01
【问题描述】:
我试图在不链接的情况下重载 + 运算符,这样我就可以组合两个链表。使用我当前的代码,它似乎根本没有将它们结合起来。
这是我目前重载运算符以组合列表的尝试。
void WORD::operator+ (const WORD & B)
{
character *p = new character;
while (p == 0)
{
Insert(p->symbol);
}
}
这是我得到的帮助编写代码的程序头。
#include <iostream>
#include <string>
using namespace std;
#ifndef WORD_H
#define WORD_H
class character
{
public:
char symbol;
character *next;
};
class WORD
{
public:
bool IsEmpty() { return front == 0; };
int Length(); //Length: Determines the length of the word A; remember A is the current object;
friend ostream & operator<<(ostream & out, const WORD & org); //Overload the insertion operator as a friend function with chaining to print a word A;
void operator=(const string & s);// Overload the assignment operator as a member function to take a
//string (C-style or C++ string, just be consistent in your implementation) as an argument and
//assigns its value to A, the current object;
WORD & operator=(const WORD & w); // Overload the assignment operator as a member function with chaining to take a word
//object as an argument and assigns its value to A, the current object;
void operator+(const WORD & B); //Overload the ‘+” operator as a member function without chaining to add word B
//(adds the set of symbols that makep B's linked list to the back of A's linked list) to the back of word A;
//remember A is the current object;
void WORD::Insert(char key);
WORD();//The default constructor will initialize your state variables.
//The front of the linked list is initially set to NULL or 0; this implies a non-header node
//implementation of the link list.
WORD(const string & s); //Explicit-value constructor: This constructor will have one argument;
//a C-style string or a C++ string representing the word to be created;
WORD(const WORD & org); // Copy Constructor: Used during a call by value, return, or initialization/declaration of a word object;
~WORD(); //Destructor: The destructor will de-allocate all memory allocated for the word. Put the message
//"destructor called\n" inside the body of the destructor.
bool IsEqual(const WORD & B);// Returns true if two word objects are equal; otherwise false; remember A is the current
private:
character *front, *back;
};
#endif
这也是在重载运算符中使用的“插入”函数。
void WORD::Insert(char key)
{
character *p = new character;
p->next = 0;
p->symbol = key;
if (front == 0)
{
front = back = p;
}
else
{
back->next = p;
back = p;
}
}
用于测试重载运算符功能的驱动程序。
cout << "************TEST#8*******************************" << endl;
cout << "Adding 2 words by adding us to the back of their. Their is the current object" << endl;
WORD their("AAAA0000AAA0000AAA");
WORD us("XXXX");
their + us;
cout<<"their followed by us is \n" <<their<< " = AAAA0000AAA0000AAAXXXX"<<endl;
cout << "*************************************************" << endl;
cout<<endl<<endl;
cout << "************TEST#9*******************************" << endl;;
cout << "Adding 2 words, their2 is empty, by adding us to the back of their.Their is the current object" << endl;;
WORD their2("");
their2 + us;
cout<<"their followed by us is \n" <<their<< " = XXXX"<<endl;
cout << "*************************************************" << endl;
cout << endl << endl;
cout << "************TEST#10*******************************" << endl;;
cout << "Adding 2 words, their3 is empty, by adding us to the back of their.Their is the current object" << endl;
WORD their3("");
us + their3;
cout << "us followed by empty their3 is \n" << us << " = XXXX" << endl;
cout << "*************************************************" << endl;
cout<<endl<<endl;
值得一提的是,我无法真正更改“插入”函数或重载运算符的函数头。话虽如此,我将如何组合两个链表?感谢您的帮助。
【问题讨论】:
-
您在
class WORD定义中声明了void WORD::Insert(char key);...当它已经在您的class WORD块中时,您不需要WORD::(实际上它可能无法编译) -
另外,在
operator +重载中,您的while (p==0)不会运行,因为您刚刚将p分配给了一个新角色。实际上,您甚至没有在任何地方使用过输入B,所以我不太确定您期望它如何工作。 -
大概你需要从
B.front开始,复制那个字符,然后得到那个字符的next,复制它,依此类推,直到你到达back
标签: c++ linked-list operator-overloading operator-keyword