0

I'm trying to pass the value contained in the object obj to the function addnode but I get a code blocks error that it can't convert obj from mos* to mos. How could this be rewritten to pass a pointer to the function addnode The code is as shown below.

#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.
}   

1 Answer 1

3

Your function declaration and definition don't match. If you want to pass a mos*, change the declaration to:

void addnode(mos*);

At the time the compiler was seeing your call to addnode, it had only seen a declaration that takes a mos rather than a mos*.

Sign up to request clarification or add additional context in comments.

6 Comments

That worked perfectly well thank you. I didn't expect the problem to be in the function declaration. Finally do you know how I could rewrite my main function to make it more efficient? If you do, could you please post the code it in your reply?
@MosesNjagiMwaniki more efficient? in terms of how to implement a linked list? or just your code there?
@MosesNjagiMwaniki There's really nothing inefficient about it. I don't know your reasoning for using std::nothrow and I also recommend not doing using namespace std; ever. Don't forget that the standard output stream may not necessarily flush automatically, so you might want to do << std::flush (or << std::endl, which will give you a new line too).
@MosesNjagiMwaniki a hint - in terms of a list - is it better to move through them all or go straight to the last? If the latter how could you do that.
@sftrabbit I've used nothrow to check whether a new object has been created. Though no concrete steps would be taken after that. I'll look for alternatives for using namespace std;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.