使用头文件编译C++程序
Compiling C++ Program with a header file
很抱歉,我是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++中的头文件
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 使用mongocxx驱动程序时包含头文件问题
- 如何在c++程序中找到函数的地址