0

Sorry I'm new to C++, but I am having a problem compiling a C++ program that uses the functions of a class defined in another file.

I have:

 // GradeBook.h
 // GradeBook class definition in a separate file from main.
 //


#include <iostream>
#include <string> 
using namespace std;

class GradeBook
{
    public:
    GradeBook (string name)
    {
        setCourseName(name);
    } // end GradeBook constructor

    void setCourseName(string name)
    {
        courseName = name;
    }

    string getCourseName()
    { return CourseName; }

    void displayMessage()
    { cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
    private:
        string courseName;
}; //End of class

and this main file:

// grade.cpp
// Including class GradeBook from file GradeBook.h for use in main.
//
#include <iostream>
#include "Gradebook.h"
using namespace std;

int main()
{
    GradeBook gradeBook1( "CS101 Introduction to C++ Programing");
    GradeBook gradeBook2( "Cs102 Data Structures in C++");

    cout <<"gradeBook1 create for course: "<<gradeBook1.getCourseName() <<"\ngradeBook2 created for course: "<<gradeBook2.getCourseName() <<endl;
}// end main
// grade.cpp
// Including class GradeBook from file GradeBook.h for use in main.
//
#include <iostream>
#include "Gradebook.h"
using namespace std;

int main()
{
    GradeBook gradeBook1( "CS101 Introduction to C++ Programing");
    GradeBook gradeBook2( "Cs102 Data Structures in C++");

    cout <<"gradeBook1 create for course: "<<gradeBook1.getCourseName() <<"\ngradeBook2 created for course: "<<gradeBook2.getCourseName() <<endl;
}// end main

When I try to compile grade.cpp I get so many errors. My operating system is Fedora, and I use g++ grade.cpp -o grade -Wall
to compile the program. Can someone please help?


Error Message:

 g++ grade.cpp -o grade -lm
In file included from grade.cpp:5:0:
Gradebook.h: In member function ‘std::string GradeBook::getCourseName()’:
Gradebook.h:23:11: error: ‘CourseName’ was not declared in this scope
  { return CourseName; }
           ^
Gradebook.h: In member function ‘void GradeBook::displayMessage()’:
Gradebook.h:26:66: error: no match for ‘operator<’ (operand types are ‘std::basic_ostream<char>’ and ‘<unresolved overloaded function type>’)
  { cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
                                                                  ^
Gradebook.h:26:66: note: candidates are:
In file included from /usr/include/c++/4.8.3/bits/stl_algobase.h:64:0,
                 from /usr/include/c++/4.8.3/bits/char_traits.h:39,
                 from /usr/include/c++/4.8.3/ios:40,
                 from /usr/include/c++/4.8.3/ostream:38,
                 from /usr/include/c++/4.8.3/iostream:39,
                 from grade.cpp:4:
/usr/include/c++/4.8.3/bits/stl_pair.h:220:5: note: template<class _T1, class _T2> bool std::operator<(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
     operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
     ^
/usr/include/c++/4.8.3/bits/stl_pair.h:220:5: note:   template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::pair<_T1, _T2>’
  { cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
                                                                   ^
In file included from /usr/include/c++/4.8.3/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8.3/bits/char_traits.h:39,
                 from /usr/include/c++/4.8.3/ios:40,
                 from /usr/include/c++/4.8.3/ostream:38,
                 from /usr/include/c++/4.8.3/iostream:39,
                 from grade.cpp:4:
/usr/include/c++/4.8.3/bits/stl_iterator.h:297:5: note: template<class _Iterator> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
     operator<(const reverse_iterator<_Iterator>& __x,
     ^
/usr/include/c++/4.8.3/bits/stl_iterator.h:297:5: note:   template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
  { cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
                                                                   ^
In file included from /usr/include/c++/4.8.3/bits/stl_algobase.h:67:0,
                 from /usr/include/c++/4.8.3/bits/char_traits.h:39,
                 from /usr/include/c++/4.8.3/ios:40,
                 from /usr/include/c++/4.8.3/ostream:38,
                 from /usr/include/c++/4.8.3/iostream:39,
                 from grade.cpp:4:
/usr/include/c++/4.8.3/bits/stl_iterator.h:347:5: note: template<class _IteratorL, class _IteratorR> bool std::operator<(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
     operator<(const reverse_iterator<_IteratorL>& __x,
     ^
/usr/include/c++/4.8.3/bits/stl_iterator.h:347:5: note:   template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::reverse_iterator<_Iterator>’
  { cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
                                                                   ^
In file included from /usr/include/c++/4.8.3/string:52:0,
                 from /usr/include/c++/4.8.3/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8.3/bits/ios_base.h:41,
                 from /usr/include/c++/4.8.3/ios:42,
                 from /usr/include/c++/4.8.3/ostream:38,
                 from /usr/include/c++/4.8.3/iostream:39,
                 from grade.cpp:4:
/usr/include/c++/4.8.3/bits/basic_string.h:2569:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/4.8.3/bits/basic_string.h:2569:5: note:   template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
  { cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
                                                                   ^
In file included from /usr/include/c++/4.8.3/string:52:0,
                 from /usr/include/c++/4.8.3/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8.3/bits/ios_base.h:41,
                 from /usr/include/c++/4.8.3/ios:42,
                 from /usr/include/c++/4.8.3/ostream:38,
                 from /usr/include/c++/4.8.3/iostream:39,
                 from grade.cpp:4:
/usr/include/c++/4.8.3/bits/basic_string.h:2581:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
     operator<(const basic_string<_CharT, _Traits, _Alloc>& __lhs,
     ^
/usr/include/c++/4.8.3/bits/basic_string.h:2581:5: note:   template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note:   ‘std::basic_ostream<char>’ is not derived from ‘const std::basic_string<_CharT, _Traits, _Alloc>’
  { cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
                                                                   ^
In file included from /usr/include/c++/4.8.3/string:52:0,
                 from /usr/include/c++/4.8.3/bits/locale_classes.h:40,
                 from /usr/include/c++/4.8.3/bits/ios_base.h:41,
                 from /usr/include/c++/4.8.3/ios:42,
                 from /usr/include/c++/4.8.3/ostream:38,
                 from /usr/include/c++/4.8.3/iostream:39,
                 from grade.cpp:4:
/usr/include/c++/4.8.3/bits/basic_string.h:2593:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator<(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
     operator<(const _CharT* __lhs,
     ^
/usr/include/c++/4.8.3/bits/basic_string.h:2593:5: note:   template argument deduction/substitution failed:
In file included from grade.cpp:5:0:
Gradebook.h:26:67: note:   mismatched types ‘const _CharT*’ and ‘std::basic_ostream<char>’
  { cout <<"Welcome to the gradebook for\n" <<getCourseName()<<"!"<endl; }
7
  • You want to compile with g++ -Wall -Wextra -g grape.cpp -o grade. And you should give some of the error messages you've got. Commented Oct 23, 2014 at 20:28
  • SOrrry but I don't understand what you're saying. Commented Oct 23, 2014 at 20:31
  • I included the error messages now. Commented Oct 23, 2014 at 20:32
  • For the first error, theres a typo there. Change CourseName to courseName. Second error is also a type, its << and not <. When I was starting to learning c++ the error messages used to scary me. If it helps, try to check/fix one error per time. Commented Oct 23, 2014 at 20:33
  • 2
    Fix ONE thing at a time, recompile and see if what the next error is. A lot of your errors are "follow on errors" based on the compiler getting a bit lost. Commented Oct 23, 2014 at 20:39

1 Answer 1

1

you have to link it against the file to do this both must be compiled. e.g. g++ -Wall -Wextra grade.cpp Gradebook.cpp -o grade see how to link header files in c++

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.