代码:
#include
#include
#include
#include "testcpp.cpp"
int main()
{
aaa t;
t.ChangeK(&t);
t.PutK(&t);
system("pause");
return 0;
}
testcpp.cpp
#include
using namespace std;
typedef struct aaa{
int k = 0;
void ChangeK(aaa *b);
void PutK(aaa *b);
}aaa;
void aaa::ChangeK(aaa *b){ int c = 0; cin >> c; b->k = c; }
void aaa::PutK(aaa *b){ cout << b->k; }
错误是我多次定义了这个ChangeK和PutK函数、、、、已在OBJ里存在了。。。
将函数的声明,类型的定义放在.h头文件
将函数的实现放在.cpp源文件中,你直接include源文件肯定报重定义的错误
分成三个文件:
1.testcpp.h
#ifndef TESTCPP_H
#define TESTCPP_H
#include<iostream>
using namespace std;
typedef struct aaa{
int k;
void ChangeK(aaa *b);
void PutK(aaa *b);
}aaa;
#endif
2.testcpp.cpp
#include"testcpp.h"
void aaa::ChangeK(aaa *b)
{
int c = 0;
cin >> c;
b->k = c;
}
void aaa::PutK(aaa *b)
{
cout << b->k;
}
3.main.cpp
#include<fstream>
#include<string>
#include<cmath>
#include "testcpp.h"
int main()
{
aaa t;
t.ChangeK(&t);
t.PutK(&t);
system("pause");
return 0;
}
把函数定义放到testcpp.h中。其他代码中include testcpp.h。而不是cpp
在前面几位的基础上,再严谨点应该加上头文件保护符:
#ifndef TESTCPPH
#define TESTCPPH
/*头文件内容*/
#endif