c.h.cpp文件包含有关问题
c.h.cpp文件包含问题
//c1.h
#include <stdio.h>
//c2.h
struct
{
int a;
}integer;
//c3.cpp
void print(integer item)
{
item.a=1;
printf("%d", item.a);
}
//main.cpp
#include "c1.h"
#include "c2.h"
#include "c3.cpp"
int main()
{
integer item;
print(item);
return 0;
}
为什么编译不能通过,问题是:
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(1) : error C2065: 'integer' : undeclared identifier
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(1) : error C2146: syntax error : missing ')' before identifier 'item'
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(1) : error C2182: 'print' : illegal use of type 'void'
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(1) : error C2059: syntax error : ')'
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(2) : error C2143: syntax error : missing ';' before '{'
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(2) : error C2447: missing function header (old-style formal list?)
main.cpp
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(1) : error C2146: syntax error : missing ')' before identifier 'item'
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(1) : error C2182: 'print' : illegal use of type 'void'
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(1) : error C2440: 'initializing' : cannot convert from 'struct ' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(1) : error C2059: syntax error : ')'
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(2) : error C2143: syntax error : missing ';' before '{'
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(2) : error C2447: missing function header (old-style formal list?)
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\main.cpp(7) : error C2146: syntax error : missing ';' before identifier 'item'
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\main.cpp(7) : error C2065: 'item' : undeclared identifier
请大神帮忙!!!谢谢!!!
------解决方案--------------------
你这些所谓的头文件定义是很混乱的,但这不是你出错的主要问题,你把struct类的申明和定义搞混了,应该是
但是这样修改之后应该还是编译不过的,这就是你这些头文件的问题了,比如在c3.cpp
你要知道这是个cpp文件,是要进行编译的,那么integer printf是哪里来的?这里肯定会报错,那么叫他认识不就行了,前面给他加个#include “c1.h”,#include “c2.h”这样对吗?还是不对,为啥又说你你重复定义了。。。总之问题是相当的多,而且基本上是不可能修正的。
模块化编程的大体方法:
这样定义的好处是你可以重复的包含此文件,而只被加载一次,之后在.c 或.cpp文件中实现函数原型,最后在你想要调用该函数的地方加上上面定义的头文件即可!
另外使用extern关键字也可以进行外部函数或变量的引用。
//c1.h
#include <stdio.h>
//c2.h
struct
{
int a;
}integer;
//c3.cpp
void print(integer item)
{
item.a=1;
printf("%d", item.a);
}
//main.cpp
#include "c1.h"
#include "c2.h"
#include "c3.cpp"
int main()
{
integer item;
print(item);
return 0;
}
为什么编译不能通过,问题是:
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(1) : error C2065: 'integer' : undeclared identifier
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(1) : error C2146: syntax error : missing ')' before identifier 'item'
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(1) : error C2182: 'print' : illegal use of type 'void'
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(1) : error C2059: syntax error : ')'
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(2) : error C2143: syntax error : missing ';' before '{'
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\cpp.cpp(2) : error C2447: missing function header (old-style formal list?)
main.cpp
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(1) : error C2146: syntax error : missing ')' before identifier 'item'
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(1) : error C2182: 'print' : illegal use of type 'void'
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(1) : error C2440: 'initializing' : cannot convert from 'struct ' to 'int'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(1) : error C2059: syntax error : ')'
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(2) : error C2143: syntax error : missing ';' before '{'
d:\program files\microsoft visual studio\myprojects\hcpp\cpp.cpp(2) : error C2447: missing function header (old-style formal list?)
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\main.cpp(7) : error C2146: syntax error : missing ';' before identifier 'item'
D:\Program Files\Microsoft Visual Studio\MyProjects\hcpp\main.cpp(7) : error C2065: 'item' : undeclared identifier
请大神帮忙!!!谢谢!!!
c
------解决方案--------------------
你这些所谓的头文件定义是很混乱的,但这不是你出错的主要问题,你把struct类的申明和定义搞混了,应该是
struct integer;
{
int a;
}
但是这样修改之后应该还是编译不过的,这就是你这些头文件的问题了,比如在c3.cpp
void print(integer item)
{
item.a=1;
printf("%d", item.a);
}
你要知道这是个cpp文件,是要进行编译的,那么integer printf是哪里来的?这里肯定会报错,那么叫他认识不就行了,前面给他加个#include “c1.h”,#include “c2.h”这样对吗?还是不对,为啥又说你你重复定义了。。。总之问题是相当的多,而且基本上是不可能修正的。
模块化编程的大体方法:
#ifndef __TEXT_H__
#define __TEXT_H__
//你要在别处用的函数体或者变量
#endif
这样定义的好处是你可以重复的包含此文件,而只被加载一次,之后在.c 或.cpp文件中实现函数原型,最后在你想要调用该函数的地方加上上面定义的头文件即可!
另外使用extern关键字也可以进行外部函数或变量的引用。