首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在成员函数内调用成员函数

在成员函数内调用成员函数
EN

Stack Overflow用户
提问于 2019-04-16 07:05:20
回答 2查看 73关注 0票数 0

我正在尝试创建一个成员函数,它接受四个输入,一个主列表,一个键和两个辅助列表。然后,它根据键将主列表划分为两个次列表。

这是从编程书中摘录的,因此需要有一个成员函数。

我得到这个错误:"->“的基本操作数有非指针类型'UnsortedType‘。

这就是导致问题的整个函数。

代码语言:javascript
运行
复制
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,我认为这是问题的一部分。

我做错了什么?

耽误您时间,实在对不起。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-16 07:08:37

可以像使用原始对象一样使用引用。如果通过引用传递内容,则可以使用.而不是->

代码语言:javascript
运行
复制
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;
        }
    }
};
票数 1
EN

Stack Overflow用户

发布于 2019-04-16 07:10:17

您传递的是引用,而不是指针。

类型& type //引用它的用法类似于type.do_smth()

Type* type //指针它的用法类似于type->do_smth() //,因为你必须首先“解引用”它。

一般而言,指针是引用的包装器。指针包含对smth的引用。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55698366

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档