我看钱能的那本《C++程序设计教程》,第六章,程序结构的时候,6。1节有个例题//-------------------------
//-- ch6_1.prj --
//-------------------------ch6_1.cpp
ch6_1_1.cpp//*************************
//** ch6_1.cpp **
//*************************#include <iostream.h>
void fn1();
void fn2();
int n;void main()
{
n=3;
fn1(); //fn1函数的定义在本文件中 cout << n << endl;
}
void fn1()
{
fn2(); //fn2函数的定义不在本文件中
}//***************************
//** ch6_1_1.cpp **
//***************************extern int n; //n由另一个源文件定义void fn2() //fn2()函数用于另一个源文件{
n=8; //使用n
}我把这两段代码分别保存在两个cpp文件里面,ch6_1.cpp和ch6_1_1.cpp里面,怎么连接的时候会报错阿?
--------------------Configuration: ch6_1 - Win32 Debug--------------------
Compiling...
ch6_1.cpp
D:\C++\ch6_1.cpp(5) : error C2143: syntax error : missing ';' before '.'
D:\C++\ch6_1.cpp(5) : error C2501: 'ch6_1' : missing storage-class or type specifiers
D:\C++\ch6_1.cpp(5) : error C2143: syntax error : missing ';' before '.'
Error executing cl.exe.ch6_1.obj - 3 error(s), 0 warning(s)
//-- ch6_1.prj --
//-------------------------ch6_1.cpp
ch6_1_1.cpp//*************************
//** ch6_1.cpp **
//*************************#include <iostream.h>
void fn1();
void fn2();
int n;void main()
{
n=3;
fn1(); //fn1函数的定义在本文件中 cout << n << endl;
}
void fn1()
{
fn2(); //fn2函数的定义不在本文件中
}//***************************
//** ch6_1_1.cpp **
//***************************extern int n; //n由另一个源文件定义void fn2() //fn2()函数用于另一个源文件{
n=8; //使用n
}我把这两段代码分别保存在两个cpp文件里面,ch6_1.cpp和ch6_1_1.cpp里面,怎么连接的时候会报错阿?
--------------------Configuration: ch6_1 - Win32 Debug--------------------
Compiling...
ch6_1.cpp
D:\C++\ch6_1.cpp(5) : error C2143: syntax error : missing ';' before '.'
D:\C++\ch6_1.cpp(5) : error C2501: 'ch6_1' : missing storage-class or type specifiers
D:\C++\ch6_1.cpp(5) : error C2143: syntax error : missing ';' before '.'
Error executing cl.exe.ch6_1.obj - 3 error(s), 0 warning(s)
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货