首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

看Essential C++时遇到有关问题

2013-07-09 
看Essential C++时遇到问题!第二章冒泡排序出现问题,求解答!书中将display函数放在NumericSeq.h中最后编译

看Essential C++时遇到问题!
第二章冒泡排序出现问题,求解答!
书中将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;
}

C++
[解决办法]
上面加上:

#pragma once

#include <vector>
using namespace std;

热点排行