怎么将一个函数放到头文件中?

itelly 2011-12-22 05:58:12
是这样的,我写了一个函数get,再get.h文件中申明了,然后再get.cpp中实现,再main.cpp中使用,但是出现了错误
求解
get.h
#ifndef GET_H_INCLUDED
#define GET_H_INCLUDED
extern istream &get(istream& gt)



#endif // GET_H_INCLUDED

get.cpp
#include <iostream>
#include <stdexcept>
#inlucde "get.h"
using namespace std;
istream &get(istream& gt)
{
string a;
while(gt>>a)
{
if(gt.bad())
throw runtime_error("iostream is corrupted");
cout<<a<<endl;
}
return gt;
}

main.cpp
#include <iostream>
#include "get.h"
using namespace std;
int main()
{
double ival;
get(cin);
cin>>ival;
cout<<ival;
return 0;
}


编译出错信息如下:
C:\Users\admin\Documents\Codeblocks\text\get.h|3|error: expected initializer before '&' token|
C:\Users\admin\Documents\Codeblocks\text\main.cpp||In function 'int main()':|
C:\Users\admin\Documents\Codeblocks\text\main.cpp|7|error: 'cin' was not declared in this scope|
C:\Users\admin\Documents\Codeblocks\text\main.cpp|7|error: 'get' was not declared in this scope|
C:\Users\admin\Documents\Codeblocks\text\main.cpp|9|error: 'cout' was not declared in this scope|
||=== Build finished: 4 errors, 0 warnings ===|
...全文
236 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangbina07 2011-12-23
  • 打赏
  • 举报
回复
extern istream &get(istream& gt);
haolly 2011-12-23
  • 打赏
  • 举报
回复
你那个include写错啦
itelly 2011-12-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 seucs 的回复:]

是命名空间和函数声明有问题?帮你修改如下
C/C++ code

// get.h
#ifndef GET_H_INCLUDED
#define GET_H_INCLUDED

#include<iostream>
using std::istream;
istream &get(istream& gt);

#endif // GET_H_INCLUDED



改完你……
[/Quote]
其实这是书上的练习题,目的就是熟悉一下io

不过改过来后还有些问题,再main.cpp文件里

C:\Users\admin\Documents\Codeblocks\text\main.cpp|3|error: expected initializer before 'using'|
C:\Users\admin\Documents\Codeblocks\text\main.cpp||In function 'int main()':|
C:\Users\admin\Documents\Codeblocks\text\main.cpp|7|error: 'get' was not declared in this scope|
||=== Build finished: 2 errors, 0 warnings ===|
seucs 2011-12-22
  • 打赏
  • 举报
回复
是命名空间和函数声明有问题?帮你修改如下

// get.h
#ifndef GET_H_INCLUDED
#define GET_H_INCLUDED

#include<iostream>
using std::istream;
istream &get(istream& gt);

#endif // GET_H_INCLUDED


改完你再试试,另外,不知道你的函数的意图是什么

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧