【问题标题】:How to call a function which is type predefined struct如何调用类型为预定义结构的函数
【发布时间】:2016-05-03 05:29:36
【问题描述】:

这是我第一次提问。论坛对我很有帮助,所以我会尽量只给你多汁的部分:

我有两个函数,一个是搜索函数,它通过指针搜索预先创建的二叉搜索树(我可以通过不同的函数显示搜索树,所以我知道它已填充)以获取特定值。它将来自该节点的信息放入具有相同类型变量(int、float 和 string)的预定义数据结构 Nubline,然后返回该数据结构。

这是我的代码:

struct node
{
    int id;
    string name;
    float balance;
    node *left;
    node *right;
};
node *rootID, *rootName;

struct Nubline
{
    int ID;
    string Name;
    float Amnt;
};
//Search function; the node is a pointer to a linked list with move id's node *left and node *right;    
Nubline SearchbyID(node* &t, int x)
{
    if (t != NULL)
    {
        if (t->id == x)
        {
            Nubline r;
            r.ID = t->id;
            r.Name = t->name;
            r.Amnt = t->balance;
            return r;
        }
        SearchbyID(t->left, x);
        SearchbyID(t->right, x);
    }
}
//function that calls the search function
void BalancebyID()
{
    int num;
    cout << "\tWhat is your ID number? "; cin >> num;
    Nubline duke = SearchbyID(rootID, num);
    cout << "\t\t"<< duke.Name << " your balance is $" << duke.Amnt;
}

void main()
{
//calling statement
    BalancebyID();
    system("pause");//pausing to view result
}

它会抛出以下错误:

Expression: "(_Ptr_user & (_BIG_ALLOCATION_ALIGNMENT -1)) == 0

我认为我已经将问题缩小到函数初始化,因为我可以使函数无效并运行(当然,没有所有其他代码)。我也可以使函数无效,设置一个 Nubline 类型的任意全局变量并将其放在变量“r”所在的位置,然后在我的 BalancebyID 函数中使用它,但它只显示零,所以我可以假设它没有填充。

抱歉,这篇文章冗长。

Tl;dr:如何创建返回数据结构的函数?

【问题讨论】:

  • 函数不是struct。一个函数返回一个,它有一个类型。另外,放弃void main 教过的任何书——绝对是int main
  • 您可以通过让node 包含Nubline 而不是名称稍有不同的相同3 个成员来简化您的代码

标签: c++ function data-structures linked-list binary-search-tree


【解决方案1】:

为确保SearchbyID 正常工作,您应该将return 添加到所有条件中。

此外,您可以将返回类型设为 Nubline*,然后您可以返回 nullptr 以表示未找到任何内容。

Nubline* SearchbyID(node* t, int x)
{
    if(t == nullptr) return nullptr;

    //else
    if (t->id == x)
    {
        auto r = new Nubline();
        r->ID   = t->id;
        r->Name = t->name;
        r->Amnt = t->balance;
        return r;
    }

    auto pLeft = SearchbyID(t->left, x);
    if (pLeft) return pLeft;

    return SearchbyID(t->right, x);
    //return NULL if nothing found
}

【讨论】:

  • 或者更好的是,返回一个nullptr 值。
  • @Jarod42 我认为在这种情况下,原始拥有指针定义更容易阅读。
  • 从当前的声明中,我不会(错误地)delete 返回指针。智能指针更改 2 行并修复声明。 optional 实际上是一个不错的选择。
  • @Jarod42 好的,好的。如你所愿。
  • 对不起,伙计,但它在BalancebyID() 中引发了访问读取冲突,即使在我将duke 更改为指针并将cout&lt;&lt;duke 语句更改为指针语句之后也是如此。有什么建议吗?
【解决方案2】:

好的,这是我的解决方案:

我取消了这个函数,使用了一个全局变量 Nubline r; 并将 t 设置为如下:

void SearchbyID(node* &t, int x)
{
    if (t != NULL);
    {
        if (t->id == x)
        {
            r.ID = t->id;
            r.Name = t->name;
            r.Amnt = t->balance;
        }
//I also changed this to make it more efficient and not throw an access violation up by the if(t->id == x) statement
        if (x < t->id)
        {
            SearchbyID(t->left, x);
        }
        if (x > t->id)
        {
            SearchbyID(t->right, x);
        }
    }
}
//PART B
//Option 1: show Balance when ID is given
void BalancebyID()
{
    int num;
    cout << "\tWhat is your ID number? "; cin >> num;
    SearchbyID(rootID, num);
    cout << "\t\t"<< r.Name << " your balance is $" << r.Amnt;
}

这对我有用。谢谢大家的解决方案;它帮助我隔离了问题并找到了解决方案。

【讨论】:

    猜你喜欢
    • 2021-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    • 2015-11-19
    相关资源
    最近更新 更多