Prev: Why does this crash?
Next: An Algorithm Intensive Programming Contest - Manthan @ Codefest - IT-BHU
From: Paul Richards on 24 Mar 2010 21:49 I am working my way through 'Accelerated C++" and have come upon a compiler error. The code is cut'n'pasted from downloaded source from the Accelerated C++ website. The compiler gives this error "error:'max' not declared in this scope" (see <<< ERROR below). The program is contained in Section 4.5, pp70-71. As I have #included <algorithm> why would this error occur? Thanks ==== #include <algorithm> #include <iomanip> #include <iostream> #include <stdexcept> #include <string> #include <vector> #include "grade.h" #include "Student_info.h" using std::cin; using std::setprecision; using std::cout; using std::sort; using std::domain_error; using std::streamsize; using std::endl; using std::string; using std::vector; int main() { vector<Student_info> students; Student_info record; string::size_type maxlen = 0; // the length of the longest name // read and store all the students' data. while (read(cin, record)) { maxlen = max(maxlen, record.name.size()); <<< ERROR students.push_back(record); } // alphabetize the student records sort(students.begin(), students.end(), compare); // write the names and grades for (vector<Student_info>::size_type i = 0; i != students.size(); ++i) { // write the name, padded on the right to `maxlen' `+' `1' characters cout << students[i].name << string(maxlen + 1 - students[i].name.size(), ' '); // compute and write the grade try { double final_grade = grade(students[i]); streamsize prec = cout.precision(); cout << setprecision(3) << final_grade << setprecision(prec); } catch (domain_error e) { cout << e.what(); } cout << endl; } return 0; } -- Paul Melbourne, Australia
From: Paul Bibbings on 24 Mar 2010 22:53 "Paul Richards" <paulrichards(a)XXXNOSPAMiinet.net.au> writes: > I am working my way through 'Accelerated C++" and have come upon a > compiler error. The code is cut'n'pasted from downloaded source from > the Accelerated C++ website. The compiler gives this error "error:'max' > not declared in this scope" (see <<< ERROR below). The program is > contained in Section 4.5, pp70-71. > > As I have #included <algorithm> why would this error occur? > > Thanks > > ==== > > #include <algorithm> > #include <iomanip> > #include <iostream> > #include <stdexcept> > #include <string> > #include <vector> > #include "grade.h" > #include "Student_info.h" > > using std::cin; using std::setprecision; > using std::cout; using std::sort; > using std::domain_error; using std::streamsize; > using std::endl; using std::string; > using std::vector; <snip /> Having #included <iostream>, you allow simply `cin' and `cout' by adding `using std::cin' and `using std::cout'. Similarly, having #included <vector>, you have added `using std::vector' so that you can just use `vector<whatever> as a type. Merely continue that theme. Having #included <algorithm>, to use just `max(...,...)', ... Regards Paul Bibbings
From: Paul Richards on 25 Mar 2010 20:30 Paul Bibbings wrote: > "Paul Richards" <paulrichards(a)XXXNOSPAMiinet.net.au> writes: > > > I am working my way through 'Accelerated C++" and have come upon a > > compiler error. The code is cut'n'pasted from downloaded source from > > the Accelerated C++ website. The compiler gives this error > > "error:'max' not declared in this scope" (see <<< ERROR below). The > > program is contained in Section 4.5, pp70-71. > > > > As I have #included <algorithm> why would this error occur? > > > > Thanks > > > > ==== > > > > #include <algorithm> > > #include <iomanip> > > #include <iostream> > > #include <stdexcept> > > #include <string> > > #include <vector> > > #include "grade.h" > > #include "Student_info.h" > > > > using std::cin; using std::setprecision; > > using std::cout; using std::sort; > > using std::domain_error; using std::streamsize; > > using std::endl; using std::string; > > using std::vector; > > <snip /> > > Having #included <iostream>, you allow simply `cin' and `cout' by > adding `using std::cin' and `using std::cout'. Similarly, having > #included <vector>, you have added `using std::vector' so that you > can just use `vector<whatever> as a type. > > Merely continue that theme. Having #included <algorithm>, to use just > `max(...,...)', ... > > Regards > > Paul Bibbings Paul: OK, thanks. Adding using std::max did the trick. The follow upquestion is which headers does namespace std apply to? All of them? -- Paul Melbourne, Australia
From: Anand Hariharan on 26 Mar 2010 17:43 On Mar 25, 7:30 pm, "Paul Richards" <paulricha...(a)XXXNOSPAMiinet.net.au> wrote: > Paul Bibbings wrote: > > "Paul Richards" <paulricha...(a)XXXNOSPAMiinet.net.au> writes: > > > > I am working my way through 'Accelerated C++" and have come upon a > > > compiler error. The code is cut'n'pasted from downloaded source from > > > the Accelerated C++ website. The compiler gives this error > > > "error:'max' not declared in this scope" (see <<< ERROR below). The > > > program is contained in Section 4.5, pp70-71. > > > > As I have #included <algorithm> why would this error occur? > > > > Thanks > > > > ==== > > > > #include <algorithm> > > > #include <iomanip> > > > #include <iostream> > > > #include <stdexcept> > > > #include <string> > > > #include <vector> > > > #include "grade.h" > > > #include "Student_info.h" > > > > using std::cin; using std::setprecision; > > > using std::cout; using std::sort; > > > using std::domain_error; using std::streamsize; > > > using std::endl; using std::string; > > > using std::vector; > > > <snip /> > > > Having #included <iostream>, you allow simply `cin' and `cout' by > > adding `using std::cin' and `using std::cout'. Similarly, having > > #included <vector>, you have added `using std::vector' so that you > > can just use `vector<whatever> as a type. > > > Merely continue that theme. Having #included <algorithm>, to use just > > `max(...,...)', ... > > > Regards > > > Paul Bibbings > > Paul: OK, thanks. Adding using std::max did the trick. > > The follow upquestion is which headers does namespace std apply to? All > of them? > In theory, yes. In practice however, most C headers (that are also available to C++) tend to put their function declarations in the global namespace. - Anand
|
Pages: 1 Prev: Why does this crash? Next: An Algorithm Intensive Programming Contest - Manthan @ Codefest - IT-BHU |