help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A problem with the g++ and template function


From: gx
Subject: A problem with the g++ and template function
Date: Mon, 31 Mar 2008 12:55:32 -0700 (PDT)
User-agent: G2/1.0

Hi Everyone,
I've got a problem with the g++ compiler (to be specific I've
encountered that problem both on the GNU/Linux (Gentoo distribution)
(gcc-4.1.2) and Microsoft Windows (XP SP2) platform (minigw32)).

The source code is listed below (you're also able to download it from
this location: http://pablik.ic.cz/data/pokus2.cpp, at least until
somebody won't delete it):

#include <iostream>
#include <vector>

template<class T> T mmax(const T a,const T b){
        return a<b?b:a;
}

template<class T> void app(std::vector<T> &v, T i){
        v.push_back(i);
}

template<class T> bool comp(std::vector<T> &a, std::vector<T> &b){
        return a[0]>b[0];
}

template<class T> bool find0(const std::vector<T> &v){
        for(std::vector<T>::const_iterator it=v.begin(); it!=v.end(); +
+it){
                if(!*it)
                        return true;
        }
        return false;
}

int main(int argc, char* argv[]){

        std::cout<< mmax(3.2,1.0) <<std::endl;

        std::vector<double> x;
        x.push_back(1.2);
        app(x,4.2);

        std::cout<< x[1] <<std::endl;

        std::vector<double> y;
        y.push_back(0.2);
        std::cout<< comp(x,y) <<std::endl;

        std::cout<< find0(x) <<std::endl;
        return 0;
}


I believe that the problem is in the definition of template function.

When I try to compile a source code with g++/minigw32-g++ I obtain
this output:

$ g++ -Wall -pedantic pokus2.cpp
pokus2.cpp: In function 'bool find0(const std::vector<T,
std::allocator<_CharT> >&)':
pokus2.cpp:17: error: expected `;' before 'it'
pokus2.cpp:17: error: 'it' was not declared in this scope
pokus2.cpp: In function 'bool find0(const std::vector<T,
std::allocator<_CharT> >&) [with T = double]':
pokus2.cpp:38:   instantiated from here
pokus2.cpp:17: error: dependent-name
'std::vector<T,std::allocator<_CharT> >::const_iterator' is parsed as
a non-type, but instantiation yields a type
pokus2.cpp:17: note: say 'typename
std::vector<T,std::allocator<_CharT> >::const_iterator' if a type is
meant

I would gladly welcome your replies.
Thank you


reply via email to

[Prev in Thread] Current Thread [Next in Thread]