main.cpp
Code:
#include <cstdlib>
#include <iostream>
#include "int128_t.h"
int main(int argc, char **argv)
{
using namespace std;
int128_t a = 3434;
cout << a << endl;
return EXIT_SUCCESS;
}
int128_t.h
Code:
#include <cstdint>
#include <iostream>
class int128_t
{
/* Basic arithmetic */
friend inline int128_t operator+(const int128_t &, const int128_t &);
friend inline int128_t operator-(const int128_t &, const int128_t &);
friend inline int128_t operator*(const int128_t &, const int128_t &);
friend inline int128_t operator/(const int128_t &, const int128_t &);
/* Logical comparison */
friend inline bool operator==(const int128_t &, const int128_t &);
template <typename T> friend inline bool operator==(const int128_t &, const T &);
template <typename T> friend inline bool operator==(const T &, const int128_t &);
friend inline bool operator!=(const int128_t &, const int128_t &);
template <typename T> friend inline bool operator!=(const int128_t &, const T &);
template <typename T> friend inline bool operator!=(const T &, const int128_t &);
friend inline bool operator<(const int128_t &, const int128_t &);
template <typename T> friend inline bool operator<(const int128_t &, const T &);
template <typename T> friend inline bool operator<(const T &, const int128_t &);
friend inline bool operator<=(const int128_t &, const int128_t &);
template <typename T> friend inline bool operator<=(const int128_t &, const T &);
template <typename T> friend inline bool operator<=(const T &, const int128_t &);
friend inline bool operator>(const int128_t &, const int128_t &);
template <typename T> friend inline bool operator>(const int128_t &, const T &);
template <typename T> friend inline bool operator>(const T &, const int128_t &);
friend inline bool operator>=(const int128_t &, const int128_t &);
template <typename T> friend inline bool operator>=(const int128_t &, const T &);
template <typename T> friend inline bool operator>=(const T &, const int128_t &);
/* Input/Output */
friend std::ostream &operator<<(std::ostream &ostr, const int128_t &rhs);
private:
int64_t lo, hi;
public:
/* Assignment and SignEqual operators */
inline int128_t &operator=(const int128_t &);
template <typename T> inline int128_t &operator=(const T &);
inline void operator+=(const int128_t &);
template <typename T> inline void operator+=(const T &);
inline void operator-=(const int128_t &);
template <typename T> inline void operator-=(const T &);
inline void operator*=(const int128_t &);
template <typename T> inline void operator*=(const T &);
inline void operator/=(const int128_t &);
template <typename T> inline void operator/=(const T &);
};
template <typename T>
inline int128_t &int128_t::operator=(const T &rhs)
{
lo = rhs;
hi = 0;
return *this;
}
/* Basic arithmetic */
template <typename T>
inline void int128_t::operator+=(const T &rhs)
{
lo += rhs;
hi = 0;
}
template <typename T>
inline void int128_t::operator-=(const T &rhs)
{
lo -= rhs;
hi = 0;
}
template <typename T>
inline void int128_t::operator*=(const T &rhs)
{
lo *= rhs;
hi = 0;
}
template <typename T>
inline void int128_t::operator/=(const T &rhs)
{
lo += rhs;
hi = 0;
}
/* Logical comparison */
template <typename T>
inline bool operator==(const int128_t &lhs, const T &rhs)
{
return (lhs.lo == rhs) && (lhs.hi == 0);
}
template <typename T>
inline bool operator==(const T &lhs, const int128_t &rhs)
{
return (lhs == rhs.lo) && (rhs.hi == 0);
}
template <typename T>
inline bool operator!=(const int128_t &lhs, const T &rhs)
{
return (lhs.lo != rhs) || (lhs.hi != 0);
}
template <typename T>
inline bool operator!=(const T &lhs, const int128_t &rhs)
{
return (lhs != rhs.lo) || (rhs.hi != 0);
}
template <typename T>
inline bool operator<(const int128_t &lhs, const T &rhs)
{
return (lhs.lo < rhs) && (lhs.hi == 0);
}
template <typename T>
inline bool operator<(const T &lhs, const int128_t &rhs)
{
return (lhs < rhs.lo) && (rhs.hi == 0);
}
template <typename T>
inline bool operator<=(const int128_t &lhs, const T &rhs)
{
return (lhs.lo <= rhs) && (lhs.hi == 0);
}
template <typename T>
inline bool operator<=(const T &lhs, const int128_t &rhs)
{
return (lhs <= rhs.lo) && (rhs.hi == 0);
}
template <typename T>
inline bool operator>(const int128_t &lhs, const T &rhs)
{
return (lhs.lo > rhs) && (lhs.hi == 0);
}
template <typename T>
inline bool operator>(const T &lhs, const int128_t &rhs)
{
return (lhs > rhs.lo) && (rhs.hi == 0);
}
template <typename T>
inline bool operator>=(const int128_t &lhs, const T &rhs)
{
return (lhs.lo >= rhs) && (lhs.hi == 0);
}
template <typename T>
inline bool operator>=(const T &lhs, const int128_t &rhs)
{
return (lhs >= rhs.lo) && (rhs.hi == 0);
}
int128_t.cpp
Code:
#include "int128_t.h"
/* Basic arithmetic */
inline int128_t &int128_t::operator=(const int128_t &rhs)
{
lo = rhs.lo;
hi = rhs.hi;
return *this;
}
inline int128_t operator+(const int128_t &a, const int128_t &b)
{
int128_t x;
x.lo = a.lo + b.lo;
x.hi = a.hi + b.hi;
return x;
}
inline int128_t operator-(const int128_t &a, const int128_t &b)
{
int128_t x;
x.lo = a.lo - b.lo;
x.hi = a.hi - b.hi;
return x;
}
inline int128_t operator*(const int128_t &a, const int128_t &b)
{
int128_t x;
x.lo = a.lo * b.lo;
x.hi = a.hi * b.hi;
return x;
}
inline int128_t operator/(const int128_t &a, const int128_t &b)
{
int128_t x;
x.lo = a.lo / b.lo;
if(b.hi == 0) x.hi = 0;
else x.hi = a.hi / b.hi;
return x;
}
inline void int128_t::operator+=(const int128_t &rhs)
{
lo += rhs.lo;
hi += rhs.hi;
}
inline void int128_t::operator-=(const int128_t &rhs)
{
lo -= rhs.lo;
hi -= rhs.hi;
}
inline void int128_t::operator*=(const int128_t &rhs)
{
lo *= rhs.lo;
hi *= rhs.hi;
}
inline void int128_t::operator/=(const int128_t &rhs)
{
lo /= rhs.lo;
if(rhs.hi == 0) hi = 0;
else hi /= rhs.hi;
}
/* Logical comparison */
inline bool operator==(const int128_t &lhs, const int128_t &rhs)
{
return (lhs.lo == rhs.lo) && (lhs.hi == rhs.hi);
}
inline bool operator!=(const int128_t &lhs, const int128_t &rhs)
{
return (lhs.lo != rhs.lo) || (lhs.hi != rhs.hi);
}
inline bool operator<(const int128_t &lhs, const int128_t &rhs)
{
return lhs.hi < rhs.hi;
}
inline bool operator<=(const int128_t &lhs, const int128_t &rhs)
{
return lhs.hi <= rhs.hi;
}
inline bool operator>(const int128_t &lhs, const int128_t &rhs)
{
return lhs.hi > rhs.hi;
}
inline bool operator>=(const int128_t &lhs, const int128_t &rhs)
{
return lhs.hi >= rhs.hi;
}
/* Input/Output */
std::ostream &operator<<(std::ostream& ostr, const int128_t &rhs)
{
if(rhs.hi == 0) ostr << rhs.lo;
return ostr;
}
I can't find out what's the problem. When trying to assing 3434 to a in main.cpp I get an error: "conversion from ‘int’ to non-scalar type ‘int128_t’ requested" although I have made a template to accept ints and such. Something is obviously wrong, but I don't know what. I'm weary of swimming trough code and diving in the internet. There are probably tons of errors and some enlightenment on this matter is more than welcome.