473,378 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

compilation error related to template parameter

Consider the following program:

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

using namespace std;

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

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

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

int main()
{
Vec<strings(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>::operator[](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

Aug 14 '07 #1
9 1958
su**************@yahoo.com, India wrote:
Consider the following program:

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

using namespace std;

template<class Tclass 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<strings(3);

return 0;
}
y.cpp: In member function `T& Vec<T>::operator[](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
Aug 14 '07 #2
red floyd wrote:
su**************@yahoo.com, India wrote:
>Consider the following program:

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

using namespace std;

template<class Tclass 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<strings(3);

return 0;
}
y.cpp: In member function `T& Vec<T>::operator[](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
Aug 14 '07 #3
On Aug 14, 11:02 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
Consider the following program:

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

using namespace std;

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

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

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

};

int main()
{
Vec<strings(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>::operator[](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)

Aug 14 '07 #4
su**************@yahoo.com wrote:
Consider the following program:

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

using namespace std;

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

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

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

int main()
{
Vec<strings(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>::operator[](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 Tclass 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 Tclass 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
Aug 14 '07 #5
On Aug 14, 8:22 am, Gianni Mariani <gi3nos...@mariani.wswrote:
red floyd wrote:
subramanian10...@yahoo.com, India wrote:
Consider the following program:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template<class Tclass 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:ja*********@gmail.com
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

Aug 14 '07 #6
James Kanze wrote:
On Aug 14, 8:22 am, Gianni Mariani <gi3nos...@mariani.wswrote:
>red floyd wrote:
>>subramanian10...@yahoo.com, India wrote:
Consider the following program:
>>>#include <iostream>
#include <string>
#include <vector>
>>>using namespace std;
>>>template<class Tclass 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
Aug 14 '07 #7
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
Aug 14 '07 #8

Victor Bazarov <v.********@comAcast.netwrote in message...
>
My newsserver is slow today,
I've noticed that too. I wonder if it has anything to do with dictator
bush's plan to monitor everything going into/out_of the U.S.A.. (that seems
to be about when it started.)

--
Bob R
POVrookie
Aug 14 '07 #9
BobR wrote:
Victor Bazarov <v.********@comAcast.netwrote in message...
>>
My newsserver is slow today,

I've noticed that too. I wonder if it has anything to do with dictator
bush's plan to monitor everything going into/out_of the U.S.A.. (that
seems to be about when it started.)
<shrug Then I'd see slowness on other sources as well, and I didn't.

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Saulius | last post by:
template<template<typename T> class StoragePolicy> class construction_info { // Empty by default, specialize for other types. }; template<template<typename> class StoragePolicy, typename...
2
by: bigfaceworm | last post by:
I had what I thought was proper SFINAE code. Two declarations of a function, one more general, one more specific. The code compiles just fine on gcc 3.3, but fails on 3.4 and 3.4.2. What is...
6
by: Raja Koduru | last post by:
Hello everybody, I am not yet fully comfortable with understanding c++ warnings/error messages. Very recently I have updated my IDE to VC++7.0 (MS.Net 7.0-Enterprise architect) from vc++6....
2
by: AlexW | last post by:
Hi, I have error of compilation when I compile below template classes in xcode 2.4.1 on MacOSX10.4.8. This xcode uses GCC 4.0.1 error: there are no arguments to 'SetLeft' that depend on a...
9
by: Jerome Durand | last post by:
Hello, I'm trying to write something along the following lines but I cannot get this to compile. template <typename derivedstruct Base { typedef typename derived::valueType valueType;...
7
by: =?iso-8859-2?q?Seweryn_Habdank-Wojew=F3dzki?= | last post by:
Hi There is an example of the code below (mostly written by P. Bindels). In that example is strongly highlighted that very important is the sequence of compiled code. In C++ could be assume...
3
by: krunalb | last post by:
hi, I am facing compilation error for following snippet: Compilation Error: t7.cc:17: error: expected `)' before '*' token <code snippet>
1
by: BSand0764 | last post by:
I'm getting an error that I can't seem to resolve. When I compile the Functor related logic in a test program, the files compile and execute properly (see Listing #1). However, when I...
2
by: Lionel B | last post by:
I frequently seem to run into the following annoyance regarding template class specialisation: I have a template class which implements, for a general template parameter, some basic functionality...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.