我正在尝试创建一个成员函数,它接受四个输入,一个主列表,一个键和两个辅助列表。然后,它根据键将主列表划分为两个次列表。
这是从编程书中摘录的,因此需要有一个成员函数。
我得到这个错误:"->“的基本操作数有非指针类型'UnsortedType‘。
这就是导致问题的整个函数。
void UnsortedType::SplitLists(UnsortedType list,
ItemType item,
UnsortedType& list1,
UnsortedType& list2){
ItemType whichItem;
int numItems = list.GetLength();
//Loop through all items in the list
for(int i = 0; i < numItems; i ++){
whichItem = list.GetNextItem();
try{
switch(whichItem.ComparedTo(item)){
case LESS:
case EQUAL:
if(list1->isFull()){//Error thrown on this line
throw std::string("List1 is full.");
return;
}
//add item to list1
list1->PutItem(whichItem);//Error thrown on this line
break;
case GREATER:
if(list2->isFull()){//Error thrown on this line
throw std::string("List2 is full.");
return;
}
//add item to list2
list2->PutItem(whichItem);//Error thrown on this line
break;
}
}
catch(std::string e){
std::cout << e << std::endl;
}
}
};请注意,"list1“和"list2”是通过引用传递的UnsortedTypes,我认为这是问题的一部分。
我做错了什么?
耽误您时间,实在对不起。
发布于 2019-04-16 07:08:37
可以像使用原始对象一样使用引用。如果通过引用传递内容,则可以使用.而不是->
void UnsortedType::SplitLists(UnsortedType list,
ItemType item,
UnsortedType& list1,
UnsortedType& list2){
ItemType whichItem;
int numItems = list.GetLength();
//Loop through all items in the list
for(int i = 0; i < numItems; i ++){
whichItem = list.GetNextItem();
try{
switch(whichItem.ComparedTo(item)){
case LESS:
case EQUAL:
if(list1.isFull()){//Error thrown on this line
throw std::string("List1 is full.");
return;
}
//add item to list1
list1.PutItem(whichItem);//Error thrown on this line
break;
case GREATER:
if(list2.isFull()){//Error thrown on this line
throw std::string("List2 is full.");
return;
}
//add item to list2
list2.PutItem(whichItem);//Error thrown on this line
break;
}
}
catch(std::string e){
std::cout << e << std::endl;
}
}
};发布于 2019-04-16 07:10:17
您传递的是引用,而不是指针。
类型& type //引用它的用法类似于type.do_smth()
Type* type //指针它的用法类似于type->do_smth() //,因为你必须首先“解引用”它。
一般而言,指针是引用的包装器。指针包含对smth的引用。
https://stackoverflow.com/questions/55698366
复制相似问题