0
我试图将obj对象中包含的值传递给函数addnode
,但是出现代码块错误,它无法将obj
从mos*
转换为mos
。如何重写这个函数以传递指向函数的指针addnode
代码如下所示。将对象指针传递给函数
#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.
}
非常好,谢谢。我没想到问题出现在函数声明中。最后,你知道我可以如何重写我的主要功能,使它更有效率吗?如果你这样做,你可以在你的回复中发布代码吗? – urbanslug 2013-04-04 16:40:55
@MosesNjagiMwaniki更高效?在如何实现链表?或者只是你的代码? – Caribou 2013-04-04 16:44:32
@MosesNjagiMwaniki它真的没有什么低效率。我不知道你使用'std :: nothrow'的原因,我也建议不要使用'namespace std''。不要忘记,标准输出流可能不一定会自动刷新,因此您可能需要执行'<< std :: flush'(或'<< std :: endl',这也会给你一个新行)。 – 2013-04-04 16:44:53