[RESOLVED] template function in non-template class
I am trying to implement BigInteger library by myself. I am storing numbers in std::string.
The header file (BigInt.h) contains:
Code:
#ifndef BIGINT_H
#define BIGINT_H
#include <iostream>
//#include <vector>
/**Specifications:
* Constructor: can take, int, string(formatted as number), float or double
* operators to define +, -, *, /, %, power(), <<, >>
*/
class BigInt
{
public:
/** Default constructor */
BigInt();
/** string constructor */
BigInt(const std::string&);
/** int, float or double constructor */
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);
/**
* Overloaded << ostream operator
*/
friend std::ostream& operator<<(std::ostream& os, const 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 dec_rep;
};
#endif // BIGINT_H
definition of member functions is in BigInt.cpp:
Code:
#include "BigInt.h"
#include <string>
#include <regex>
//#include <sstream>
BigInt::BigInt()
{
//ctor
dec_rep = "+0";
}
template<typename T>
BigInt::BigInt(const T& n)
{
dec_rep = std::to_string(n);
if( dec_rep[0] != '+' && dec_rep[0]!='-')
{
dec_rep = "+" + dec_rep;
}
}
/**
* it accepts input as string if it is correctly formatted as number
* accepted forms are: 1234, 1.234, +1233, +1.234, 0.234, .234, +.234, -123 etc
* these are unaccepted; --123, +-23, -+32, 0.234.0 or any other wrong format
*/
BigInt::BigInt(const std::string& str)
{
std::regex re("[-+]?([0-9]*\\.[0-9]+)"); //regular expression to check if input is in proper format
std::smatch m;
if(std::regex_match(str, m, re))
{
dec_rep = str;
if((str[0] == '+' || str[0] == '-') && str[1] == '.')
{
dec_rep = dec_rep[0] + "0" + dec_rep.substr(1);
}
else if(str[0] == '.')
{
dec_rep = "+0" + dec_rep;
}
else if(str[0]>='0' && str[0]<='9')
{
dec_rep = "+" + dec_rep;
}
}
}
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
return *this;
}
std::ostream& operator<<(std::ostream& os, const BigInt& bigInt)
{
os<<bigInt.dec_rep;
return os;
}
main() is in main.cpp:
Code:
#include <iostream>
#include "BigInt.h"
using namespace std;
int main()
{
BigInt n(1); //Line 9 where 1st error comes
BigInt n_default;
BigInt str_n("1"); //Line 11 where 2nd error comes
cout<<n<<" "<<n_default<<" "<<str_n<<endl;
cout << "Hello world!" << endl;
return 0;
}
When I try to run it, it shows following errors:
obj/Debug/main.o||In function `main':|
1. undefined reference to `BigInt::BigInt<int>(int const&)' on line 9 of main.cpp
2. undefined reference to `BigInt::BigInt<char [2]>(char const (&) [2])' on line 11 of main.cpp
3. error: ld returned 1 exit status
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
I'm using code-blocks 16.01 with GCC compiler on Linux Mint 18.2 'Sonya' (64 bit).
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.