1、A.cpp的内容:

int foo(int a, int b)
{
    return a + b;
}

2、B.cpp内容:

int foo(int a, int b);

int foo2(int a, int b)
{
    return foo(a, b);
}

3、C.cpp内容:

#include<iostream>
using namespace std;

int foo2(int a, int b); //如果调用B中的foo2函数,需要提前声明。
int foo(int a, int b); //如果调用A中的foo函数,需要提前声明。

int main()
{
    cout << foo(2, 3) << endl; //调用A中国的foo函数
    return 0;
}

这里,麻烦的的是,需要提前声明其他文件中的函数。所以不如使用头文件,统一管理即可。

这样处理:
A.h内容:

int foo(int a, int b);

A.cpp内容:

#include "A.h"
int foo(int a, int b)
{
    return a + b;
}

B.h内容:

int foo2(int a, int b);

B.cpp内容:

#include "A.h"

int foo2(int a, int b)
{
    return foo(a, b);
}

C.cpp内容:

#include<iostream>
#include "B.h"
using namespace std;

int main()
{
    cout << foo2(2, 3) << endl;
    return 0;
}

这样通过include头文件即可解决这个问题喽。

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐