首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

大家来看看,帮一下忙解决思路

2012-03-23 
大家来看看,帮一下忙#include iostream.h#include string.hclasst{staticintxpublic:t()//这里好像

大家来看看,帮一下忙
#include <iostream.h>
#include <string.h>
class   t
{
static   int   x;
public:
t();                                 //这里好像也有问题
t(char   *s);
void   showf();
private:
char   name[10];
};
static   int   x=0;
t::t(char   *s)                                 //我认为问题主要出在这里
{
strcpy(name,s);          
}
void   t::showf()
{
cout < <name < <endl;
cout < <x < <endl;
}
void   main()
{
t   kk;
t   kkk( "kkk ");
kkk.showf();
}
大家看看这个程序,我的t类中有一个静态成员,我想在构造时直接默认为就是此成员的初始值,却遇到了错误,应该怎样改?
也不知道是不是错在这里,希望你们帮我改一下。

[解决办法]
static int x=0;

改成int t::x = 0;

另外t::t(void)
{
}
要写一下,哪怕空
[解决办法]
#include <iostream>
#include <string.h>

class t
{
static int x;
public:
t();
t(const char *s);
void showf();
private:
char name[10];
};

int t::x=0; //1.初始化静态成员变量

t::t() // 2.默认构造函数
{
}

t::t(const char *s)
{

strncpy(name,s,9); //3. 防止s长度超过name的长度
}
void t::showf()
{
std::cout < <name < <std::endl;
std::cout < <x < <std::endl;
}

int main() //4. main()返回int
{
t kk;
t kkk( "kkk ");
kkk.showf();

return 0;
}

热点排行