Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > compilation error related to template parameter

Reply
Thread Tools

compilation error related to template parameter

 
 
subramanian100in@yahoo.com, India
Guest
Posts: n/a
 
      08-14-2007
Consider the following program:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

template<class T> class Vec : public vector<T>
{
public:
Vec() : vector<T>() { }

Vec(int s) : vector<T>(s) { }

T& operator[] (int i) { return at(i); }
};

int main()
{
Vec<string> s(3);

return 0;
}

Suppose this program is named as y.cpp

When this program is compiled under Redhat Enterprise Linux
Workstation, as
g++ -std=c++98 -pedantic -Wall -Wextra y.cpp

I am getting the following compilation error:

y.cpp: In member function `T& Vec<T>:perator[](int)':
y.cpp:14: error: there are no arguments to `at' that depend on a
template parameter, so a declaration of `at' must be available

However under VC++ Express Edition 2005, it compiles well without any
warning or error.

Kindly explain what is wrong with the above program and help me in
fixing the compilation error with g++ under Linux

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      08-14-2007
http://www.velocityreviews.com/forums/(E-Mail Removed), India wrote:
> Consider the following program:
>
> #include <iostream>
> #include <string>
> #include <vector>
>
> using namespace std;
>
> template<class T> class Vec : public vector<T>

generally not a good idea. vector is not intended for inheritance.

> {
> public:
> Vec() : vector<T>() { }
>
> Vec(int s) : vector<T>(s) { }
>
> T& operator[] (int i) { return at(i); }


T& operator[](int i) { return this->at(i); }
> };
>
> int main()
> {
> Vec<string> s(3);
>
> return 0;
> }
> y.cpp: In member function `T& Vec<T>:perator[](int)':
> y.cpp:14: error: there are no arguments to `at' that depend on a
> template parameter, so a declaration of `at' must be available



See FAQ 35.19:
http://www.parashift.com/c++-faq-lit...html#faq-35.19
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-14-2007
red floyd wrote:
> (E-Mail Removed), India wrote:
>> Consider the following program:
>>
>> #include <iostream>
>> #include <string>
>> #include <vector>
>>
>> using namespace std;
>>
>> template<class T> class Vec : public vector<T>

> generally not a good idea. vector is not intended for inheritance.


Tell me this is not another - "need virtual destructor so you can't
inherit" - argument. If it is, you will find it's not a position of
consensus.

>
>> {
>> public:
>> Vec() : vector<T>() { }
>>
>> Vec(int s) : vector<T>(s) { }
>>
>> T& operator[] (int i) { return at(i); }

>
> T& operator[](int i) { return this->at(i); }
>> };
>>
>> int main()
>> {
>> Vec<string> s(3);
>>
>> return 0;
>> }
>> y.cpp: In member function `T& Vec<T>:perator[](int)':
>> y.cpp:14: error: there are no arguments to `at' that depend on a
>> template parameter, so a declaration of `at' must be available

>
>
> See FAQ 35.19:
> http://www.parashift.com/c++-faq-lit...html#faq-35.19

 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-14-2007
* (E-Mail Removed), India:
> Consider the following program:
>
> #include <iostream>
> #include <string>
> #include <vector>
>
> using namespace std;
>
> template<class T> class Vec : public vector<T>
> {
> public:
> Vec() : vector<T>() { }
>
> Vec(int s) : vector<T>(s) { }
>
> T& operator[] (int i) { return at(i); }
> };
>
> int main()
> {
> Vec<string> s(3);
>
> return 0;
> }
>
> Suppose this program is named as y.cpp
>
> When this program is compiled under Redhat Enterprise Linux
> Workstation, as
> g++ -std=c++98 -pedantic -Wall -Wextra y.cpp
>
> I am getting the following compilation error:
>
> y.cpp: In member function `T& Vec<T>:perator[](int)':
> y.cpp:14: error: there are no arguments to `at' that depend on a
> template parameter, so a declaration of `at' must be available
>
> However under VC++ Express Edition 2005, it compiles well without any
> warning or error.
>
> Kindly explain what is wrong with the above program and help me in
> fixing the compilation error with g++ under Linux


As an alternative to Red Floyd's suggestion (else-thread) of

this->at( i )

you can add in the class

using vector<T>::at;

For the compiler needs to be informed -- somehow -- that "at" is meant
to be assumed to be a member function of vector<T>. Because whether it
is or not depends on T, which is unknown during the first pass through
the template definition. For example, "at" could be a global function.

<speculation>
I always find the explanation I gave above to be lacking, but it is the
one usually offered. It's a bit lacking because before two-phase
template instantiation was standardized, compilers managed code such as
above very well thank you, as evidently Visual C++ still does, without
needing to be informed about anything. A more Real(TM) reason why it
matters could be that "export" needs a context-independent template
definition (that could also help explain why "export" is so unpopular,
only officially supported by Comeau).
</speculation>

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Premal
Guest
Posts: n/a
 
      08-14-2007
On Aug 14, 11:02 am, "(E-Mail Removed), India"
<(E-Mail Removed)> wrote:
> Consider the following program:
>
> #include <iostream>
> #include <string>
> #include <vector>
>
> using namespace std;
>
> template<class T> class Vec : public vector<T>
> {
> public:
> Vec() : vector<T>() { }
>
> Vec(int s) : vector<T>(s) { }
>
> T& operator[] (int i) { return at(i); }
>
> };
>
> int main()
> {
> Vec<string> s(3);
>
> return 0;
>
> }
>
> Suppose this program is named as y.cpp
>
> When this program is compiled under Redhat Enterprise Linux
> Workstation, as
> g++ -std=c++98 -pedantic -Wall -Wextra y.cpp
>
> I am getting the following compilation error:
>
> y.cpp: In member function `T& Vec<T>:perator[](int)':
> y.cpp:14: error: there are no arguments to `at' that depend on a
> template parameter, so a declaration of `at' must be available
>
> However under VC++ Express Edition 2005, it compiles well without any
> warning or error.
>
> Kindly explain what is wrong with the above program and help me in
> fixing the compilation error with g++ under Linux


Hi,
you can try following one:
rather than using return at(i) use the following one:
return vector<T>::at(i)

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-14-2007
(E-Mail Removed) wrote:
> Consider the following program:
>
> #include <iostream>
> #include <string>
> #include <vector>
>
> using namespace std;
>
> template<class T> class Vec : public vector<T>
> {
> public:
> Vec() : vector<T>() { }
>
> Vec(int s) : vector<T>(s) { }
>
> T& operator[] (int i) { return at(i); }
> };
>
> int main()
> {
> Vec<string> s(3);
>
> return 0;
> }
>
> Suppose this program is named as y.cpp
>
> When this program is compiled under Redhat Enterprise Linux
> Workstation, as
> g++ -std=c++98 -pedantic -Wall -Wextra y.cpp
>
> I am getting the following compilation error:
>
> y.cpp: In member function `T& Vec<T>:perator[](int)':
> y.cpp:14: error: there are no arguments to `at' that depend on a
> template parameter, so a declaration of `at' must be available
>
> However under VC++ Express Edition 2005, it compiles well without any
> warning or error.
>
> Kindly explain what is wrong with the above program and help me in
> fixing the compilation error with g++ under Linux


My newsserver is slow today, so I may be replying to something that
has already been discussed, sorry for that.

In your case 'at' is a dependent name. You need to help the compiler
to find it by pointing it to the member function. There are two ways
to do that, a 'using' declaration and an explicit qualification of
the name. So, either do

template<class T> class Vec : public vector<T>
{
using vector<T>::at;
public:
Vec() : vector<T>() { }

Vec(int s) : vector<T>(s) { }

T& operator[] (int i) { return at(i); }
};

or


template<class T> class Vec : public vector<T>
{
using vector<T>::at;
public:
Vec() : vector<T>() { }

Vec(int s) : vector<T>(s) { }

T& operator[] (int i) { return this->at(i); }
// or
// T& operator[] (int i) { return vector<T>->at(i); }
};

Next time read the FAQ first.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-14-2007
On Aug 14, 8:22 am, Gianni Mariani <(E-Mail Removed)> wrote:
> red floyd wrote:
> > (E-Mail Removed), India wrote:
> >> Consider the following program:


> >> #include <iostream>
> >> #include <string>
> >> #include <vector>


> >> using namespace std;


> >> template<class T> class Vec : public vector<T>

> > generally not a good idea. vector is not intended for inheritance.


> Tell me this is not another - "need virtual destructor so you can't
> inherit" - argument. If it is, you will find it's not a position of
> consensus.


It's probably more simply a case of "using a class in ways it
was not designed for doesn't work". A fact of life. (But there
are always amateur hackers who try to ignore it.)

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      08-14-2007
James Kanze wrote:
> On Aug 14, 8:22 am, Gianni Mariani <(E-Mail Removed)> wrote:
>> red floyd wrote:
>>> (E-Mail Removed), India wrote:
>>>> Consider the following program:

>
>>>> #include <iostream>
>>>> #include <string>
>>>> #include <vector>

>
>>>> using namespace std;

>
>>>> template<class T> class Vec : public vector<T>
>>> generally not a good idea. vector is not intended for inheritance.

>
>> Tell me this is not another - "need virtual destructor so you can't
>> inherit" - argument. If it is, you will find it's not a position of
>> consensus.

>
> It's probably more simply a case of "using a class in ways it
> was not designed for doesn't work". A fact of life. (But there
> are always amateur hackers who try to ignore it.)


It's probably another attempt to impose a style (coding or design)
ridden with undue limitations. Condescending tone does make one
look/sound important, no doubt.

Search the archives for "standard containers are not intended to be
derived from" and make your own conclusions, that's what I say.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-14-2007
On Aug 14, 8:55 am, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * (E-Mail Removed), India:
>
>
>
> > Consider the following program:

>
> > #include <iostream>
> > #include <string>
> > #include <vector>

>
> > using namespace std;

>
> > template<class T> class Vec : public vector<T>
> > {
> > public:
> > Vec() : vector<T>() { }

>
> > Vec(int s) : vector<T>(s) { }

>
> > T& operator[] (int i) { return at(i); }
> > };

>
> > int main()
> > {
> > Vec<string> s(3);

>
> > return 0;
> > }

>
> > Suppose this program is named as y.cpp

>
> > When this program is compiled under Redhat Enterprise Linux
> > Workstation, as
> > g++ -std=c++98 -pedantic -Wall -Wextra y.cpp

>
> > I am getting the following compilation error:

>
> > y.cpp: In member function `T& Vec<T>:perator[](int)':
> > y.cpp:14: error: there are no arguments to `at' that depend on a
> > template parameter, so a declaration of `at' must be available

>
> > However under VC++ Express Edition 2005, it compiles well without any
> > warning or error.

>
> > Kindly explain what is wrong with the above program and help me in
> > fixing the compilation error with g++ under Linux

>
> As an alternative to Red Floyd's suggestion (else-thread) of
>
> this->at( i )
>
> you can add in the class
>
> using vector<T>::at;
>
> For the compiler needs to be informed -- somehow -- that "at" is meant
> to be assumed to be a member function of vector<T>. Because whether it
> is or not depends on T, which is unknown during the first pass through
> the template definition. For example, "at" could be a global function.
>
> <speculation>
> I always find the explanation I gave above to be lacking, but it is the
> one usually offered. It's a bit lacking because before two-phase
> template instantiation was standardized, compilers managed code such as
> above very well thank you, as evidently Visual C++ still does, without
> needing to be informed about anything. A more Real(TM) reason why it
> matters could be that "export" needs a context-independent template
> definition (that could also help explain why "export" is so unpopular,
> only officially supported by Comeau).
> </speculation>
>
> --
> A: Because it messes up the order in which people normally read text.
> Q: Why is it such a bad thing?
> A: Top-posting.
> Q: What is the most annoying thing on usenet and in e-mail?



 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      08-14-2007
Alf P. Steinbach wrote:
::
:: For the compiler needs to be informed -- somehow -- that "at" is
:: meant to be assumed to be a member function of vector<T>. Because
:: whether it is or not depends on T, which is unknown during the
:: first pass through the template definition. For example, "at"
:: could be a global function.
::
:: <speculation>
:: I always find the explanation I gave above to be lacking, but it
:: is the one usually offered. It's a bit lacking because before
:: two-phase template instantiation was standardized, compilers
:: managed code such as above very well thank you, as evidently
:: Visual C++ still does, without needing to be informed about
:: anything. A more Real(TM) reason why it matters could be that
:: "export" needs a context-independent template definition (that
:: could also help explain why "export" is so unpopular, only
:: officially supported by Comeau). </speculation>
::

It doesn't really work, it just pretends. Note that the OP used
"-std=c++98 -pedantic" with g++. That is definitely not the default
setting for VC++.

As you certainly know, in "compatibility mode" VC++ will pick whatever
it finds at instantiation time, a vector<mytype>::at() or some other
visible at() function, or just fail if none is present. I think this
is an ODR violation in disguise.

Isn't it better to fix the problem up front with the original
template, and not wait for the third case (or debug case two for a
month)?


Bo Persson


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compilation error with function template parameter subramanian100in@yahoo.com, India C++ 2 08-30-2010 01:48 AM
A workable scheme for "template parameter-conditional compilation"? Lionel B C++ 2 07-25-2008 01:15 PM
template template parameter compilation errors!!! aaragon C++ 6 03-23-2008 06:16 PM
Using declaration inside first template parameter as default valuefor second template parameter. Stuart Redmann C++ 5 12-14-2007 08:42 AM
Compilation error [sort of OT, qt related] Lionel van den Berg C++ 1 02-16-2005 02:19 AM



Advertisments