<<方法名称>>"未在此范围内声明
'<<name of method>>' was not declared in this scope
我想独立重载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行,变量/日期,过程/方法的名称,也有一些评论写在罗马尼亚(我的自然语言),这将是一个新的人更难理解它。
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中