注册 登录
编程论坛 C++教室

这段程序谁能找出错误在哪儿?

haitao9999 发布于 2009-10-21 21:04, 617 次点击
这段程序谁能找出错误在哪儿?

#include <iostream>
using namespace std;

class counted{
    int id;
    static int count ;
public:
    counted():id(count++){
    cout<<id<<endl<<"it's being created"<<endl;
    }
    ~counted(){
    cout<<id--<<endl<<"it's being destroyed"<<endl;
    }
};


int main(){
    counted me;
    return 1;
}
MinGW下编译报错为:
--------------------Configuration: 1301 - Debug--------------------
Linking...
D:\MinGWStudio\1301\Debug\1301.o: In function `ZN7countedC1Ev':
D:\MinGWStudio\1301\1301.cpp:(.text$_ZN7countedC1Ev[counted::counted()]+0xa): undefined reference to `counted::count'
D:\MinGWStudio\1301\1301.cpp:(.text$_ZN7countedC1Ev[counted::counted()]+0x10): undefined reference to `counted::count'
collect2: ld returned 1 exit status
1301.exe - 2 error(s), 0 warning(s)

4 回复
#2
lezhe2009-10-22 14:35
count是私有成员,不能直接使用。
#3
caolihui10082009-10-22 16:06
请问counted():id(count++)是干什么用的?没见过这么用呀
#4
Tomato_fan2009-10-22 23:39
static 成员未初始化,在类外,main之前加上int counted::count=0即可
#5
baojin2009-10-25 20:25
counted():id(count++)有语法错误吧?应该是:: 2个的吧
1