博文

c++编译器为什么不支持模板的分离式编译

当我们声明和定义一个模板的时候,必须要让声明和定义放在一个文件里。否则编译器会报错。 这就是为什么boost的实现文件的后缀名是hpp了。 这其中的理由是什么呢?为什么会这样?  <!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Century Gothic"; panose-1:2 11 5 2 2 2 2 2 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:647 0 0 0 159 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New...

c++中静态成员变量要在类外部再定义,否则产生link2001错误

c++中静态成员变量要在类外部再定义,否则产生link2001错误. class testClass {   public:   static int m_i; }; // 类外部定义,若不写会产生 // error LNK2001: unresolved external symbol "public: static int testClass::m_i" (?m_i@testClass@@2HA) int testClass::m_i; int main(int argc, char* argv[]) {   printf("%d\n",testClass::m_i);   printf("\n");   return 0; } 为什么要在类的外部进行定义的原因: 1. 在类中,只是声明了静态变量,并没有定义。 2. 声明只是表明了变量的数据类型和属性,并不分配内存;定义则是需要分配内存的。    注意:如果在类里面这么写int a; 那么是既声明了变量,也定义了变量,两者合在一起了。 3. 静态成员是“类级别”的,也就是它和类的地位等同,而普通成员是“对象(实例)级别”的。    类级别的成员,先于该类任何对象的存在而存在,它被该类所有的对象共享。 4. 现在,咱们假定要实例化该类的一个对象,那么会发生什么事情呢?    静态成员肯定要出现在这个对象里面的,对吧?这时候才去定义那个静态成员吗?这显然是不合适的。    因为,比如有另外一个线程也要创建该类的对象,那么也要按照这个方式去定义那个静态成员。    这会产生两种可能的情况:       A. 重复定义;       B. 就算不产生重复定义的情况,也会产生竞争,从而造成死锁的问题,以至于对象无法创建。          很显然,编译器不能这么干。那么很合理的解决办法,就是事先在类的外部把它定义好,然后再供所有的对象共享。 ...

在实际编程中遇到的一些问题

在实际编程中遇到的一些问题   ➔ 使用floor函数。floor(x)返回的是小于或等于x的最大整数。     如:floor(10.5) == 10    floor(-10.5) == -11  ➔使用ceil函数。ceil(x)返回的是大于x的最小整数。     如:ceil(10.5) == 11    ceil(-10.5) ==-10      ➔floor()是向负无穷大舍入,floor(-10.5) == -11;    ceil()是向正无穷大舍入,ceil(-10.5) == -10   ➔fix     朝零方向取整,如fix(-1.3)=-1; fix(1.3)=1;     floor     朝负无穷方向取整,如floor(-1.3)=-2; floor(1.3)=1;     ceil     朝正无穷方向取整,如ceil(-1.3)=-1; ceil(1.3)=2;     round     四舍五入到最近的整数,如 round(-1.3)=-1;round(-1.52)=-2;round(1.3)=1;round(1.52)=2  ➔有关头文件的使用       对于floor函数等,要引入<cmath>,不用写成std::floor来使用。 ➔atof函数的使用( ascii to floating point numbers)    头文件:  #include <stdlib.h>    功 能: 把字符串转换成浮点数    

Warning: Control reaches end of non-void function

图片
Warning: Control reaches end of non-void function--Reason and Solution   The reason for this warning is that the compiler thinks your function may never return something at the end of the function, though you are 100% sure that it can return what you expect.  The solution is simple, just add a statement returning something at the end of you function body. And the warning or error will disappear. UPDATE!  Last time I said that the solution the the problem is to add a expression "return 0;" to the end of the function. However, when I was having class, our teacher gave us a better solution! That is: REMOVE THE ELSE SENTENCE !  If the condition is satisfied, the following expression will be executed and an  integer will be returned. If the condition is not satisfied, directly return a -1 and the function is ended. Here is the demonstration: This way is succinct and more logical. But it seems it can't be applied in the first example. ...

C++中头文件的编写方法

头文件(.h):      写类的声明(包括类里面的成员和方法的声明)、函数原型、#define常数等,但一般来说不写出具体的实现。      在写头文件时需要注意,在开头和结尾处必须按照如下样式加上预编译语句(如下): #ifndef NAME_H #define NAME_H // YOUR CODE #endif
图片
java swing项目总结 遇到的问题和解决方法 二维数组的大小  复制数组 数组长度:arrayName.length