Problem with template and class

This is probably a really easy fix but I just cant seem to figure it out..

I am trying to make it so say for example I have a class that will store a large number. Then I have derived classes for base units, kilo units , mega....ect..
Then add those derived classes to the large number class but it doesn't seem to be working.

I am trying this
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
class BigInt
{
public:
    BigInt( void );

    template<class T>
    BigInt& operator+= ( const T& ); //compiles but I don't think is correct.
    
    std::string value;

protected:
     void error( const short& ); //assert for value in wrong unit.
};

class Base : public BigInt
{
    Base( void );
    Base( const short& );
    
    std::string value;
};

*******
source file...
*******
template <class T>
BigInt& BigInt::operator+= ( const T &value ) //This compiles but isn't correct I think
{
 //stuff
}

//^^^ This function worked good when I had const Base &value as a parameter
//I am trying to template since all the derived classes are using the same
//thing to add to the base class.

********
main source
********
int main()
{
    BigInt bi;
    Base ba( 400 ); //setting value to 400
    bi.value = "600"; //setting value to 600
    bi += ba;/**This line gets the error**/
}


Error -> undefined reference to `BigInt& BigInt::operator+=<Base>(Base const&)'


Any help would be greatly appreciated.


**Added a little bit more info**
if I do this it works but that would be a really big pain to overload the operator+= like 20 times. I am having a hard time trying to template the parameter of operator+= any help/suggestions would be great :)
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
****
header
****
class A
{
public:
    A();
    template <class T>
    A& operator+= ( const T& );
    A& operator+= ( const A& );
};

class B : public A
{
public:
    B();
}

****
class source
****
template <class T>
A& A::operator+= ( const T& rhs );
A& A::operator+= ( const A& rhs );

****
main source
****
int main()
{
    A a;
    B b( 4 );
    a.value = "3";
    a += b;
    std::cout << a.value << std::endl;
}


**this is just an example code I didn't put in all the variables and stuff it's just so you get the picture please don't tell me this little bit doesn't compile...**
Last edited on
Put a Semicolon after your closing bracket for the class
I have those in my actual code sorry forgot to put on this. Will edit the post. I just typed part of my code.
I still would like to know how to template a function with different objects although I changed my code. I can run my program to do euler project 20 in .2 seconds and my class lets me go way beyond It did 1e10e3 * 1e10e3 in about 12 seconds so I'm not sure if that is good or bad =p and the number have to be less than 1073741820 9's but I guess I could make it go to another string after that but too lazy.
Cool thanks works when I put the definition in the header. I didn't know you had to do that with templates I just figured it was like normal parameters. Also thanks for the info on translation I'll have to read up on that some more later.
Topic archived. No new replies allowed.