Re: type mismatch when compile 1999 code

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

 



Hi Eric,

On Wed, Jun 22, 2011 at 11:05:33PM -0700, eric wrote:
> Dear gcc programers:
> 
>   I tried to copy (from internet download book's example source code,
> from
> http://comscigate.com/BookCode/cppbooks.htm
> on C++ Primer 3rd Ed, written by Lippman , Lajoie
> that source code is written before 1999 and I guess is compiled based on
> borland's compiler
> I tried to compile/run chap17,  text_query.C
> attach 4 related files(I already modify a little bit, ie replace
> iostream.h by iostream and add using namespace std;)
> ------------------
> eric@eric-laptop:~/CppPrimer3/download/chap17$ g++ Query.C text_query.C
> In file included from Query.C:1:0:
> Query.h:27:41: error: type/value mismatch at argument 3 in template
> parameter list for ‘template<class _Key, class _Compare, class _Alloc>
> class std::set’
> Query.h:27:41: error:   expected a type, got ‘allocator’
> Query.h:28:36: error: type/value mismatch at argument 2 in template
> parameter list for ‘template<class _Tp, class _Alloc> class std::vector’
> Query.h:28:36: error:   expected a type, got ‘allocator’
> ----------
> this is just first 4 errors of the whole(a lot).
> /* which I believe is gcc specific(4.5.2), so I decide not post to
> comp.lang.c++ */
No, it's a problem in your code. GCC is absolutely right.

GCC states that in Query.h line 27 you're using "allocator" as a template
parameter. At this position, you would need a type, but "allocator" is
no type (but nevertheless: allocator is some object/word known to the
compiler).

The first line in question is the following:
> 	const set< short,less<short>,allocator >    *solution();
Due to your "using namespace std;" the word allocator is resolved to the
standard template "std::allocator" defined in <memory>.
However, std::allocator is a TEMPLATE, so you have to pass the type of
variable which you want to allocate: 
> 	const set< short,less<short>,allocator<short> >    *solution();
allocator<short> IS a type, so gcc will be happy :-)

(and of course you need equivalent changes throughout your code).

On the other hand: if you don't pass a third parameter to std::set, it
will use by default std::allocator<T> (with T being the appropriate
type). The same is true for the second template parameter: std::set
defaults to use std::less<T> for comparisons.
So, you can just replace this line by
> 	const set< short >    *solution();
which will give you the same behaviour.


HTH,

Axel


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux