看Essential C++时遇到问题!

Handz_cpp 2013-06-28 02:26:18
第二章冒泡排序出现问题,求解答!
书中将display函数放在NumericSeq.h中最后编译出现以下问题:

e:\c++\essential c++\chapter2\example2\numericseq.h(1) : error C2143: syntax error : missing ',' before '<'
e:\c++\essential c++\chapter2\example2\numericseq.h(1) : error C2059: syntax error : '<'
e:\c++\essential c++\chapter2\example2\example2.cpp(46) : error C2665: 'display' : none of the 2 overloads can convert parameter 1 from type 'class std::vector<int,class std::allocator<int> >'
e:\c++\essential c++\chapter2\example2\example2.cpp(49) : error C2665: 'display' : none of the 2 overloads can convert parameter 1 from type 'class std::vector<int,class std::allocator<int> >'

void display(const vector <int>&, ostream &= cout);

#include <iostream>
#include <vector>
#include <fstream>
#include "NumericSeq.h"
using namespace std;

void display(const vector<int> &vec, ostream &os)
{
for(int ix = 0; ix < vec.size(); ++ix)
os<<vec[ix]<<' ';
os<<endl;
}

void swap(int &val1, int &val2)
{
int temp = val1;
val1 = val2;
val2 = temp;
}

void bubble_sort(vector<int> &vec, ofstream *ofil = 0)
{
for(int ix = 0; ix < vec.size(); ++ix)
for(int jx = ix+1; jx < vec.size(); ++jx)
if(vec[ix] > vec[jx])
{
if(ofil != 0)
{
(*ofil)<<"about to call swap!"
<<" ix: "<<ix<<" jx: "<<jx<<'\t'
<<" swapping: "<<vec[ix]
<<" with "<<vec[jx]<<endl;
}
swap(vec[ix], vec[jx]);
}
}

int main()
{
int ia[8] = {8, 34, 3, 13, 1, 21, 5, 2};
vector<int> vec(ia, ia+8);
cout<<"vector before sort: ";
display(vec);
bubble_sort(vec);
cout<<"vector after sort: ";
display(vec);
ofstream ofil("data.txt");
bubble_sort(vec, &ofil);
display(vec, ofil);

return 0;
}
...全文
165 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Handz_cpp 2013-06-28
  • 打赏
  • 举报
回复
引用 7 楼 u010936098 的回复:
上面加上:

#pragma once

#include <vector>
using namespace std;
谢谢!解决了,还要把.cpp里面的东西弄过去,只留#include "NumericSeq.h"
橡木疙瘩 2013-06-28
  • 打赏
  • 举报
回复
上面加上:

#pragma once

#include <vector>
using namespace std;
Handz_cpp 2013-06-28
  • 打赏
  • 举报
回复
引用 5 楼 u010936098 的回复:
你一直没有说numericseq.h的第一行 到底是什么内容。
整个numericseq.h只有
void display(const vector <int>&, ostream &= cout);
这一行代码
橡木疙瘩 2013-06-28
  • 打赏
  • 举报
回复
你一直没有说numericseq.h的第一行 到底是什么内容。
Handz_cpp 2013-06-28
  • 打赏
  • 举报
回复
引用 1 楼 u010936098 的回复:
numericseq.h的第一行
额。。。什么意思?没懂
Handz_cpp 2013-06-28
  • 打赏
  • 举报
回复
引用 2 楼 adlay 的回复:
&= 中间加空格 ostream& =cout &= 本身是一个运算符, 连在一起编译器分不清了.
还是没对,一样的错误。
www_adintr_com 2013-06-28
  • 打赏
  • 举报
回复
&= 中间加空格 ostream& =cout &= 本身是一个运算符, 连在一起编译器分不清了.
橡木疙瘩 2013-06-28
  • 打赏
  • 举报
回复
numericseq.h的第一行

33,311

社区成员

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

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