0

我想独立重载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;
}
4

3 回答 3

0

如果您实现operator+=为成员函数,则可以访问其他列表的变量。

我会实现operator+=,然后遍历另一个列表,将另一个列表的节点附加到这个列表中。

并将另一个列表作为const &.

于 2013-03-31T17:46:39.030 回答
0

用例如someObject.function(...)or调用的函数someObject->function(...)(其中someObject分别是一个对象或指向某个具有function(...)函数的类的对象的指针)可以直接访问someObject.

因此,作为类成员的函数不需要将该类的对象作为参数传递,除非您想在该函数中使用 2 个对象。

的函数Node应该看起来更像:

void set_value (float element) { value = element; }
float get_value () { return value; }
Node *set_back () { return back; }
Node *set_next () { return next; }
void set_back_nod (Node *y) { back = y; }
void set_next_nod (Node *y) { next = y; }
void next_to_2next () { next = next->next; }
void next_back_to_origins () { next->back = this; }

另外,get_prim

Node *get_prim() { return prim; };

然后导致operator+看起来更像:(const &正如托马斯建议的那样)

DoubleChainedList operator+ (const DoubleChainedList &myList1,
                             const DoubleChainedList &myList2)
{
    DoubleChainedList myList3;
    Node *current1, *current2;
    current1 = myList1.get_prim();
    current2 = myList2.get_prim();
    cout << current1->get_value() << " ";
    cout << current2->get_value() << " ";
    // ...
    return myList3;
}

按照托马斯的建议使用operator+=可能也是一个更好的主意。

于 2013-03-31T18:12:36.123 回答
0

修改程序如下,但还是有问题。如果我尝试在“operator+”过程之外显示列表,我会得到“Segmentation fault”(当我调试程序时,它立即发生在该指令之后:“myList3=myList1+myList2;”)。如果我在该过程中显示它,一切都很好。我认为这是因为“return”语句而发生的,并且因为我返回了一个临时对象,该对象在“operator+”过程结束后将不再存在,但我不知道如何解决这个问题。

#include <iostream>
using namespace std;

//Create node class
class Node
{
private:
    float value;         
    Node *back;        
    Node *next;       
public:
    Node *set_value (float element) { value=element; return this; }
    float get_value () { return value; }
    Node *set_back () { return back; }
    Node *set_next () { return next; }
    Nod *set_back_node (Node *y) { back=y; return this; }
    Nod *set_next_node (Node *y) { next=y; return this; }
    void next_to_2next () { next=next->next; }
    void next_back_to_origins () { next->back=this; }
};

//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
    Node *get_prim () { return prim; }                       //Intended to obtain the address of the first element from a list
};

DoubleChainedList operator+ (DoubleChainedList myList1, DoubleChainedList myList2)
{
    DoubleChainedList myList3;
    Node *current1,*current2,*current3;
    current1=myList1.get_prim();
    current2=myList2.get_prim();
    while ((current1!=NULL)||(current2!=NULL))
    {
        if (current1!=NULL)
        {
            myList3.insert_back(current1->get_value());
            current1=current1->set_next();
        }
        else
            if (current2!=NULL)
            {
                myList3.insert_back(current2->get_value());
                current2=current2->set_next();
            }
    }
    //myList3.show_left_right();
    //cout<<endl;
    //myList3.show_right_left();
    return myList3;
}

int main()
{
    int i,number_elem_myList1,number_elem_myList2,element;
    DoubleChainedList myList1,myList2,myList3;
    cin>>nr_elem_lista1;
    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;
    myList3.show_left_right();
    myList3.show_right_left();
    return 0;
}

@Dukeling 如您所说修改了“Node”类的方法(当然还有必要的程序),也可以。

如果有人感兴趣,我可以发布完整的代码,但它有 200 多行、变量/日期、程序/方法的名称以及一些用罗马尼亚语(我的自然语言)写的评论,对于新手来说会更难明白它。

于 2013-04-01T12:16:08.570 回答