0

I need help creating my operator+ function.The first thing i did was send the binary numbers to the reverselist funtion, but i ended up getting some errors.

Binary.cpp: In function binary operator+(const binary&, const binary&)': Binary.cpp:164: error: invalid initialization of reference of type 'nodeType*&' from expression of type 'nodeType* const' Binary.cpp:98: error: in passing argument 1 ofvoid reverselist(nodeType&)'
Binary.cpp:165: error: invalid initialization of reference of type 'nodeType
&' from expression of type 'nodeType* const'
Binary.cpp:98: error: in passing argument 1 of `void reverselist(nodeType*&)'

#include <iostream>
#include<cstdlib>
using namespace std;

struct nodeType
{
int info;
nodeType *link;
};

class binary
{
    friend ostream& operator<<(ostream& outfile,const binary& outclass);
    friend istream& operator>>(istream& infile, binary & inclass);
    friend binary operator+(const binary& num1,const binary& num2);

    public:
           binary();
           binary(const binary & inclass);
           ~binary();
           const binary & operator =(const binary & otherlist);
           int getcount() const;
    private:
            nodeType *head_ptr;
            int count;
    };

void list_clear(nodeType*&head_ptr);
void list_copy(const nodeType*source_ptr, 
     nodeType*&head_ptr, nodeType*&tail_ptr);
void reverselist(nodeType*&mylist);

binary::binary()
{
    count = 0;
    head_ptr = NULL;
}
binary::binary(const binary & inclass)
{
    nodeType *tail_ptr;
    list_copy(inclass.head_ptr, head_ptr, tail_ptr);
}
binary::~binary()
{
    list_clear(head_ptr);
}
const binary & binary::operator =(const binary & otherlist)
{
       nodeType *tail_ptr;
      if(this != &otherlist)
               list_clear(head_ptr);      
      list_copy(otherlist.head_ptr,head_ptr,tail_ptr);
               return otherlist;

}
int binary::getcount() const
{
    return count;
}
void list_clear(nodeType*&head_ptr)
{
    nodeType *temp;

    while(head_ptr != NULL)
    {
    temp = head_ptr;
    head_ptr = head_ptr->link;
    delete temp;
    }
}
void list_copy(nodeType*&source_ptr, nodeType*&head_ptr, nodeType*&tail_ptr)
{
     nodeType *temp;
     head_ptr = NULL;
     tail_ptr = NULL;

     if(source_ptr == NULL)
         return;

     head_ptr = new nodeType;
     head_ptr->link = NULL;
     head_ptr->info = source_ptr->info;
     tail_ptr = head_ptr;

     source_ptr = source_ptr->link;

     while(source_ptr !=NULL)
     {
                      temp = new nodeType;
                      temp->link = NULL;
                      temp->info = source_ptr->info;
                      tail_ptr->link = temp;
                      tail_ptr = tail_ptr->link;
                      source_ptr = source_ptr->link;
     }
}
void reverselist(nodeType*&mylist)
{
    nodeType * templist;
    nodeType * tempnode;
    templist = NULL;

    while(mylist != NULL)
    {
    tempnode = mylist;
    mylist = mylist->link;
    tempnode->link = templist;
    templist = tempnode;
    }
    mylist = templist;
}
istream& operator>>(istream & infile, binary & inclass)
{
    nodeType *newNode, *tail;
    int numconv;
    char ch;

    tail = NULL;
    inclass.head_ptr = NULL;
    list_clear(inclass.head_ptr);

    infile>>ws;
    infile.get(ch);


    while(infile && isdigit(ch))
    {
    numconv = ch -'0';//Converts the number to ASCII
    newNode= new nodeType;
    newNode->info = numconv;
    newNode->link = NULL;

    if(inclass.head_ptr == NULL)
    {
         inclass.head_ptr = newNode;
    }
    else
    {
         tail->link = newNode;
    }
         tail = newNode;
         infile.get(ch);
    }
    infile>>numconv;
}
ostream& operator<<(ostream& outfile,const binary& outclass)
{

    nodeType *newNode;
    newNode = new nodeType;
    newNode = outclass.head_ptr;

    while(newNode != NULL)
    {
                  outfile<<newNode->info<< " ";
                  newNode=newNode->link;
    }
    return outfile;
}
binary operator+(const binary& num1,const binary& num2)// Adds 2 binary numbers 1 bit at a time
{ 
      int carry = 0;
      int sum =0;
      reverselist(num1.head_ptr);
      reverselist(num2.head_ptr);      
}

Edited by deceptikon: Please don't create code snippets when asking a question

2
Contributors
1
Reply
15
Views
4 Years
Discussion Span
Last Post by Labdabeta
2

The issue is that reverselist as you wrote it expects a reference to a pointer of a nodetype, but you passed it num#.head_ptr which is of type pointer which will be converted to a const reference to a pointer of a nodetype (since num1,num2 are declared const in the function definition). Basically you either need to edit reverselist so that it takes const references (I doubt that will work at all!) or you can edit the binary + operator so that it takes binary references (minus the const).

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.