advance problem 02

closed account (28poGNh0)
In this expamle everything seems to be fine

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
# include <iostream>
# include <map>
using namespace std;

class c_map
{
    map<int,int> classMap;

    public:
        c_map(map<int,int> &tmpMap){classMap = tmpMap;}

        void showMap(void)
        {
            map<int,int>::iterator iter;

            for(iter = classMap.begin() ;iter != classMap.end() ;iter++)
            {
                cout << iter->first << " " << iter->second << endl;
            }

        }
};

int main()
{
    map<int,int> myMap;

    myMap.insert(pair<int,int>(1,2));
    myMap.insert(pair<int,int>(3,4));
    myMap.insert(pair<int,int>(5,6));

    c_map firstC(myMap);
    firstC.showMap();

    return 0;
}


But when I turn this class to template class things goes bad ,can you tell me why please?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
template<class T,class T2>
class c_map
{
    map<T,T2> classMap;

    public:
        c_map(map<T,T2> &tmpMap){classMap = tmpMap;}

        void showMap(void)
        {
            map<int,int>::iterator iter;

            for(iter = classMap.begin() ;iter != classMap.end() ;iter++)
            {
                cout << iter->first << " " << iter->second << endl;
            }

        }
};


Thanks for reading
Last edited on
But when I turn this class to template class things goes bad ,can you tell me why please?


It helps if you're more specific. "Things go bad" or "it doesn't work" doesn't really give us any indication of what the problem is. Is it a compiler error? What are the errors? Does it compile but crash when you run it?


The only thing I can see that's wrong is this:

1
2
3
            map<int,int>::iterator iter; // <- your iter is map<int,int>

            for(iter = classMap.begin() ;iter != classMap.end() ;iter++)


You don't want map<int,int>::iterator here... you'd want map<T,T2>::iterator. Or better yet, just use auto:

1
2
3
//            map<int,int>::iterator iter; // <- get rid of this.  Add auto to below

            for(auto iter = classMap.begin() ;iter != classMap.end() ;iter++)
closed account (28poGNh0)
Thanks for reading and, I am sorry to give such a bad specification, I mean by "Things go bad" that it does not compile with these errors in code::blocks

C:\Documents and Settings\Techno01\Bureau\myProject\main.cpp|35|error: missing template arguments before 'firstC'|

C:\Documents and Settings\Techno01\Bureau\myProject\main.cpp|35|error: expected ';' before 'firstC'|

C:\Documents and Settings\Techno01\Bureau\myProject\main.cpp|36|error: 'firstC' was not declared in this scope|

what I am trying to do is to pass my myMap container ,but It seems that the compiler wants me to set aguments or something

also my compiler does not support c++11 to use auto(I think)

C:\Documents and Settings\Techno01\Bureau\myProject\main.cpp|35|error: missing template arguments before 'firstC'|


This error is exactly what it says. If this is a template class, you have to give it the types you want it to work with:

1
2
3
c_map firstC(myMap); // <- if cmap is a template, this won't work.

c_map<int,int> firstC(myMap); // <- try this instead 
closed account (28poGNh0)
You great @Disch

and Oh my god ,how did you get this number 10650 ,all respect hope you read my advance problem 01 and advance problem 03

Thanks alot
PM me links to the threads and I'll check them out.
Topic archived. No new replies allowed.