这是书上的代码,为何我编译通不过。
#include <iostream>
using namespace std;class Hello
{
public:
static int count;
Hello(){count ++;}
~Hello(){count --;}
}main( )
{
Hello::count =0;
cout<<Hello::count;
Hello *h1=new Hello;
cout<<Hello::count;
Hello *h2=new Hello;
cout<<Hello::count;
delete h1;
cout<<Hello::count;
delete h2;
cout<<Hello::count;
}
错误如下:
正在编译...
test.cpp
c:\6\test.cpp(14) : warning C4326: “main”的返回类型应为“int 或 void”而非“Hello”
正在链接...
test.obj : error LNK2001: 无法解析的外部符号 "public: static int Hello::count" (?count@Hello@@2HA)
Debug/6.exe : fatal error LNK1120: 1 个无法解析的外部命令
#include <iostream>
using namespace std;class Hello
{
public:
static int count;
Hello(){count ++;}
~Hello(){count --;}
}main( )
{
Hello::count =0;
cout<<Hello::count;
Hello *h1=new Hello;
cout<<Hello::count;
Hello *h2=new Hello;
cout<<Hello::count;
delete h1;
cout<<Hello::count;
delete h2;
cout<<Hello::count;
}
错误如下:
正在编译...
test.cpp
c:\6\test.cpp(14) : warning C4326: “main”的返回类型应为“int 或 void”而非“Hello”
正在链接...
test.obj : error LNK2001: 无法解析的外部符号 "public: static int Hello::count" (?count@Hello@@2HA)
Debug/6.exe : fatal error LNK1120: 1 个无法解析的外部命令
using namespace std;什么意思??
那么我们在第一个程序中为何要using namespace std;呢?其实也是为了把"std::cout"变成简洁一点的"cout"。
using namespace std;class Hello
{
public:
static int count;
Hello(){count ++;};
~Hello(){count --;};
};int Hello::count; // implement the static memberint main( )
{
Hello::count = 0;
cout< < Hello::count;
Hello *h1 = new Hello;
cout << Hello::count;
Hello *h2 = new Hello;
cout << Hello::count;
delete h1;
cout << Hello::count;
delete h2;
cout << Hello::count;
return 0;
}