1.通常写法
//test.app
#include<iostream>
#include "func.h"
using namespace std;
void main()
{
char str[] = "abcde";
fanzhuan(str);
cout << str;
system("pause");
}
//func.h
void fanzhuan(char *str);
//func.cpp
void fanzhuan(char *str)
{
char *star = str;
char *end = str;
while (*end != '\0')
{
end++;
}
end--;
while (star < end)
{
char temp;
temp = *star;
*star = *end;
*end = temp;
star++;
end--;
}
}
2.如果不使用#include "func.h" 可以使用 extern void fanzhuan(char *str);直接声明函数是外部函数。这样就没有用上头文件的函数声明。
注意:
如果直接使用包含#include "func.cpp" (这里是包含的.cpp文件,不是.h)会报错。
错误 1 error LNK2005: "void __cdecl fanzhuan(char *)" (?fanzhuan@@YAXPAD@Z) 已经在 func.obj 中定义 d:\Projects\Project1\Project1\源.obj Project1
这个错误的解释请参考
https://blog.csdn.net/candcplusplus/article/details/7036917
//func.cpp
void fanzhuan(char *str)
{
char *star = str;
char *end = str;
while (*end != '\0')
{
end++;
}
end--;
while (star < end)
{
char temp;
temp = *star;
*star = *end;
*end = temp;
star++;
end--;
}
}
//test.app
#include<iostream>
extern void fanzhuan(char *str);
using namespace std;
void main()
{
char str[] = "abcde";
fanzhuan(str);
cout << str;
system("pause");
}