This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: type mismatch when compile 1999 code


Hi Eric,

On Thu, Jun 23, 2011 at 01:20:00AM -0700, eric wrote:
> On Thu, 2011-06-23 at 08:56 +0200, Axel Freyn wrote:
> > 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
> ---------------
> I tried to add <short> after allocator to become allocator<short>
> improve
> but still not compile
> this is result I get
> ------------
> eric@eric-laptop:~/CppPrimer3/download/chap17$ g++ Query.C UserQuery.C
> TextQuery.C text_query.C  
Well, here you're even compiling additional files which I don't have
(e.g. UserQuery.C). Nevertheless:
> In file included from UserQuery.C:1:0:
> UserQuery.h:46:68: error: wrong number of template arguments (3, should
> be 2)
> /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c
> ++/4.5.2/bits/stl_stack.h:92:11: error: provided for âtemplate<class
> _Tp, class _Sequence> class std::stackâ
The compiler says that in line 46 of UserQuery.h you use the template
std::stack, but you pass 3 parameter instead of 2. The C++-Standard says
that the Container adaptor std::stack need exactly 2 Parameters ==> Your
code is NO VALID C++.
The two parameters are first the data type to be stored in the stack,
and then the container to be used internally (which defaults to
std::deque<T>).
So, you should change this line's beginning to something like
std::stack< short, std::deque<short> > ...
(assuming you're storing short's in the stack).
However, you can also use the shorter version
std::stack< short > ...
> UserQuery.h:47:68: error: wrong number of template arguments (3, should
> be 2)
> /usr/local/lib/gcc/i686-pc-linux-gnu/4.5.2/../../../../include/c
> ++/4.5.2/bits/stl_stack.h:92:11: error: provided for âtemplate<class
> _Tp, class _Sequence> class std::stackâ
That the same Problem: std::stack only allows 2 template parameters, you
pass 3 in UserQuery.h, line 47.

> UserQuery.h: In member function âvoid UserQuery::evalAnd()â:
> UserQuery.h:73:28: error: request for member âtopâ in
> â((UserQuery*)this)->UserQuery::_query_stackâ, which is of non-class
> type âintâ
This is probably a follow-up error which will disappear as soon as you
corrected the two lines in UserQuery.h: As the compiler did non
understand the definition of the stack, it can't call the
member-function "top" here.
You'll have MANY errors like that :-)

> -------------------------------------------------
> this is only first couple lines of errors, it still have a lot
> is stack(template) is we defined or system(compiler) defined? why it
> must need 2 in stead of 3?  so should I eliminate last parameter?
Stack is defined by the C++-Standard, and your usage is illegal. Only 1
or 2 parameters are allowed. Probably you should eliminate the last
parameter (i don't know which parameters you're using, so I can't say
which one is the wrong :-))
> and how about other error?  
Probably, the others are only appearing as Gcc does not understand the
definition of your stack. Probably they will disappear as soon as you
corrected lines 46 and 47 of UserQuery.h

Axel


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]