Linked List Confusion - Operator Overloading help?

I am creating a linked list and I am confused about how to operator overload the addition of a linked list. this is my .cpp file and the portion i'm confused about is how to make the addition operator.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  #include "LargeInt.h"
#include <string>

using namespace std;
#include "LargeInt.h"
#include <string>

using namespace std;

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ostream &operator<< (ostream &out, const LargeInt &n){
	//forces the display to be in human readable form, not operational order
	out << n.ToString();
	return out;
}

//-----------------------------------------------------------------------------
LargeInt::LargeInt(const string n)
: head(nullptr)
, end(nullptr)
, negative(false)

{
	this->negative = n[0] == '-';
	unsigned int index = this->negative ? 1 : 0;
	while (index < n.size())
	{
		char c = n[index];
		this->Append(atoi(&c));
		++index;
	}
}


//-----------------------------------------------------------------------------
LargeInt::~LargeInt()
{
	ListNode *nodePTR;
	ListNode *nextNode;

	nodePTR = head;

	while (nodePTR != NULL)
	{
		nextNode = nodePTR->next;
		delete nodePTR;
		nodePTR = nextNode;
	}
}

//-----------------------------------------------------------------------------
void LargeInt::Append(int n)
{
	//Allocate new node
	ListNode *newNode = new ListNode(n);

	//if there are 0 newnodes, make newNode the first node.
	if (this->head == nullptr)
	{
		this->head = newNode;
		this->end = newNode;
	}
	else // insert newnode at the end
	{
		newNode->previous = this-> end;
		this->end->next = newNode;
		this->end = newNode;

	}

}

void LargeInt::addDigits()
{ 
	LargeInt current;
	int carry = 0;
	int offset = 0;
	int temp = 0;

	while (temp->next != nullptr)
		temp = temp->next;



}

//-----------------------------------------------------------------------------
LargeInt LargeInt::operator+()
{
	return 0;
}



bool LargeInt::Negative() const
{
	return this->negative;
}

//-----------------------------------------------------------------------------
string LargeInt::ToString() const
{
	//string message = "";
	string message = this->negative ? "-" : "";

	ListNode *current = this->head;

	while (current != nullptr)
	{
		message += to_string(current->number);
		current = current->next;
	}
	
	return message;
}


If you want to make it work for any numeric data type, you can do it this way and use static_assert to make sure the type used is numeric:

1
2
3
4
5
6
7
8
9
10
template <typename T>
LargeInt LargeInt::operator+(T &other) const
{
    static_assert(decltype(T) == int ||
                  decltype(T) == long ||
                  decltype(T) == LargeInt ||
                  decltype(T) == short /*...etc*/,
                  "The type specified is not a numeric type!");
    return LargeInt("12345678910");
}


Otherwise, if you just want to make it work for LargeInts only, then simply this should suffice:

1
2
3
4
LargeInt LargeInt::operator+(const LargeInt &other) const
{
    return LargeInt("12345678910");
}
Topic archived. No new replies allowed.