<分区>
比如我有2个cpp文件:f1.cpp和f2.cpp,还有一个头文件:xxx.h。
f1.cpp 具有以下源代码:
#include <iostream>
#include "xxx.h"
using namespace std;
int main ()
{
rect rplace;
polar pplace;
cout<<"Enter the x and y values: ";
while (cin>>rplace.x>>rplace.y)
{
pplace=rect_to_polar(rplace);
show_polar(pplace);
cout<<"Next two numbers (q to quit): ";
}
cout<<"Done.\n";
return 0;
}
f2.cpp源码:
#include <iostream>
#include <cmath>
#include "xxx.h"
polar rect_to_polar (rect xypos)
{
using namespace std;
polar answer;
answer.distance=sqrt(xypos.x*xypos.x+xypos.y*xypos.y);
answer.angle=atan2(xypos.y, xypos.x);
return answer;
}
void show_polar (polar dapos)
{
using namespace std;
const double Rad_to_deg=57.29577951;
cout<<"distance="<<dapos.distance;
cout<<", angle= "<<dapos.angle*Rad_to_deg;
cout<<"degrees\n";
}
和xxx.h:
struct polar
{
double distance;
double angle;
};
struct rect
{
double x;
double y;
};
polar rect_to_polar (rect xypos);
void show_polar(polar dapos);
我认为应该是编译器错误,因为 header xxx.h
和 iostream
包含两次:一次在 f1.cpp 中,一次在 f2.cpp 中.但是一切都是编译的,所以我不明白它是如何工作的。