由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 弱问C++一个问题 一直不解
相关主题
怎样include一个函数问低级问题
c++,这种做法不行?C 里面有办法永久改变一个指针的属性吗?
弱问c++里有没有NULL这个keyword?Help C++ Template function link error .
又一个初级问题: C++中多如牛毛的#define格式问个C++问题,高手帮帮忙
问个两个.h文件互相include的问题关于C++中 extern "C"的问题。
forward declarationHow to tell gcc stop compiling.
global variable usage question in C++两个class的交叉引用问题
Why should i include .cpp instead of .h最基本的C语言编程问题请教
相关话题的讨论汇总
话题: cpp话题: compile话题: include话题: int话题: try
进入Programming版参与讨论
1 (共1页)
f******n
发帖数: 640
1
就是为什么c++定义一个类要一个.h和一个.cpp呢?
.h定义一个方法,又要跑到cpp去写,有一段要重复的,多麻烦啊?
还有一个问题,如果定义了.h和.cpp
那么include的时候只需要用include"**.h"就可以了吗?
还需要include"**.cpp"吗?
大家见笑了啊
小白一个
l*********s
发帖数: 5409
2
generally no
f******n
发帖数: 640
3
写了一段程序试了一下
声明try.h:
#ifndef TRY_H_INCLUDED
#define TRY_H_INCLUDED
int add_two_number(int& a, int& b);
#endif // TRY_H_INCLUDED
然后是定义try.cpp:
#include "try.h"
int add_two_number(int& a, int& b)
{
return a+b;
}
然后是主程序:
#include "try.h"
#include
using namespace std;
int main()
{
int x1=1;
int x2=2;
int x3=add_two_number(x1, x2);
cout< cout< }
可以一直不能运行啊
但是在主程序中只有加了#include "try.cpp"才可以运行
这是怎么回事呢?
谢谢了
S**I
发帖数: 15689
4
How do you compile your program?

【在 f******n 的大作中提到】
: 写了一段程序试了一下
: 声明try.h:
: #ifndef TRY_H_INCLUDED
: #define TRY_H_INCLUDED
: int add_two_number(int& a, int& b);
: #endif // TRY_H_INCLUDED
: 然后是定义try.cpp:
: #include "try.h"
: int add_two_number(int& a, int& b)
: {

t*****n
发帖数: 4908
5
windows/linux?

【在 f******n 的大作中提到】
: 写了一段程序试了一下
: 声明try.h:
: #ifndef TRY_H_INCLUDED
: #define TRY_H_INCLUDED
: int add_two_number(int& a, int& b);
: #endif // TRY_H_INCLUDED
: 然后是定义try.cpp:
: #include "try.h"
: int add_two_number(int& a, int& b)
: {

d**o
发帖数: 864
6
因为你只compile了主程序,没有compile并link try.cpp

【在 f******n 的大作中提到】
: 写了一段程序试了一下
: 声明try.h:
: #ifndef TRY_H_INCLUDED
: #define TRY_H_INCLUDED
: int add_two_number(int& a, int& b);
: #endif // TRY_H_INCLUDED
: 然后是定义try.cpp:
: #include "try.h"
: int add_two_number(int& a, int& b)
: {

d**o
发帖数: 864
7
简单地说,.h管定义,.cpp管实现
当B对A有dependency时,只需要找A.h里的定义就可以compile了,但如果程序要运行必
须得compile A.cpp,并link起来。
好处在于,A.h里只管定义,所以需要编辑的时候不多,如果A.h不变,编辑A.cpp后,
重新编译程序时,只compile A.cpp,B就不用compile了,只样节省了compile B的时间。
这在项目大的时候节省很多时间。

【在 f******n 的大作中提到】
: 就是为什么c++定义一个类要一个.h和一个.cpp呢?
: .h定义一个方法,又要跑到cpp去写,有一段要重复的,多麻烦啊?
: 还有一个问题,如果定义了.h和.cpp
: 那么include的时候只需要用include"**.h"就可以了吗?
: 还需要include"**.cpp"吗?
: 大家见笑了啊
: 小白一个

G*****7
发帖数: 1759
8

you can separate declaration and implementation into .h and .cpp. and that
makes sense if you collaborate with other programmers.
if you are entirely on your own, nothing stops you from writing a gigantic
main.cpp that has all the classes in it.
you could use ide tools such as visual assist x to generate the duplicated
function signatures.
yes, if you compile **.cpp and link against **.obj.
no, if you just compile your main.cpp.
sometimes you will, esp if templates are involved.

【在 f******n 的大作中提到】
: 就是为什么c++定义一个类要一个.h和一个.cpp呢?
: .h定义一个方法,又要跑到cpp去写,有一段要重复的,多麻烦啊?
: 还有一个问题,如果定义了.h和.cpp
: 那么include的时候只需要用include"**.h"就可以了吗?
: 还需要include"**.cpp"吗?
: 大家见笑了啊
: 小白一个

ns
发帖数: 124
9
声明,确切的说

间。

【在 d**o 的大作中提到】
: 简单地说,.h管定义,.cpp管实现
: 当B对A有dependency时,只需要找A.h里的定义就可以compile了,但如果程序要运行必
: 须得compile A.cpp,并link起来。
: 好处在于,A.h里只管定义,所以需要编辑的时候不多,如果A.h不变,编辑A.cpp后,
: 重新编译程序时,只compile A.cpp,B就不用compile了,只样节省了compile B的时间。
: 这在项目大的时候节省很多时间。

d**o
发帖数: 864
10
谢谢 ,改正过来了

【在 ns 的大作中提到】
: 声明,确切的说
:
: 间。

相关主题
forward declaration问低级问题
global variable usage question in C++C 里面有办法永久改变一个指针的属性吗?
Why should i include .cpp instead of .hHelp C++ Template function link error .
进入Programming版参与讨论
f******n
发帖数: 640
11
谢谢大家了
a****l
发帖数: 8211
12
这不是一个节省时间的问题,想不节省都不成,不这么做就根本没办法link。

间。

【在 d**o 的大作中提到】
: 谢谢 ,改正过来了
d**o
发帖数: 864
13
.h .cpp只是文件后缀,没有任何现实意义,
全弄成cpp,然后include,就相当于把一个超 大的main分割成几个文件,根本没有
link的问题。

【在 a****l 的大作中提到】
: 这不是一个节省时间的问题,想不节省都不成,不这么做就根本没办法link。
:
: 间。

G*****7
发帖数: 1759
14
you could just include everything in a all_compile.cpp

【在 a****l 的大作中提到】
: 这不是一个节省时间的问题,想不节省都不成,不这么做就根本没办法link。
:
: 间。

a***n
发帖数: 538
15
可以只写一个cpp的呀,就是编译器来比较慢。
G*****7
发帖数: 1759
16
if you use a lot of template classes, a single file (compilation unit)
actually compiles faster, because the instantiation of duplicate templates
are curtailed. if you happen to have CGAL, notice they use this kind of "all
_compile.cpp" trick.
if you observe a slow down in release mode, that also makes sense: the
compile might be busy looking for opportunities to inline (a poor man's ltcg
essentially...).

【在 a***n 的大作中提到】
: 可以只写一个cpp的呀,就是编译器来比较慢。
a**********e
发帖数: 157
17


【在 f******n 的大作中提到】
: 就是为什么c++定义一个类要一个.h和一个.cpp呢?
: .h定义一个方法,又要跑到cpp去写,有一段要重复的,多麻烦啊?
: 还有一个问题,如果定义了.h和.cpp
: 那么include的时候只需要用include"**.h"就可以了吗?
: 还需要include"**.cpp"吗?
: 大家见笑了啊
: 小白一个

a**********e
发帖数: 157
18

原因大概2个
1. 如果implementation也放在header file里,同时很多其他文件都要include这个文
件,会导致严重redundancy,同样的代码出现很多次。(在cpp preprocess后)
2. 如果在cpp文件改变implementation,而declaration(header file里)不变,只需
要recompile cpp file。否则,所有include这个文件的所有file都要recompile,很浪
费资源和时间。

【在 f******n 的大作中提到】
: 就是为什么c++定义一个类要一个.h和一个.cpp呢?
: .h定义一个方法,又要跑到cpp去写,有一段要重复的,多麻烦啊?
: 还有一个问题,如果定义了.h和.cpp
: 那么include的时候只需要用include"**.h"就可以了吗?
: 还需要include"**.cpp"吗?
: 大家见笑了啊
: 小白一个

x****u
发帖数: 44466
19
简单点说,就是和8x3文件名,640kb限制一样,属于历史遗留缺陷。

【在 a**********e 的大作中提到】
:
: 原因大概2个
: 1. 如果implementation也放在header file里,同时很多其他文件都要include这个文
: 件,会导致严重redundancy,同样的代码出现很多次。(在cpp preprocess后)
: 2. 如果在cpp文件改变implementation,而declaration(header file里)不变,只需
: 要recompile cpp file。否则,所有include这个文件的所有file都要recompile,很浪
: 费资源和时间。

h*******n
发帖数: 82
20
.h加个extern改成
#ifndef TRY_H_INCLUDED
#define TRY_H_INCLUDED
extern int add_two_number(int& a, int& b);
#endif // TRY_H_INCLUDED
虽然是全局函数,但是默认只有自己的编译单元可见,加个extern才能在其他单元里面
使用
相关主题
问个C++问题,高手帮帮忙两个class的交叉引用问题
关于C++中 extern "C"的问题。最基本的C语言编程问题请教
How to tell gcc stop compiling.gcc 编译的时候要包括 header source file 吗?
进入Programming版参与讨论
t****t
发帖数: 6806
21
呃...看你吹捧C++, 原来水平不怎么样...第一个贴就错了.

【在 h*******n 的大作中提到】
: .h加个extern改成
: #ifndef TRY_H_INCLUDED
: #define TRY_H_INCLUDED
: extern int add_two_number(int& a, int& b);
: #endif // TRY_H_INCLUDED
: 虽然是全局函数,但是默认只有自己的编译单元可见,加个extern才能在其他单元里面
: 使用

c0
发帖数: 116
22
不算大错,DEFAULT搞反而已

【在 t****t 的大作中提到】
: 呃...看你吹捧C++, 原来水平不怎么样...第一个贴就错了.
z*y
发帖数: 1311
23
the dot h dot c thing, really not big of a deal
fundamentally, compiler is not smart enough
or those who wrote compiler did not work hard enough
j*a
发帖数: 14423
24
用static吧

【在 t****t 的大作中提到】
: 呃...看你吹捧C++, 原来水平不怎么样...第一个贴就错了.
1 (共1页)
进入Programming版参与讨论
相关主题
最基本的C语言编程问题请教问个两个.h文件互相include的问题
gcc 编译的时候要包括 header source file 吗?forward declaration
is there any lib can read .Z file in c++/c program?global variable usage question in C++
另一个Fortran 问题Why should i include .cpp instead of .h
怎样include一个函数问低级问题
c++,这种做法不行?C 里面有办法永久改变一个指针的属性吗?
弱问c++里有没有NULL这个keyword?Help C++ Template function link error .
又一个初级问题: C++中多如牛毛的#define格式问个C++问题,高手帮帮忙
相关话题的讨论汇总
话题: cpp话题: compile话题: include话题: int话题: try