a template function promble!

I defined a template function call PrintSetBtIterator as show in the following
source code , the code seams to be correct , but the commplier just give me some error infomation.Can someone help me!


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

template<typename Container>
    void PrintSetByIterator(const Container & );

int main()
{
    std::set<int> myset;
    std::multiset<int> myMutiset;

    myset.insert(1);
    myset.insert(2);
    myset.insert(3);
    myset.insert(4);
    myset.insert(5);

    PrintSetByIterator(myset);

    return 0;
}


template<typename Container>
void PrintSetByIterator(const Container & _container)
{

    if(_container.size() == 0)
    {
        cout<<" { There is no data .} "<<endl;
        return;
    }

    cout<<" { ";
    Container::const_iterator temp_iterator = _container.begin();

    for ( ; temp_iterator != _container.end() ; temp_iterator ++)
    {
        cout<<*temp_iterator<<" ";
    }
    cout<<" } "<<endl;

    return ;
}

Here is the building message:

C:\Users\Administrator\C++_File\Set_Multiset_Test.cpp||In function 'void PrintSetByIterator(const Container&)':|
C:\Users\Administrator\C++_File\Set_Multiset_Test.cpp|36|error: expected ';' before 'temp_iterator'|
C:\Users\Administrator\C++_File\Set_Multiset_Test.cpp|38|error: 'temp_iterator' was not declared in this scope|
C:\Users\Administrator\C++_File\Set_Multiset_Test.cpp||In function 'void PrintSetByIterator(const Container&) [with Container = std::set<int, std::less<int>, std::allocator<int> >]':|
C:\Users\Administrator\C++_File\Set_Multiset_Test.cpp|19|instantiated from here|
C:\Users\Administrator\C++_File\Set_Multiset_Test.cpp|36|error: dependent-name 'Container::const_iterator' is parsed as a non-type, but instantiation yields a type|
C:\Users\Administrator\C++_File\Set_Multiset_Test.cpp|36|note: say 'typename Container::const_iterator' if a type is meant|
||=== Build finished: 3 errors, 0 warnings ===|
The compiler also gives you a clue in all that list of error related messages.
The clue is:
C:\Users\Administrator\C++_File\Set_Multiset_Test.cpp|36|error: dependent-name 'Container::const_iterator' is parsed as a non-type, but instantiation yields a type
C:\Users\Administrator\C++_File\Set_Multiset_Test.cpp|36|note: say 'typename Container::const_iterator' if a type is meant



change line 36 to:
typename Container::const_iterator temp_iterator = _container.begin();
Last edited on
Thanks guestgulkan! It works. But I don't understand why is that?
The syntax

 
identifier1 :: identifier2


when parsed by the compiler, could be interpreted two different ways:

identifier1 could be a type (like a class, struct, or namespace), and identifier2 could
be a static data member inside that class, struct, or namespace. In any event,
identifier2 is a variable in this case and the expression is a variable.

or

identifier1 could be a type (like a class, struct, or namespace), and identifier2 could
be a second type (class or struct only). In this case, the expression is a type.

Although the compiler knows which one it is (by looking up the definition of identifier1
and identifier2 in its symbol table), if it names a type, it wants you to tell it that it is
a type. This is for your convenience, because the compiler just wants to know that
that is what you meant.

To tell it that it names a type, you prefix the expression with the keyword "typename"
as guestgulkan showed.
Topic archived. No new replies allowed.