'&lt; <name of =“”method =“”>&gt;'未在此范围内声明</name>

时间:2013-03-31 17:41:35

标签: c++

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

3 个答案:

答案 0 :(得分:0)

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

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

将其他列表作为const &传递。

答案 1 :(得分:0)

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

因此,作为类成员的函数不需要作为参数传递该类的对象,除非您想在该函数中使用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+=可能也是一个更好的主意。

答案 2 :(得分:0)

修改程序如下,但我仍然有问题。如果我尝试在“operator +”过程之外显示列表,我会得到“Segmentation fault”(当我调试程序时,它紧跟在这条指令之后:“myList3 = myList1 + myList2;”)。如果我在该程序中显示它,一切都没问题。我认为这是因为“返回”语句而发生的,因为我返回一个临时对象,在“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行,变量/日期的名称,程序/方法以及一些用罗马尼亚语(我的自然语言)写的评论,这将更难有人了解它。