<<方法名称>>"未在此范围内声明

'<<name of method>>' was not declared in this scope

本文关键字:gt lt 范围内 声明 方法      更新时间:2023-10-16

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

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

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

并将另一个列表作为const &传递。

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

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

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; }

Also, get_prim:

Node *get_prim() { return prim; };

然后导致operator+看起来更像:(const &如Thomas建议)

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;
}

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

将程序修改如下,但还是有问题。如果我尝试在过程"operator+"之外显示列表,我会得到"分割错误"(当我调试程序时,它立即发生在此指令之后:"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行,变量/日期,过程/方法的名称,也有一些评论写在罗马尼亚(我的自然语言),这将是一个新的人更难理解它。