读书人

请赐教,不懂了解决思路

发布时间: 2012-04-04 16:38:51 作者: rapoo

请赐教,不懂了
#include <iostream>
#include <fstream.h>
#include <string.h>
#include <vector>
using namespace std ;

ofstream outfile( "1.txt ", ios_base::app ) ; //第一个错误指向这里

void display(vector <int> vec)
{
for(int i = 0 ; i < vec.size() ; i++)
{
cout < <vec[i] < < ' '; //第二个错误指向这里
cout < <endl ; //第三个错误指向这里
}
}

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

void bubble_sort(vector <int> &vec, ofstream *outfile = 0 )
{
for( int i = 0 ; i < vec.size() ; i++)
for( int j = i + 1 ; j < vec.size() ; j++)
{
if(vec[i] > vec[j])
{
if(outfile != 0)
{
(*outfile) < < "about to call swap " < <endl < < "i: " < <i //第四个错误指向这里
< <endl < < "j: " < <j < < "\t "
< < "swaping: " < <vec[i]
< < "with " < <vec[j] < <endl ;
}
swap(vec[i],vec[j]) ;
}
}
}
void main()
{
int a[8] = {8, 34, 3, 13, 1, 21, 5, 2} ;
vector <int> vec(a, a+8) ;

cout < < "排序前: " < <endl ; //第五个错误指向这里
display(vec) ;

bubble_sort(vec) ;
cout < < "排序后: " < <endl ; //第六个错误指向这里

display(vec) ;
}
就这么一段代码,是冒泡排序,有6个错误:

:\c++\wrok\练习\sort.cpp(8) : error C2872: 'ofstream ' : ambiguous symbol
f:\c++\wrok\练习\sort.cpp(14) : error C2872: 'cout ' : ambiguous symbol
f:\c++\wrok\练习\sort.cpp(15) : error C2872: 'cout ' : ambiguous symbol
f:\c++\wrok\练习\sort.cpp(57) : error C2100: illegal indirection
f:\c++\wrok\练习\sort.cpp(73) : error C2872: 'cout ' : ambiguous symbol
f:\c++\wrok\练习\sort.cpp(77) : error C2872: 'cout ' : ambiguous symbol
Error executing cl.exe.

sort.exe - 6 error(s), 0 warning(s)

不知道为什么


------解决方案--------------------


#include <iostream>
#include <fstream> //头文件格式,不要带 .h
#include <vector>
[解决办法]
更改为:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std ;


.........头部乱七八遭的

读书人网 >C++

热点推荐