哪里出错了?
有下面一段程序:
//---------------------------------------------------------------------------
#include <vector>
#include <iostream>
#include <string>
#include <list>
//---------------------------------------------------------------------------
struct Entry{
string bookname ;
string card ;
};
void f(vector<Entry>& ve, list<Entry>& le)
{
sort(ve.begin(), ve.end()) ;
unique_copy(ve.begin(), ve.end), le.begin()) ;
}
void f1(vector<Entry>& ve, list<Entry>& le)
{
sort(ve.begin(), ve.end) ;
unique_copy(ve.begin(), ve.end(), back_insert(le))
}
int main(int argc, char* argv[])
{
vector<Entry> &vbook ;
list<Entry>& &lbook ;
f(vbook, lbook) ;
return 0;
}
//---------------------------------------------------------------------------
在vc6种编译时除下面错误:
aglor1.cpp
G:\exise\aglor1.cpp(13) : error C2146: syntax error : missing ';' before identifier 'bookname'
G:\exise\aglor1.cpp(13) : error C2501: 'string' : missing storage-class or type specifiers
G:\exise\aglor1.cpp(13) : error C2501: 'bookname' : missing storage-class or type specifiers
G:\exise\aglor1.cpp(14) : error C2146: syntax error : missing ';' before identifier 'card'
G:\exise\aglor1.cpp(14) : error C2501: 'string' : missing storage-class or type specifiers
G:\exise\aglor1.cpp(14) : error C2501: 'card' : missing storage-class or type specifiers
G:\exise\aglor1.cpp(16) : error C2065: 'vector' : undeclared identifier
G:\exise\aglor1.cpp(16) : error C2275: 'Entry' : illegal use of this type as an expression
G:\exise\aglor1.cpp(12) : see declaration of 'Entry'
G:\exise\aglor1.cpp(16) : error C2065: 've' : undeclared identifier
G:\exise\aglor1.cpp(16) : error C2065: 'list' : undeclared identifier
G:\exise\aglor1.cpp(16) : error C2275: 'Entry' : illegal use of this type as an expression
G:\exise\aglor1.cpp(12) : see declaration of 'Entry'
G:\exise\aglor1.cpp(16) : error C2065: 'le' : undeclared identifier
G:\exise\aglor1.cpp(17) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
G:\exise\aglor1.cpp(21) : error C2275: 'Entry' : illegal use of this type as an expression
G:\exise\aglor1.cpp(12) : see declaration of 'Entry'
G:\exise\aglor1.cpp(21) : error C2275: 'Entry' : illegal use of this type as an expression
G:\exise\aglor1.cpp(12) : see declaration of 'Entry'
G:\exise\aglor1.cpp(22) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
G:\exise\aglor1.cpp(28) : error C2275: 'Entry' : illegal use of this type as an expression
G:\exise\aglor1.cpp(12) : see declaration of 'Entry'
G:\exise\aglor1.cpp(28) : error C2065: 'vbook' : undeclared identifier
G:\exise\aglor1.cpp(29) : error C2275: 'Entry' : illegal use of this type as an expression
G:\exise\aglor1.cpp(12) : see declaration of 'Entry'
G:\exise\aglor1.cpp(29) : error C2065: 'lbook' : undeclared identifier
G:\exise\aglor1.cpp(29) : error C2102: '&' requires l-value
G:\exise\aglor1.cpp(30) : error C2065: 'f' : undeclared identifier
Error executing cl.exe.
aglor1.obj - 22 error(s), 0 warning(s)
//____________________________________
请问错再哪了?
问题点数:20、回复次数:12Top
1 楼Flood1984(峰子)回复于 2004-12-04 18:37:44 得分 4
在
#include <vector>
#include <iostream>
#include <string>
#include <list>
后加上一句:
using namespace std;Top
2 楼oyljerry(【勇敢的心】→ ㊣提拉米苏√㊣)回复于 2004-12-04 18:52:05 得分 3
加上namespace
sort(ve.begin(), ve.end) ;
-》 sort(ve.begin(), ve.end()) ;
变量定义不需要引用Top
3 楼greenteanet(扎扎实实打基础,保持一颗平常心。)回复于 2004-12-04 19:05:03 得分 3
//---------------------------------------------------------------------------
#include <vector>
#include <iostream>
#include <string>
#include <list>
using namespace std;
//---------------------------------------------------------------------------
struct Entry
{
string bookname ;
string card ;
};
void f(vector<Entry>& ve, list<Entry>& le)
{
sort(ve.begin(), ve.end()) ;
unique_copy(ve.begin(), ve.end(), le.begin()) ;
}
void f1(vector<Entry>& ve, list<Entry>& le)
{
sort(ve.begin(), ve.end) ;
unique_copy(ve.begin(), ve.end(), back_insert(le));
}
int main()
{
vector <Entry> &vbook ;
list <Entry>& lbook ;
f(vbook, lbook) ;
return 0;
}
//---------------------------------------------------------------------------Top
4 楼WiseNeuro(春之舞)回复于 2004-12-04 21:57:41 得分 0
谢谢大家:)
现在程序变成这样了:
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
#include <list>
#pragma hdrstop
//---------------------------------------------------------------------------
//#pragma argsused
using namespace std ;
struct Entry{
string name ;
string number ;
};
void f(vector<Entry>& ve, list<Entry>& le)
{
sort(ve.begin(), ve.end()) ;
unique_copy(ve.begin(), ve.end(), le.begin()) ;
}
void f1(vector<Entry>& ve, list<Entry>& le)
{
sort(ve.begin(), ve.end()) ;
unique_copy(ve.begin(), ve.end(), back_inserter(le)) ;
}
int main(int argc, char* argv[])
{
vector<Entry> &vbook ;
list<Entry>& lbook ;
f(vbook, lbook) ;
return 0;
}
//---------------------------------------------------------------------------
编译错误为:
Compiling...
aglor1.cpp
G:\exise\aglor1.cpp(21) : error C2065: 'sort' : undeclared identifier
G:\exise\aglor1.cpp(22) : error C2065: 'unique_copy' : undeclared identifier
G:\exise\aglor1.cpp(31) : error C2530: 'vbook' : references must be initialized
G:\exise\aglor1.cpp(32) : error C2530: 'lbook' : references must be initialized
Error executing cl.exe.
aglor1.obj - 4 error(s), 0 warning(s)
//——————————————————————————————
我还应该改哪里?谢谢。
Top
5 楼WiseNeuro(春之舞)回复于 2004-12-05 13:08:01 得分 0
我添加了两个头文件#include <algorithm>,#include <functional>
//---------------------------------------------------------------------------
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#pragma hdrstop
//---------------------------------------------------------------------------
//#pragma argsused
using namespace std ;
struct Entry{
string name ;
string number ;
};
void f(vector<Entry>& ve, list<Entry>& le)
{
sort(ve.begin(), ve.end()) ;
unique_copy(ve.begin(), ve.end(), le.begin()) ;
}
void f1(vector<Entry>& ve, list<Entry>& le)
{
sort(ve.begin(), ve.end()) ;
unique_copy(ve.begin(), ve.end(), back_inserter(le)) ;
}
int main(int argc, char* argv[])
{
vector<Entry> &vbook;
list<Entry> &lbook ;
//copy()
f(vbook, lbook) ;
return 0;
}
//---------------------------------------------------------------------------
编译错误为:
aglor1.cpp
G:\exise\aglor1.cpp(34) : error C2530: 'vbook' : references must be initialized
G:\exise\aglor1.cpp(35) : error C2530: 'lbook' : references must be initialized
Error executing cl.exe.
aglor1.obj - 2 error(s), 0 warning(s)Top
6 楼avalonBBS("︶.︶メ)→( ̄ε ̄メ)回复于 2004-12-05 13:14:30 得分 3
vector<Entry> &vbook;
list<Entry> &lbook ;
引用要初始化Top
7 楼avalonBBS("︶.︶メ)→( ̄ε ̄メ)回复于 2004-12-05 13:14:59 得分 2
vector<Entry> vbook;
list<Entry> lbook ;
这样就行了Top
8 楼WiseNeuro(春之舞)回复于 2004-12-05 19:05:00 得分 0
改成
vector<Entry> vbook;
list<Entry> lbook ;
后错误更多了。关键是如何初始化vbook和lbook 。Top
9 楼tlzhu()回复于 2004-12-05 19:44:18 得分 1
初始化就往里添加Entry结构的数据啊。Top
10 楼WiseNeuro(春之舞)回复于 2004-12-05 22:57:48 得分 0
该怎样添加数据呢?可不可以告诉我具体代码?Top
11 楼libbyliugang()回复于 2004-12-06 19:19:15 得分 2
#include <iostream> 是C++标准形式
但是,要使用它必须包含:
using namespace std;
因为,许多东西都在std命名空间中声明的Top
12 楼rexking0(风之彩)回复于 2004-12-06 20:51:40 得分 2
#include <iostream> 是一种标准输入流
用cin时 可这样使用std::cin 但每次都要加上std::
也可在先using std::cin 那以后可直接用cin
补充一下libbyliugang的话语
小程序可使用using namespace std;但大程序也这样用那可能会造成名字冲突Top




