Fibonacci Numbers Problem 2 separate Compilers
Jun 2, 2017 at 9:56pm UTC
I am in a College c++ course working on data structures and I have gotten to the List class we need to use it to implement a Fibonacci number calculator. I have gotten the program to work perfectly on Microsoft Visual Studio however when i upload it to the school Linux Lab using a G++ compiler it fails to compile I cant seem to find the problem between the two compilers. The list.h was the list class i wrote and i know that works fine.
this is the error I'm getting... its being displayed by every fiboNumbers.push_front
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
g++ -c fibonacci.cpp
fibonacci.cpp: In function ‘void fibonacci()’:
fibonacci.cpp:65:45: error: no matching function for call to ‘WholeNumber::WholeNumber(WholeNumber)’
fiboNumbersOne.push_front(WholeNumber(zero));
^
fibonacci.cpp:65:45: note: candidates are:
In file included from fibonacci.cpp:13:0:
fibonacci.h:20:2: note: WholeNumber::WholeNumber(WholeNumber&)
WholeNumber(WholeNumber & rhs);
^
fibonacci.h:20:2: note: no known conversion for argument 1 from ‘WholeNumber’ to ‘WholeNumber&’
fibonacci.h:19:2: note: WholeNumber::WholeNumber(int )
WholeNumber(int input);
^
fibonacci.h:19:2: note: no known conversion for argument 1 from ‘WholeNumber’ to ‘int ’
fibonacci.h:18:2: note: WholeNumber::WholeNumber()
WholeNumber();
^
fibonacci.h:18:2: note: candidate expects 0 arguments, 1 provided
In file included from fibonacci.h:13:0,
from fibonacci.cpp:13:
list.h:255:6: error: initializing argument 1 of ‘void List<T>::push_front(T) [with T = WholeNumber]’
void List<T>::push_front(T item)
this is my .h file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "list.h"
class WholeNumber
{
public :
WholeNumber();
WholeNumber(int input);
WholeNumber(WholeNumber & rhs);
WholeNumber& operator = (WholeNumber & rhs);
WholeNumber& operator += (WholeNumber & rhs);
private :
List <int > * wholeNumberList;
friend std::ostream & operator << (std::ostream & out, WholeNumber & rhs);
};
// the interactive fibonacci program
void fibonacci();
and this is my .cpp file
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
#include <iostream>
#include <iomanip>
#include "fibonacci.h" // for fibonacci() prototype
#include "list.h" // for LIST
using namespace std;
ostream & operator <<(ostream & out, WholeNumber & rhs)
{
ListIterator <int > it;
ListIterator <int > itCheck;
for (it = rhs.wholeNumberList->rbegin(); it != rhs.wholeNumberList->rend(); --it)
{
if (it == rhs.wholeNumberList->rbegin())
{
itCheck = it;
--itCheck;
if (itCheck != rhs.wholeNumberList->rend())
{
cout << *it << "," ;
}
else
{
cout << *it;
}
}
else
{
itCheck = it;
--itCheck;
if (itCheck != rhs.wholeNumberList->rend())
{
cout << setw(3) << setfill('0' ) << *it << "," ;
}
else
{
cout << setw(3) << setfill('0' ) << *it;
}
}
}
return out;
}
/************************************************
* FIBONACCI
* The interactive function allowing the user to
* display Fibonacci numbers
***********************************************/
void fibonacci()
{
List <WholeNumber> fiboNumbersOne;
List <WholeNumber> fiboNumbersTwo;
fiboNumbersOne.push_front(WholeNumber(0));
fiboNumbersOne.push_front(WholeNumber(1));
fiboNumbersOne.push_back(WholeNumber(0));
// show the first serveral Fibonacci numbers
int number;
cout << "How many Fibonacci numbers would you like to see? " ;
cin >> number;
ListIterator <WholeNumber> a = fiboNumbersOne.begin();
ListIterator <WholeNumber> b = fiboNumbersOne.begin();
ListIterator <WholeNumber> c = fiboNumbersOne.begin();
++b;
++c;
++c;
for (int i = 0; i < number; i++)
{
*c = *b;
*b += *a;
*a = *c;
cout << *b << endl;
}
// prompt for a single large Fibonacci
cout << "Which Fibonacci number would you like to display? " ;
cin >> number;
fiboNumbersTwo.push_front(WholeNumber(0));
fiboNumbersTwo.push_front(WholeNumber(1));
fiboNumbersTwo.push_back(WholeNumber(0));
a = fiboNumbersTwo.begin();
b = fiboNumbersTwo.begin();
c = fiboNumbersTwo.begin();
++b;
++c;
++c;
for (int i = 0; i < number; i++)
{
*c = *b;
*b += *a;
*a = *c;
}
cout << *b;
}
WholeNumber::WholeNumber()
{
wholeNumberList = new List<int >;
}
WholeNumber::WholeNumber(WholeNumber & rhs)
{
wholeNumberList = new List<int >;
wholeNumberList = rhs.wholeNumberList;
}
WholeNumber::WholeNumber(int input)
{
wholeNumberList = new List<int >;
if (input < 1000)
{
wholeNumberList->push_back(input);
}
}
WholeNumber & WholeNumber::operator =(WholeNumber & rhs)
{
wholeNumberList = rhs.wholeNumberList;
return *this ;
}
WholeNumber & WholeNumber::operator +=(WholeNumber & rhs)
{
WholeNumber afterAdd;
ListIterator <int > lhsIt = wholeNumberList->begin();
ListIterator <int > rhsIt = rhs.wholeNumberList->begin();
int added = 0;
int modulus = 0;
int i = 0;
if (wholeNumberList->size() < rhs.wholeNumberList->size())
{
for (; i < wholeNumberList->size(); i++)
{
added += *lhsIt + *rhsIt;
if (added >= 1000)
{
modulus = added % 1000;
added = 1;
afterAdd.wholeNumberList->push_back(modulus);
modulus = 0;
}
else
{
afterAdd.wholeNumberList->push_back(added);
added = 0;
modulus = 0;
}
++lhsIt;
++rhsIt;
}
for (; i < rhs.wholeNumberList->size(); i++)
{
afterAdd.wholeNumberList->push_back(*rhsIt + added);
added = 0;
}
}
else if (wholeNumberList->size() == rhs.wholeNumberList->size())
{
for (; i < wholeNumberList->size(); i++)
{
added += *lhsIt + *rhsIt;
if (added >= 1000)
{
modulus = added % 1000;
added = 1;
afterAdd.wholeNumberList->push_back(modulus);
modulus = 0;
}
else
{
afterAdd.wholeNumberList->push_back(added);
added = 0;
modulus = 0;
}
++lhsIt;
++rhsIt;
}
if (added == 1)
{
afterAdd.wholeNumberList->push_back(added);
}
}
else
{
for (; i < rhs.wholeNumberList->size(); i++)
{
added += *lhsIt + *rhsIt;
if (added >= 1000)
{
modulus = added % 1000;
added = 1;
afterAdd.wholeNumberList->push_back(modulus);
modulus = 0;
}
else
{
afterAdd.wholeNumberList->push_back(added);
added = 0;
modulus = 0;
}
++lhsIt;
++rhsIt;
}
for (; i < wholeNumberList->size(); i++)
{
afterAdd.wholeNumberList->push_back(*lhsIt + added);
added = 0;
}
}
*this = afterAdd;
return *this ;
}
i appreciate any help that you can provide thank you
Last edited on Jun 2, 2017 at 9:59pm UTC
Jun 2, 2017 at 11:00pm UTC
You will have to define a move constructor or/and define a constructor which takes WholeNumber by value.
As in define WholeNumber(WholeNumber rhs);
or/and WholeNumber(WholeNumber && rhs);
Jun 3, 2017 at 12:18am UTC
You will have to define a move constructor or/and define a constructor which takes WholeNumber by value.
If you just made your code const-correct it would be fine as is. VC++ has a compiler extension enabled by default that allows a regular reference to bind to a non-const object, which is not allowed by the standard.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
class WholeNumber
{
public :
WholeNumber();
WholeNumber(int input);
WholeNumber(const WholeNumber & rhs);
WholeNumber& operator = (const WholeNumber & rhs);
WholeNumber& operator += (const WholeNumber & rhs);
private :
List <int > * wholeNumberList;
friend std::ostream & operator << (std::ostream & out, const WholeNumber & rhs);
};
Topic archived. No new replies allowed.