首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将对象指针传递给函数

将对象指针传递给函数
EN

Stack Overflow用户
提问于 2013-04-05 00:34:49
回答 1查看 82关注 0票数 0

我试图将obj对象中包含的值传递给函数addnode,但是我得到了一个代码块错误,它无法将objmos*转换为mos。如何重写此代码以传递指向函数addnode的指针,代码如下所示。

代码语言:javascript
复制
#include <iostream>
#include <sstream>

using namespace std;

struct mos
{
    int x;
    float y;
    mos * next;
};

void addnode (mos);

int main()
{
    mos * obj = new (nothrow) mos;
    //Check for proper memory allocation.
    if (obj == NULL)
    {
        cout << "\nProblem assigning memory.\n";
    }
    else
    {
        cout << "\n Memory well allocated.\n Result is: " << obj;
    }

    addnode(obj);
    return 0;
}

void addnode (mos * head)
{
    //code that adds a node to the last node in the linked list.
}   
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-05 00:35:58

您的函数声明和定义不匹配。如果要传递mos*,请将声明更改为:

代码语言:javascript
复制
void addnode(mos*);

在编译器看到您对addnode的调用时,它只看到了一个接受mos而不是mos*的声明。

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

https://stackoverflow.com/questions/15816811

复制
相关文章

相似问题

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