问大神,大神说每个cpp文件都要写一个写一个同名.h文件,这个应该是针对复杂情况,简单的情况就是写一个.h文件把全局变量和会用到的函数写进去,然后需要调用的地方写#include "xxx.h"就行。注意:main函数中的变量声明还要再写一次的(float a,b,c;)这是没用.h的情况,用的话其实也就是把几句声明放进.h,然后用的时候调用.h
main.cpp
#include <iostream>
//#include "file2.h"
//#include "file3.h"
extern float a,b,c;
bool isTriangle(float g,float h,float i);
float length(float x,float y,float z);
//#include "main.h"
using namespace std;
int main() {
float a,b,c;
cin>>a>>b>>c;
if(isTriangle(a,b,c)) cout<<length(a,b,c);
else cout<<"wrong";
return 0;
}
file2.cpp
#include<iostream>
#include "main.h"
using namespace std;
bool isTriangle(float a,float b,float c){
if (a+b>c &&b+c>a) return true;
else return false;
}
file3.cpp
#include <iostream>
#include "main.h"
using namespace std;
float length(float a,float b,float c){
return a+b+c;
}