Operator overloading mixed with templates. Complexity = High(?)

To begin with: sorry for killing English! hahaha.
It's not my native language, therefore I might have quite enough spelling mistakes and/or gramatical.

I've been writing a program with clases and got way of my skills!

As you read on, I'd like you to tell me, was I correct posting this in begginers forum or should I post it in General C++ forum?

The program uses templates in a mix with operators overloading making it a bit complex.

What it was supposed to be:
2 classes, one being the "Pair" and an other being the "TriPair".
One (Pair) holding two pieces of ANY kind of data, and the other (TriPair)
holding three.
When I tried importing some operators overloading it all went to HELL! XD
I just don't want to touch my program again! If none manages to help, I'll
just chop down the operators overloading and go back to stupid plain classes hahahaha.


My program:


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
#include<stdio.h>
#include<typeinfo>




template <typename T1,typename T2>
class Pair
{
private:
    int static NumberOfPairs;

public:
    T1 a; T2 b; bool Initialized;

    T1 F(void)
    {
        if(Initialized==true)
            return a;
        return '\0';
    }
    T2 S(void)
    {
        if(Initialized==true)
            return b;
        return '\0';
    }

    int Population(void)
    {return NumberOfPairs;}


    Pair<T1,T2> operator+ (Pair<T1,T2> TempPair)
    {
        if( (typeid(T1)!=typeid(bool)) && (typeid(T2)!=typeid(bool)) )
        {
            Pair<T1,T2> ReturnPair;

            if( (typeid(T1)!=typeid(char)) && (typeid(T1)!=typeid(char*)) )
            {
                ReturnPair.a=F()+TempPair.F();
            }
            else if( typeid(T1)==typeid(char*) )
            {
                char* Temp=F()+TempPair.F();
                ReturnPair.a=Temp;
            }
            else //T1==Char
            {
                char Temp[2]; Temp[1]=F(); Temp[2]=TempPair.F();
                char* ToReturn=Temp;
                ReturnPair.a=ToReturn;
            }

            if( (typeid(T2)!=typeid(char))&& (typeid(T2)!=typeid(char*)) )
            {
                ReturnPair.b=S()+TempPair.S();
            }
            else if( typeid(T2)==typeid(char*) )
            {
                char* Temp=S()+TempPair.S();
                ReturnPair.b=Temp;
            }
            else //T2==Char
            {
                char Temp[2]; Temp[1]=S(); Temp[2]=TempPair.S();
                char* ToReturn=Temp;
                ReturnPair.b=ToReturn;
            }

            return ReturnPair;
        }





    /*  //In the beginning I had the following code which I later on swapped with the one above
    template<typename T3,typename T4>
    Pair<T3,T4> operator+ (Pair<T1,T2> TempPair)
    {
        if( (typeid(T1)!=typeid(bool)) && (typeid(T2)!=typeid(bool)) )
        {
            Pair<T3,T4> ReturnPair;

            if( (typeid(T1)!=typeid(char)) && (typeid(T1)!=typeid(char*)) )
            {
                ReturnPair.a=F()+TempPair.F();
            }
            else if( typeid(T1)==typeid(char*) )
            {
                char* Temp=F()+TempPair.F();
                ReturnPair.a=Temp;
            }
            else //T1==Char
            {
                char Temp[2]; Temp[1]=F(); Temp[2]=TempPair.F();
                char* ToReturn=Temp;
                ReturnPair.a=ToReturn;
            }

            if( (typeid(T2)!=typeid(char))&& (typeid(T2)!=typeid(char*)) )
            {
                ReturnPair.b=S()+TempPair.S();
            }
            else if( typeid(T2)==typeid(char*) )
            {
                char* Temp=S()+TempPair.S();
                ReturnPair.b=Temp;
            }
            else //T2==Char
            {
                char Temp[2]; Temp[1]=S(); Temp[2]=TempPair.S();
                char* ToReturn=Temp;
                ReturnPair.b=ToReturn;
            }

            return ReturnPair;
        }
        //else{/*Throw error stating: can not add bools*/    /*}

    }*/

    Pair<T1,T2> operator- (Pair<T1,T2> TempPair)
    {
        if(
            (typeid(T1)!=typeid(char))&&(typeid(T1)!=typeid(bool)) &&
            (typeid(T2)!=typeid(char))&&(typeid(T2)!=typeid(bool))
          )
        {
            Pair<T1,T2> ReturnPair( F()-TempPair.F() , S()-TempPair.S());
            return ReturnPair;

        }
        //else{/*Throw error stating: can not add chars/bools*/}

    }


    Pair(T1 F,T2 S)
    {Initialized=true; a=F; b=S; NumberOfPairs++;}
    Pair(void)
    {Initialized=false; NumberOfPairs++;}
    ~Pair(void)
    {NumberOfPairs--;}

};



template <typename T3>
class TriPair
{
private:
    static int NumberOfTriPairs;

public:
    T1 a; T2 b; T3 c; bool Initialized;

    T1 F(void) //First
    {
        if (Initialized==true) return a;
        return '\0';
    }
    T2 S(void) //Second
    {
        if (Initialized==true)
            return b;
        return '\0';
    }
    T3 T(void) //Third
    {
        if (Initialized==true)
            return c;
        return '\0';
    }

    int Population(void)
    {return NumberOfTriPairs;}

    TriPair(void)
    {Initialized=false; NumberOfTriPairs++;}
    TriPair(T1 F, T2 S, T3 T)
    {Initialized=true; a=F; b=S; c=T; NumberOfTriPairs++;}
    ~TriPair(void)
    {NumberOfTriPairs--;}

};

int Pair<T1,T2>::NumberOfPairs=0;

int TriPair<T1, T2, T3>::NumberOfTriPairs=0;

int main()
{
    /*  TESTING PURPOSES*/
    Pair<int,int> G(5,4),I(3,3),Jo(-5,-3);
    printf("%i,%i",G.operator+(I),G.operator+(Jo));

}
  






The Errors I get: *
E:\...\Pairs[Template].cpp|158|error: 'T1' does not name a type|
E:\...\Pairs[Template].cpp|158|error: 'T2' does not name a type|
E:\...\Pairs[Template].cpp|160|error: 'T1' does not name a type|
E:\...\Pairs[Template].cpp|165|error: 'T2' does not name a type|
E:\...\Pairs[Template].cpp|183|error: expected ')' before 'F'|
E:\...\Pairs[Template].cpp|190|error: 'T1' was not declared in this scope|
E:\...\Pairs[Template].cpp|190|error: 'T2' was not declared in this scope|
E:\...\Pairs[Template].cpp|190|error: template argument 1 is invalid|
E:\...\Pairs[Template].cpp|190|error: template argument 2 is invalid|
E:\...\Pairs[Template].cpp|192|error: 'T1' was not declared in this scope|
E:\...\Pairs[Template].cpp|192|error: 'T2' was not declared in this scope|
E:\...\Pairs[Template].cpp|192|error: 'T3' was not declared in this scope|
E:\...\Pairs[Template].cpp|192|error: wrong number of template arguments (3, should be 1)|
E:\...\Pairs[Template].cpp|152|error: provided for 'template<class T3> class TriPair'|
E:\...\Pairs[Template].cpp||In function 'int main()':|
E:\...\Pairs[Template].cpp|198|error: cannot pass objects of non-trivially-copyable type 'class Pair<int, int>' through '...'|
E:\...\Pairs[Template].cpp|198|error: cannot pass objects of non-trivially-copyable type 'class Pair<int, int>' through '...'|
E:\...\Pairs[Template].cpp||In instantiation of 'Pair<T1, T2> Pair<T1, T2>::operator+(Pair<T1, T2>) [with T1 = int; T2 = int]':|
E:\...\Pairs[Template].cpp|198|required from here|
E:\...\Pairs[Template].cpp|45|error: invalid conversion from 'int' to 'char*' [-fpermissive]|
E:\...\Pairs[Template].cpp|46|error: invalid conversion from 'char*' to 'int' [-fpermissive]|
E:\...\Pairs[Template].cpp|52|error: invalid conversion from 'char*' to 'int' [-fpermissive]|
E:\...\Pairs[Template].cpp|61|error: invalid conversion from 'int' to 'char*' [-fpermissive]|
E:\...\Pairs[Template].cpp|62|error: invalid conversion from 'char*' to 'int' [-fpermissive]|
E:\...\Pairs[Template].cpp|68|error: invalid conversion from 'char*' to 'int' [-fpermissive]|
||=== Build finished: 22 errors, 2 warnings (0 minutes, 2 seconds) ===|





I'm using Code::blocks (programming program) and Windows 7 (although probably none of these matter :D)

*P.S. : I shortenned the paths from E:\a_folder\another_folder\Pairs[temp.].cpp
to the ones you see (for my reasons).



Any corrections for improovement is much apreciated!
Thanks in advance.
Btw, I searched already for "Operator overloading" etc, but didn't find any operator overloading in a mix with templates
Line 151 should be template <typename T1,typename T2, typename T3>
You have three parameterized types in a TriPair. The T1, T2 from the class Pair definition do not apply to a whole new class.
@booradley60, thanks for the notice. That's how my code was in the beggining, but that didn't work out, and after some more changes, Code::blocks reported that I had ALREADY declared T1 and T2 before :S ... what can one say..

Well although I thank you for helping me I also think (as and the compiler if I'm not mistaken) that I am using "template" correct, in the specific place (line 51). Also, it seems it wasn't the major problem.
Just tested with the compiler at < http://cpp.sh/ > (You can visit the page by pressing the grey object at the right of my code block. It's on the very top.

This is what I got:

151:11: error: declaration of ‘class T1’ 7:11: error: shadows template parm ‘class T1’ 151:24: error: declaration of ‘class T2’ 7:23: error: shadows template parm ‘class T2’ 190:5: error: extra qualification ‘Pair<T1, T2>::’ on member ‘NumberOfPairs’ [-fpermissive] 190:32: error: ‘int Pair<T1, T2>::NumberOfPairs’ conflicts with a previous declaration 11:16: note: previous declaration ‘int Pair<T1, T2>::NumberOfPairs’ 192:21: error: ‘T3’ was not declared in this scope 192:23: error: template argument 3 is invalid 200:1: error: expected ‘}’ at end of input In member function ‘Pair<T1, T2> Pair<T1, T2>::operator+(Pair<T1, T2>)’: 125:5: error: a function-definition is not allowed here before ‘{’ token 147:1: error: expected ‘}’ at end of input In member function ‘int Pair<T1, T2>::main()’: 200:1: warning: no return statement in function returning non-void [-Wreturn-type] At global scope: 200:1: error: expected unqualified-id at end of input
Topic archived. No new replies allowed.