我想独立重载operator+
以连接 2 个双链列表。我的想法是从第一个列表中获取第一个元素的地址,从第二个列表中获取第一个元素的地址。
在DoubleChainedList
课堂上,除了构造函数、析构函数和接下来的 4 个工作正常的方法外,我创建了一个名为的方法get_prim
,它应该让我从指定列表中获取第一个元素的地址。然后,使用get_current
我想在第一个列表中移动直到它结束的方法,同时在第三个列表中添加元素,然后将相同的原则应用于第二个列表。
但我有一个问题,我明白了
'get_prim' was not declared in this scope
和
'get_current' was not declared in this scope
当我编译时,在粗体标记的行(参见下面的代码)处。我错过了什么?
#include <iostream>
#include <stdlib.h>
using namespace std;
//Create node class
class Node
{
private:
float value;
Node *back;
Node *next;
public:
Node *set_value (Node *x, float element) { x->value=element; return x; }
float get_value (Node *x) { return x->value; }
Node *set_back (Node *x) { return x->back; }
Node *set_next (Node *x) { return x->next; }
Node *set_back_nod (Node *x, Node *y) { x->back=y; return x; }
Node *set_next_nod (Node *x, Node *y) { x->next=y; return x; }
void next_to_2next (Node *x) { x->next=x->next->next; }
void next_back_to_origins (Node *x) { x->next->back=x; }
};
//Create list class
class DoubleChainedList : public Node
{
private:
Node *prim;
Node *ultim;
public:
DoubleChainedList() { prim=NULL; ultim=prim; } //Constructor
~DoubleChainedList(); //Destructor
void insert_back(float element); //Inserts an element at the end of the list
void delete_element_from_position(int delete_position); //Deletes from the list the element whose position is equal to "delete_position"
void show_left_right(); //Shows the list from the first element to the last one
void show_right_left(); //Shows the list from the last element to the first one
Nod *get_prim (DoubleChainedList myList) { return this->prim; }; //Intended to obtain the address of the first element from "myList"
Nod *get_current (Node *x) { return set_next(x); }; //Intended to move me through the list
};
DoubleChainedList operator+ (DoubleChainedList myList1, DoubleChainedList myList2)
{
DoubleChainedList myList3;
Nod *current1,*current2;
current1=get_prim(myList1); // ERROR OVER HERE!
current2=get_prim(myList2);
cout<<get_value(current1)<<" "; // ERROR OVER HERE!
cout<<get_value(current2)<<" ";
return myList3;
}
int main()
{
int i,number_elem_myList1,number_elem_myList2,element;
DoubleChainedList myList1,myList2,myList3;
cin>>number_elem_myList1;
for (i=0;i<number_elem_myList1;i++)
{
cin>>element;
myList1.insert_back(element);
}
cin>>number_elem_myList2;
for (i=0;i<number_elem_myList2;i++)
{
cin>>element;
myList2.insert_back(element);
}
myList3=myList1+myList2;
return 0;
}