Differencies using templates in MSVC and g++

Just ran into weird problem with templates while porting code from Windows to Linux.

Provided sample compiles with MSVC and works flawlessly.

Code sample:

#include <vector>
#include <iostream>

typedef std::vector<int> IntArray;

template <typename TArray>
void printArray(TArray a)
{
    typedef TArray::iterator iter;
    for (iter it = a.begin(); it != a.end(); ++it)
        std::cout << *it << std::endl;
}

int main()
{
    IntArray a;
    a.push_back(1);
    a.push_back(2);
    a.push_back(3);

    printArray(a);
    return 0;
}

But g++ complains that

In function ‘void printArray(TArray)’:
bar.cpp:9: error: expected initializer before ‘iter’
bar.cpp:10: error: ‘iter’ was not declared in this scope
bar.cpp:10: error: expected ‘;’ before ‘it’
bar.cpp:10: error: ‘it’ was not declared in this scope


Download differencies.using.templates.in.msvc.and.g.zip
Direct Link


Download


Download differencies.using.templates.in.msvc.and.g.zip
Mediafire


Download


Download differencies.using.templates.in.msvc.and.g.zip
Gboxes


Download


Download differencies.using.templates.in.msvc.and.g.zip
Privatefiles


Download


Download differencies.using.templates.in.msvc.and.g.zip
BOX.com


Download

Both comments and pings are currently closed.

Comments are closed.