C++一个函数如何调用其他.cpp文件中的函数

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

C++一个函数如何调用其他.cpp文件中的函数

Trivis Kylee   2023-03-20 我要评论

一个函数调用其他.cpp文件中的函数

使用VC或VS创建C++项目的时候,会自动产生许多文件夹,其中有一个文件夹->源文件:

在该文件下可以自定义许多.cpp文件,但是需要注意的是这里面的各个文件只能有一个文件中含有main()函数,

而且各个文件中不能使用相同的函数名进行定义;

那么要那么多文件放在项目中有什么用呢?

当然这里C++是提供一个文件调用其他文件中函数的功能的,

这就可以让我们自定义一个只包含main()函数的文件,通过在该函数中调用其他文件中的函数就可以将各个文件链接起来,

而且更重要的一点就是,通过调用其他,cpp文件中的函数的时候,如果调用的某函数又调用了它自己文件中的一个函数,

那么只用调用“父级函数”就可以实现间接调用~~~

看示例

首先是资源管理窗口:

功能主函数.cpp

// C++上机作业.cpp : 定义控制台应用程序的入口点。
//'0-9': 48-57
 
#include "stdafx.h"
using namespace std;
extern void gotoxy(short x, short y);
extern void sort_by_name();
extern int Strtoint();
 
int main()
{
	system("title 功能主函数");
	gotoxy(23, 2); cout << "功能列表";
	gotoxy(15, 3); cout << "1:字符串转换为数值类型";
	gotoxy(15, 4); cout << "2:对中文字符进行排序";
 
	gotoxy(0, 10);
	int choice = 0;
	cout << "请输入您要执行的功能:";
	cin >> choice;
	getchar();    //吸收回车
	switch (choice)
	{
		case 1:
			Strtoint();
			break;
		case 2:
			sort_by_name();
			break;
		default:
			cout << "选择失败,感谢使用,再见!" << endl << endl;
	}	
    return 0;
}

stdafx.h(stdandard application framework extensions)

// stdafx.h : 标准系统包含文件的包含文件,
// 或是经常使用但不常更改的
// 特定于项目的包含文件
//
 
#pragma once
 
#include "targetver.h"
 
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <Windows.h>
#include <string>    //注意这里的string与cstring中的使用差别,在定义与使用cout输出string类型字符串的时候,最好使用string库,否则可能会出现乱码以及错误等一系列错误
 
 
 
// TODO:  在此处引用程序需要的其他头文件

gotoxy().cpp

#include "stdafx.h"
using namespace std;
 
void gotoxy(short x, short y)
{
	COORD pos = { x,y };
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
}

对中文字符的排序.cpp

//对中文字符串进行排序时,默认是按照第一个字符的第一个拼音的顺序进行排序
 
#include "stdafx.h"
using namespace std;
 
void sort_by_name()
{
	string s[4] = { "一号","二号","三号","四号" }, t;
	for (int i = 0; i<4; i++)
	{
		for (int j = i; j<4; j++)
		{
			if (s[i]>s[j])
			{
				t = s[i];
				s[i] = s[j];
				s[j] = t;
			}
		}
	}
	for (int i = 0; i < 4; i++)
	{
		cout << s[i] << endl;
	}
	cout << "功能运行结束!" << endl << endl;
}

字符串转换为数值型.cpp

#include "stdafx.h"
using namespace std;
 
int Strtoint_0(const char str[])  //字符串数字转换为整形
{
	int i = 0, j = 0;
	long long number1 = 0;    //定义一个长整形变量,用来存储转换后得到的值
	int number[50] = { 0 };    //定义一个数组,用来存储转换后得到的值
	int symbol = 1;    //符号常量,0为负,1为正(默认为正)
	while (str[i] != '\0')	  //测试输出判断是否正确
	{
		while (str[i] == ' ')
		{
			i++;
		}
		if ((str[i] == '+' || str[i] == '-'))
		{
			i++;
			if (str[i] == '-')
			{
				symbol = 0;
			}
		}
		else if (str[i]<'9' && str[i]>'0')
		{
			number[j++] = str[i] - 48;    //存储数据,j++
										  //			cout << number[j - 1] << endl;
			i++;
		}
		if (str[i]>'9' || str[i]<'0')    //停止输出规则判断语句
		{
			break;
		}
	}
	cout << "数的位数为:" << j << endl;    //j到这里就已经得到数组的最大索引值+1了
	int x = 1;
	for (int k = j - 1; k >= 0; k--, x = x * 10)
	{
		number1 += number[k] * x;
	}
	if (symbol == 0)
	{
		number1 = number1*(-1);
	}
	cout << "转换后的数为:" << number1 << endl << endl;
	return 1;
}
 
int Strtoint()    //调用字符转换函数,确保变量不在主函数中定义
{
	char arr[50] = { 0 };
	int i = 0;
	char c;
	cout << "Please input the string :" << endl;
	while ((c = getchar()) != '\n')
	{
		arr[i++] = c;	//注意这里下面的i就开始++了
	}
	/*
	while ((c = cin.get()) != '\n')    //另一种控制输入的方法
	{
	arr[i++] = c;
	cout << arr[i - 1];
	}
	*/
	Strtoint_0(arr);
	return 0;
}

在主文件cpp中调用其他文件函数的方法

直接用

和我们的数据成员必须加extern不同的是,你只需把待调用函数的声明写在其头文件中,然后在主函数中直接用就可以

//test.h
#ifndef TEST_H //注意,这里千万不要写成TEST.H,必须用下划线,用点不行
#define TEST_H
void print();
#endif
 
 
//test.cpp
#include<iostream>
#include"test.h"
using namespace std;
void print() {
	cout << "test函数被调用" << endl;
}
 
 
//main.cpp
#include<iostream>
#include"test.h"
using namespace std;
int main() {
	print();
}

extern方法

使用extern的时候你甚至不需要在main.cpp文件中加上引用文件的声明,直接就可以用。

#include<iostream>
using namespace std;
extern void print();
int main() {
	print();
}

但是这样写其实作用不大,在一些大的工程中反而不如以好用。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们