Topic: Simple but puzzling template problem
Author: Jonathan de Boyne Pollard <J.deBoynePollard@tesco.net>
Date: 2000/05/27 Raw View
RdeSB> The following program only compiles (BCB) in a certain
RdeSB> odd condition. If I have the global variable
RdeSB> 'number_of_students_per_class', it DOESN't recognize the
RdeSB> template operator>> defined. If I comment the variable out,
RdeSB> though, it does recognize the operator>>. But those things
RdeSB> shouldn't have to do with each other, so what's going on?!?
RdeSB>
RdeSB> #include <vector>
RdeSB> [...omitted...]
RdeSB> void main()
RdeSB> {
RdeSB> // whatever
RdeSB> }
Given that you posted this to comp.std.c++, one has to take this as a C++
language standard question. In which case the answer is that you used `void'
as the return type of `main', your program is thus ill-formed (since it
violates an explicit requirement of the standard), and your C++ compiler,
after emitting a minimum of one diagnostic, is free to do as it likes. (-:
---
[ 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: "Rodrigo de Salvo Braz" <Rodrigo_Braz@brown.edu.nospam>
Date: 2000/05/13 Raw View
The following program only compiles (BCB) in a certain odd condition.
If I have the global variable 'number_of_students_per_class', it DOESN't
recognize the template operator>> defined.
If I comment the variable out, though, it does recognize the operator>>.
But those things shouldn't have to do with each other, so what's going
on?!?
Note: this is specific to vector<>. If I use some simple template class
instead, things work well in either case.
Note also that this only happens to the vector<float> that is a *member*
of
a class, but *not* to the *local* variable. THAT's really puzzling!
Please, any help would be greatly appreciated, since this is a
simplification of a real application whose development is stalled waiting
for this to be solved...
#include <vector>
struct aistream {};
/**************************************************************/
/* Funtion to input vectors in general
/**************************************************************/
template <class T>
aistream& operator>> (aistream& i, std::vector<T>& c)
{
c;
// output c somehow
return i;
}
/**************************************************************/
/* Definition of class 'student'
/**************************************************************/
struct student
{
std::vector<float> grades;
};
/**************************************************************/
/* Global variable number_of_students_per_class
/**************************************************************/
std::vector<int> number_of_students_per_class;
/**************************************************************/
/* Input grades of student.
/**************************************************************/
aistream& operator>> (aistream& i, student& st)
{
i >> st.grades; // this DOESN'T match the template operator>> UNLESS
the
global variable is not present.
return i;
}
/**************************************************************/
/* Input some set of grades
/**************************************************************/
aistream& operator>> (aistream& i, std::vector<float>& grades)
{
i >> grades; // this DOES match the template operator>> REGARDLESS of
the global variable being present or not.
return i;
}
void main()
{
// whatever
}
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: "Michael Kochetkov" <mkochetk@trustworks.commm>
Date: 2000/05/16 Raw View
"Rodrigo de Salvo Braz" <Rodrigo_Braz@brown.edu.nospam> wrote in message
news:8fackc$k3j@cocoa.brown.edu...
> The following program only compiles (BCB) in a certain odd condition.
> If I have the global variable 'number_of_students_per_class', it
DOESN't
> recognize the template operator>> defined.
I heve compiled your program with Microsoft, Borland, gcc, Intel, Sun,
Comeau compilers whether 'number_of_students_per_class' is present or
not.
The only problem is the main function signature. It should be int main().
What are your error messages?
With regards,
Michael Kochetkov.
[skip]
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ 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: fzhong@my-deja.com
Date: 2000/05/17 Raw View
In article <8fackc$k3j@cocoa.brown.edu>,
"Rodrigo de Salvo Braz" <Rodrigo_Braz@brown.edu.nospam> wrote:
[...]
> #include <vector>
>
> struct aistream {};
>
> /**************************************************************/
> /* Funtion to input vectors in general
> /**************************************************************/
> template <class T>
> aistream& operator>> (aistream& i, std::vector<T>& c)
> {
> c;
> // output c somehow
> return i;
> }
>
> /**************************************************************/
> /* Definition of class 'student'
> /**************************************************************/
> struct student
> {
> std::vector<float> grades;
> };
>
> /**************************************************************/
> /* Global variable number_of_students_per_class
> /**************************************************************/
> std::vector<int> number_of_students_per_class;
>
> /**************************************************************/
> /* Input grades of student.
> /**************************************************************/
> aistream& operator>> (aistream& i, student& st)
> {
> i >> st.grades; // this DOESN'T match the template
> // operator>> UNLESS the
> // global variable is not present.
it has nothing to do with the global.
before you call i >> st.grades
the overload version, e.g.
aistream& operator>> (aistream&, std::vector<float>&);
must be visible.
> return i;
> }
>
> /**************************************************************/
> /* Input some set of grades
> /**************************************************************/
> aistream& operator>> (aistream& i, std::vector<float>& grades)
> {
> i >> grades; // this DOES match the template operator>>
here, you got an infinite recursive call.
Regards
Frank Z.
> // REGARDLESS of
> the global variable being present or not.
> return i;
> }
>
> void main()
> {
> // whatever
> }
>
> [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
> [ about comp.lang.c++.moderated. First time posters: do this! ]
>
> [ 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 ]
>
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ 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 ]