Topic: Is this old code standard compliant???
Author: Alex Vinokur <alexander.vinokur@telrad.co.il>
Date: 1999/03/24 Raw View
In article <36ee76ea.65895745@news.tamu.edu>,
j-perdue@tamu.edu (Jack Perdue) wrote:
>
>
> Howdy all,
>
> Can anyone shed some light on the following?
> Is the user trying to do something that the standard
> no longer allows or is HP's latest compiler incorrect?
>
> It's just a warning at this point, but it looks like
> it will be promoted to an error in the future if
> something isn't changed.
>
> The problems seems to be Vector3 not getting
> "v" from VectorConstantSize.
>
[snip]
> ------------------------------------------------------------------------
>
> #include <vector>
>
> template <int SIZE,class ELEMENT>
> class VectorConstantSize{
> public:
> VectorConstantSize(){};
> VectorConstantSize(const ELEMENT&_elt){
> v.reserve(SIZE);
> for(int i=0;i<SIZE;++i)
> v.push_back(_elt);
> };
> void printit(){
> cout << "\n";
> for(int i=0;i<SIZE;++i)
> cout << v[i] << " ";
> }
> protected:
> vector<ELEMENT> v;
> };
>
> template <class ELEMENT>
> class Vector3 : public VectorConstantSize<3,ELEMENT>{
> public:
> Vector3(){};
> Vector3(const ELEMENT&_elt)
> :VectorConstantSize<3,ELEMENT>(_elt){};
>
> #ifndef PROBLEMCHILD // WHY DOESN'T THIS WORK??? - begin
> Vector3(const ELEMENT &_x,const ELEMENT &_y,const ELEMENT &_z)
> :VectorConstantSize<3,ELEMENT>(){
> if(v.size() == 0) {
> v.push_back(_x); v.push_back(_y); v.push_back(_z);
> } else if(v.size() == 3) {
> v[0] = _x; v[1] = _y; v[2] = _z;
> } else {
> cout << " Error in Vector3D(x,y,z) constructor " << endl;
> exit(-1);
> }
> };
> #endif // WHY DOESN'T THIS WORK??? - end
>
> };
>
> int main(void){
> VectorConstantSize<6,double> a(20);
> Vector3<double> b(99);
>
> a.printit();
> b.printit();
>
> cout << "\n\n";
> }
>
[snip]
Hi,
Here's a relevant example.
Alex.
#include <vector>
//=====================================
template <int SIZE, class ELEMENT>
class BASE_CLASS
{
public :
BASE_CLASS () {}
protected:
vector<ELEMENT> vb;
};
//=====================================
template <class ELEMENT>
class DERIVED_CLASS : public BASE_CLASS<3, ELEMENT>
{
public:
DERIVED_CLASS() : BASE_CLASS<3, ELEMENT> ()
{
//#################################
// cout << vb.size() << endl;
// aCC doesn't compile it
// egcs compiles it OK
//#################################
//#################################
cout << BASE_CLASS<3, ELEMENT>::vb.size() << endl;
// aCC compiles it OK
// egcs compiles it OK
//#################################
//#################################
cout << (*this).vb.size() << endl;
// aCC compiles it OK
// egcs compiles it OK
//#################################
//#################################
cout << this->vb.size() << endl;
// aCC compiles it OK
// egcs compiles it OK
//#################################
}
};
//========================
int main(void)
{
BASE_CLASS <6, double> a;
DERIVED_CLASS <double> b;
return 0;
}
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1999/03/18 Raw View
Jack Perdue wrote:
>
> Howdy all,
>
> Can anyone shed some light on the following?
> Is the user trying to do something that the standard
> no longer allows or is HP's latest compiler incorrect?
>
> It's just a warning at this point, but it looks like
> it will be promoted to an error in the future if
> something isn't changed.
>
> The problems seems to be Vector3 not getting
> "v" from VectorConstantSize.
>
> As mentioned, this used to work.
[...]
> #include <vector>
>
> template <int SIZE,class ELEMENT>
> class VectorConstantSize{
[...]
> protected:
> vector<ELEMENT> v;
> };
>
> template <class ELEMENT>
> class Vector3 : public VectorConstantSize<3,ELEMENT>{
> public:
> Vector3(){};
> Vector3(const ELEMENT&_elt)
> :VectorConstantSize<3,ELEMENT>(_elt){};
>
> #ifndef PROBLEMCHILD // WHY DOESN'T THIS WORK??? - begin
> Vector3(const ELEMENT &_x,const ELEMENT &_y,const ELEMENT &_z)
> :VectorConstantSize<3,ELEMENT>(){
> if(v.size() == 0) {
[...]
> };
> #endif // WHY DOESN'T THIS WORK??? - end
>
> };
[...]
I don't really know if it is legal (there are AFAIK now some cases
where symbols from template argument dependant base classes are not
found; I'm not sure if that is one such case), but I want to suggest
a possible solution:
Try adding
protected:
using VectorConstantSize<3,ELEMENT>::v;
to the beginning of the Vector3 class template definition.
I don't have a HP compiler, so I can't test if it does work.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/03/18 Raw View
Jack Perdue wrote:
> The problems seems to be Vector3 not getting
> "v" from VectorConstantSize.
Exactly
> As mentioned, this used to work.
This used to be correct.
> % aCC -v mini.C
> Error (future) 641: "mini.C", line 42 # Undeclared variable 'v'. Did you
> forget to make the variable dependent on the template type
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> parameters so
^^^^^^^^^^
> it would be looked up when templates are generated?
> #include <vector>
>
> template <
> class ELEMENT>
> class VectorConstantSize{
> protected:
> vector<ELEMENT> v;
missing std:: here
> };
>
> template <class ELEMENT>
> class Vector3 : public VectorConstantSize<
> ELEMENT>{
> Vector3(
> )
> {
> v;
> }
I have minimised the code size to make things clear.
Here v isn't looked up in templated base classes (only in non
template base classes - but there aren't any); v is looked up
in Vector3 and (then) at global scope.
Only dependant names are looked up in template base classes, so
you need for ex. to write this->v instead of v (or Vector3::v,
but this one doesn't work well with functions).
--
Valentin Bonnard
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: j-perdue@tamu.edu (Jack Perdue)
Date: 1999/03/16 Raw View
Howdy all,
Can anyone shed some light on the following?
Is the user trying to do something that the standard
no longer allows or is HP's latest compiler incorrect?
It's just a warning at this point, but it looks like
it will be promoted to an error in the future if
something isn't changed.
The problems seems to be Vector3 not getting
"v" from VectorConstantSize.
As mentioned, this used to work.
TIA,
jack (template newbie)
j-perdue@tamu.edu
User has code which was working but now gives (after installing
HP's 2/99 release of aCC) the warnings/errors below.
We need to know what the problem is.
% aCC -v mini.C
Error (future) 641: "mini.C", line 42 # Undeclared variable 'v'. Did you
forget to make the variable dependent on the template type
parameters so
it would be looked up when templates are generated?
if(v.size() == 0) {
^
Error (future) 641: "mini.C", line 43 # Undeclared variable 'v'. Did you
forget to make the variable dependent on the template type
parameters so
it would be looked up when templates are generated?
v.push_back(_x); v.push_back(_y); v.push_back(_z);
^
Error (future) 641: "mini.C", line 43 # Undeclared variable 'v'. Did you
forget to make the variable dependent on the template type
parameters so
it would be looked up when templates are generated?
v.push_back(_x); v.push_back(_y); v.push_back(_z);
^
Error (future) 641: "mini.C", line 43 # Undeclared variable 'v'. Did you
forget to make the variable dependent on the template type
parameters so
it would be looked up when templates are generated?
v.push_back(_x); v.push_back(_y); v.push_back(_z);
^
Error (future) 641: "mini.C", line 44 # Undeclared variable 'v'. Did you
forget to make the variable dependent on the template type
parameters so
it would be looked up when templates are generated?
} else if(v.size() == 3) {
^
Error (future) 641: "mini.C", line 45 # Undeclared variable 'v'. Did you
forget to make the variable dependent on the template type
parameters so
it would be looked up when templates are generated?
v[0] = _x; v[1] = _y; v[2] = _z;
^
Error (future) 641: "mini.C", line 45 # Undeclared variable 'v'. Did you
forget to make the variable dependent on the template type
parameters so
it would be looked up when templates are generated?
v[0] = _x; v[1] = _y; v[2] = _z;
^
Error (future) 641: "mini.C", line 45 # Undeclared variable 'v'. Did you
forget to make the variable dependent on the template type
parameters so
it would be looked up when templates are generated?
v[0] = _x; v[1] = _y; v[2] = _z;
^
Warning: 8 future errors were detected and ignored. Add a '+p'
option to
detect and fix them before they become fatal errors in a future release.
Behavio
r of this ill-formed program is not guaranteed to match that of a
well-formed pr
ogram
/usr/ccs/bin/ld -o a.out /opt/langtools/lib/crt0.o -u ___exit -u main
-L /opt/a
CC/lib /opt/aCC/lib/cpprt0.o mini.o -lstd -lstream -lCsup -lm -lcl -lc
/usr/lib/
libdld.sl >/var/tmp/AAAa03676 2>&1
removing /var/tmp/AAAa03676
removing mini.o
------------------------------------------------------------------------
#include <vector>
template <int SIZE,class ELEMENT>
class VectorConstantSize{
public:
VectorConstantSize(){};
VectorConstantSize(const ELEMENT&_elt){
v.reserve(SIZE);
for(int i=0;i<SIZE;++i)
v.push_back(_elt);
};
void printit(){
cout << "\n";
for(int i=0;i<SIZE;++i)
cout << v[i] << " ";
}
protected:
vector<ELEMENT> v;
};
template <class ELEMENT>
class Vector3 : public VectorConstantSize<3,ELEMENT>{
public:
Vector3(){};
Vector3(const ELEMENT&_elt)
:VectorConstantSize<3,ELEMENT>(_elt){};
#ifndef PROBLEMCHILD // WHY DOESN'T THIS WORK??? - begin
Vector3(const ELEMENT &_x,const ELEMENT &_y,const ELEMENT &_z)
:VectorConstantSize<3,ELEMENT>(){
if(v.size() == 0) {
v.push_back(_x); v.push_back(_y); v.push_back(_z);
} else if(v.size() == 3) {
v[0] = _x; v[1] = _y; v[2] = _z;
} else {
cout << " Error in Vector3D(x,y,z) constructor " << endl;
exit(-1);
}
};
#endif // WHY DOESN'T THIS WORK??? - end
};
int main(void){
VectorConstantSize<6,double> a(20);
Vector3<double> b(99);
a.printit();
b.printit();
cout << "\n\n";
}
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 1999/03/18 Raw View
In article <36ee76ea.65895745@news.tamu.edu>,
j-perdue@tamu.edu (Jack Perdue) wrote:
>
>
> Howdy all,
>
> Can anyone shed some light on the following?
> Is the user trying to do something that the standard
> no longer allows or is HP's latest compiler incorrect?
>
> It's just a warning at this point, but it looks like
> it will be promoted to an error in the future if
> something isn't changed.
>
> The problems seems to be Vector3 not getting
> "v" from VectorConstantSize.
FWIW, MSVC5 compiles it OK (after adding #include <iostream> and std::
prefixes) and it looks OK to me. I'll be the first to admit, though,
templates are my weak point in C++ :-)
[snip]
Jim
Note to recruitment agencies: I will not refer my friends or colleagues
to you nor do I want to use your services to find me a job. I stop
reading unsolicited email as soon as I determine it is job-recruitment
-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]