使用头文件编译C++程序

Compiling C++ Program with a header file

本文关键字:C++ 程序 编译 文件      更新时间:2023-10-16

很抱歉,我是C++的新手,但在编译使用另一个文件中定义的类的函数的C++程序时遇到问题。

我有:

 // 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 forn" <<getCourseName()<<"!"<endl; }
    private:
        string courseName;
}; //End of class

和这个主文件:

// 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

当我试图编译grade.cpp时,我会遇到很多错误。我的操作系统是Fedora,我使用g++ grade.cpp -o grade -Wall
以编译程序。有人能帮忙吗?


错误消息:

 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 forn" <<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 forn" <<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 forn" <<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 forn" <<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 forn" <<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 forn" <<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 forn" <<getCourseName()<<"!"<endl; }

您必须将其与文件链接才能完成这两项操作。例如g++ -Wall -Wextra grade.cpp Gradebook.cpp -o grade查看如何链接c++中的头文件