一、const char* 和char* 之间的转换

    const char*是指向常量的指针,而不是指针本身为常量,可以不被初始化.该指针可以指向常量也可以指向变量,只是从该指针的角度而言,它所指向的是常量,
通过该指针不能修改它所指向的数据. 

1.const char*是不能直接赋值到char*的,这样编译都不能通过,理由:假如可以的话,那么通过char*就可以修改const char指向的内容了,这是不允许的.所以char*要另外开辟新的空间。

#include <iostream>
using
namespace std;
void
main(){
 
const char* cpc="abcde";
 
char* pc=new char[100];
 strcpy(pc,cpc);
 cout<<pc<<endl;
}

2.char*到 const char*直接赋值就可以了

const char* cpc;

char* pc="abcde";

cpc=pc;

二、指针常量,常量指针

1. 什么是指针常量?指针常量即指针类型的常量。
例:char *const name1="John";
    name1="abc"; //错误,name1指针,不能变,一个指针类型的变量,存放的是地址,所以不能把'"abc"的地址赋给name1
    char * name2= name1; //可以
2. 什么是常量指针?常量指针即是指向常量的指针,指针的值可以改变,指针所指的地址中的内容为常量不能改变,
例:const char *name1="John";
    char s[]="abc"; name1=s; //正确,name1存放的地址可以改变
    char * name2= name1; //不可以,因为name2 和 name1存放的是同一块地址,如果name2地址中的内容改了,则name1的内容也改了,那么name1就不再是指向常量的指针了。
    一句话,靠近哪个哪个不能改变!
三、编译错误:“outside of class is not definition "
 有一种可能的情况:You have semicolons (;) at the end of all your function definitions making the compiler think they're declarations.
 即在方法定义的后面都误添加上了";",导致编译器认为这仅仅是个声明。
四、编译错误:“undefined reference to `typeinfo”
The fix? You've got line(s) like
virtual float getarea() ;
that should read
virtual float getarea() {} ;

 The complete (working) source code files for this example are available here

  作者说明:在析构函数后面添加了{},再make,问题解决了。我的所有虚函数都是有定义的,没想到就因为写基类的这个虚析构函数大意,没写函数体就出现了一个困扰我几天的莫名其妙的错误。就virtual ~CSgAnalyseStatBase();和virtual ~CSgAnalyseStatBase() {};的区别,编译可以通过却搞出个莫名其妙的链接错误。链接器linker需要将虚函数表vtable 放入某个object file,但是linker无法找到正确的object文件。这个错误常见于刚刚创建一系列有继承关系的class的时候,这个时候很容易忘了给base class的virtual function加上函数实现。解决办法:给基类的virtual函数加上本来就应该有的function body。当含有虚函数的类未将析构函数声明为virtual时也会出现这个链接错误。

 而我自己遇到的问题。。。。。

 posted on 2010-07-13 16:58  chao_yu  阅读(4700)  评论(0编辑  收藏  举报