Hi everyone,
I am still stuck at using templates inside class. I'm trying to make a BigNumber library in C++ for learning purpose.
my code is:
BigInt.h
Code:#include <iostream> #include <string> /**Specifications: * stores number as: intgr for int part and dec for decimal part * member sign is 1 for positive number and -1 for negative and 0 for Not a Number i.e. NaN * Constructor: can take, int, string(formatted as number), float or double * operators to define +, -, *, /, %, power(), <<, >> */ namespace GG { class BigInt { public: /** Default constructor */ BigInt(); template<typename T> BigInt(const T&); /** Default destructor */ virtual ~BigInt(); /** Copy constructor * \param other Object to copy from */ BigInt(const BigInt& other); /** Assignment operator * \param other Object to assign from * \return A reference to this */ BigInt& operator=(const BigInt& other); friend std::ostream& operator<<(std::ostream& os, const BigInt& bigInt); friend std::istream& operator>>(std::istream& is, BigInt& bigInt); protected: private: /** * Number is represented as: * +0.123324 or -0.1232432 * +123 or -123 etc * [+ or -] then [numbers] then maybe [.] then [more numbers] */ std::string intgr; // Integer part of number i.e. 23 in 23.45 std::string dec; // decimal part of number i.e. 45 in 23.45 short sign; // 1 for positive number, -1 for negative and 0 for NaN (Not a Number) }; template<typename T> BigInt::BigInt(const T& n) { std::string num_str = std::to_string(n); // |note: previous declaration as ‘std::__cxx11::string num_str’| BigInt(num_str); // |error: conflicting declaration ‘GG::BigInt num_str’| } //Specialiazed for string template<> inline BigInt::BigInt<std::string>(const std::string& str) { BigInt(); } //Specialized for char template<> inline BigInt::BigInt<char>(const char& ch) { BigInt(); } }// namespace GG ends here
BigInt.cpp
Code:#include "BigInt.h" #include <string> #include <regex> namespace GG { #define DEBUG_SYMS #undef DEBUG_SYMS BigInt::BigInt() { //ctor dec = "NaN"; intgr = "NaN"; sign = 0; } BigInt::~BigInt() { //dtor } BigInt::BigInt(const BigInt& other) { //copy ctor } BigInt& BigInt::operator=(const BigInt& rhs) { if (this == &rhs) return *this; // handle self assignment //assignment operator this->intgr = rhs.intgr; this->dec = rhs.dec; this->sign = rhs.sign; return *this; } std::ostream& operator<<(std::ostream& os, const BigInt& bigInt) { std::string num_str = ""; if(bigInt.sign == 0) { os << "NaN"; return os; } else if(bigInt.sign == -1) num_str = "-"; num_str += bigInt.intgr; if(bigInt.dec != "0") { num_str += "."; num_str += bigInt.dec; } os << num_str; return os; } std::istream& operator>>(std::istream& is, BigInt& bigInt) { std::string input_num; is>>input_num; bigInt = BigInt(input_num); return is; } }// namespace GG ends here
main.cpp
Code:#include <iostream> #include "BigInt.h" using namespace std; int main() { short short_n = 123; GG::BigInt n(short_n); GG::BigInt n_default; std::cout << n << " " << n_default ; return 0; }
I have written all the code for the sake of completeness so that you could reproduce the problem. But you need to look mainly in BigInt.h in which I am facing problem in specialized string contructor.
errors are as follows:
1. ||error: conflicting declaration ‘GG::BigInt num_str’||
2. ||note: previous declaration as ‘std::__cxx11::string num_str’||
Please guide me.




Reply With Quote
