{"id":1579,"date":"2022-08-30T15:17:42","date_gmt":"2022-08-30T15:17:42","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/c-code-with-header-files-giving-out-function-errors-which-is-undefined-closed-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:42","modified_gmt":"2022-08-30T15:17:42","slug":"c-code-with-header-files-giving-out-function-errors-which-is-undefined-closed-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/c-code-with-header-files-giving-out-function-errors-which-is-undefined-closed-collection-of-common-programming-errors\/","title":{"rendered":"C++ code with header files giving out function errors which is undefined [closed]-Collection of common programming errors"},"content":{"rendered":"<pre><code>#ifndef GUARD_student_info_h\n#define GUARD_student_info_h\n\n#include \n#include \n#include \n\nstruct student_info\n{\n    std::string name;\n    double midterm, final;\n    std::vectorhomework;\n};\n\nbool compare(const student_info&amp;, const student_info&amp;);\nstd::istream&amp; read(std::istream&amp;, student_info&amp;);\nstd::istream&amp; read_hw(std::istream&amp;, std::vector&amp;);\n\n#endif\n<\/code><\/pre>\n<p>This is the student_info.h header file<\/p>\n<pre><code>#include \"student_info.h\"\n\nusing std::istream; using std::vector;\n\nbool compare(const student_info&amp; x, const student_info&amp; y)\n{\n    return x.name &lt; y.name;\n}\n\nistream&amp; read(istream&amp; is, student_info &amp; s)\n{\n    is &gt;&gt; s.name &gt;&gt; s.midterm &gt;&gt; s.final;\n\n    read_hw(is, s.homework);\n\n    return is;\n}\n\nistream&amp; read_hw(istream&amp; in, vector&amp; hw)\n{\n    if(in)\n    {\n        hw.clear();\n\n        double x;\n\n        while(in &gt;&gt; x)\n          hw.push_back(x);\n\n        in.clear();\n    }\n\n    return in;\n}\n<\/code><\/pre>\n<p>This is student_info.cpp<\/p>\n<pre><code>#ifndef GUARD_median_h\n#define GUARD_median_h\n\n#include \ndouble median(std::vector);\n\n#endif\n<\/code><\/pre>\n<p>This is median.h<\/p>\n<pre><code>#include \n#include \n#include \n\nusing namespace std;\nusing std::domain_error;\nusing std::vector;\n\ndouble median(vectorvec) \/\/ No need of saying it as vectorhomework, cos we wont be using this to always give median of hw, but median of some other data\n{\n    typedef vector::size_type vec_sz;\n    vec_sz size;\n\n    size = vec.size();\n\n    if(size == 0)\n      throw domain_error(\"Empty homework. From medain.cpp when vec.size() == 0.\");\n\n    sort(vec.begin(), vec.end());\n\n    vec_sz mid = size \/ 2;\n\n    return size % 2 == 0  ? vec[mid] + vec[mid - 1] \/ 2 : vec[mid];\n}\n<\/code><\/pre>\n<p>This is median.cpp<\/p>\n<pre><code>#ifndef GUARD_grade_h\n#define GUARD_grade_h\n\n#include \n#include \"student_info.h\"\n\ndouble grade(double, double, double);\ndouble grade(double, double, const std::vector&amp;);\ndouble grade(const student_info&amp;);\n\n#endif\n<\/code><\/pre>\n<p>This is grade.h<\/p>\n<pre><code>#include \n#include \n#include \"grade.h\"\n#include \"median.h\"\n#include \"student_info.h\"\n\nusing namespace std;\nusing std::domain_error;\n\ndouble grade(double midterm, double final, double homework)\n{\n    return 0.2 * midterm + 0.4 * final + 0.4 * homework;\n}\n\ndouble grade(double midterm, double final, const vector&amp; hw)\n{\n    if(hw.size() == 0)\n      throw domain_error(\"No homework. From grades function 2 in grade.cpp.\");\n\n    grade(midterm, final, median(hw));\n}\n\ndouble grade(const student_info&amp; s)\n{\n    return grade(s.midterm, s.final, s.homework);\n}\n<\/code><\/pre>\n<p>This is grade.cpp<\/p>\n<pre><code>#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \"grade.h\"\n#include \"student_info.h\"\n\nusing namespace std;\nusing std::streamsize;\nusing std::setprecision;\nusing std::domain_error;\nusing std::max;\n\nistream&amp; read(istream&amp; , student_info &amp;);\n\nint main()\n{\n    vectorstudents;\n    student_info record;\n    string::size_type maxlen = 0;\n\n    while(read(cin, record))\n    {\n        maxlen = max(maxlen, record.name.size());\n        students.push_back(record);\n    }\n     sort(students.begin(), students.end(), compare);\n\n    for (vector::size_type i = 0;i != students.size(); ++i) {\n        \/\/ write the name, padded on the right to maxlen + 1 characters\n        cout<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-27 11:53:21. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>#ifndef GUARD_student_info_h #define GUARD_student_info_h #include #include #include struct student_info { std::string name; double midterm, final; std::vectorhomework; }; bool compare(const student_info&amp;, const student_info&amp;); std::istream&amp; read(std::istream&amp;, student_info&amp;); std::istream&amp; read_hw(std::istream&amp;, std::vector&amp;); #endif This is the student_info.h header file #include &#8220;student_info.h&#8221; using std::istream; using std::vector; bool compare(const student_info&amp; x, const student_info&amp; y) { return x.name &lt; y.name; } istream&amp; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1579","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1579","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1579"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1579\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}