On Aug 14, 8:55 am, "Alf P. Steinbach" <al...@start.no> wrote:
* subramanian10...@yahoo.com, 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>::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
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?