抱歉,我是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 for\n" <<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 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; }
答案 0 :(得分:1)
你必须将它与文件链接才能做到这一点必须编译。例如g++ -Wall -Wextra grade.cpp Gradebook.cpp -o grade
见how to link header files in c++