这段程序错再哪里呢

飘荡的心 2008-09-16 09:54:36
//person.h
#ifndef PERSON_H_
#define PERSON_H_
#include<string>

class Person
{
private:
static const int LIMIT=25;
string lname;
char fname[LIMIT];
public:
Person()
{
lname=" ";
fname[0]='\0';
}
Person(const string & ln,const char * fn="Heyyou");
void Show() const;
void FormalShow() const;
};

#endif
--------------------------------------
//person.cpp
#include<iostream>
#include<string>
#include<cstring>
#include"person.h"
using namespace std;
Person::Person(const string & ln,const char * fn)
{
lname=ln;
strncpy(fname,fn,24);
fname[24]='\0';
}

void Person::Show() const
{
cout<<"Fullname: "<<fname<<" "<<lname<<endl<<endl;
}

void Person::FormalShow() const
{
cout<<"Formal show name: "<<lname<<" "<<fname<<endl<<endl;
}
...全文
280 34 打赏 收藏 转发到动态 举报
写回复
用AI写文章
34 条回复
切换为时间正序
请发表友善的回复…
发表回复
飘荡的心 2008-09-16
  • 打赏
  • 举报
回复
感谢24 25楼
飘荡的心 2008-09-16
  • 打赏
  • 举报
回复
[Quote=引用 29 楼 flight_lcf 的回复:]
引用 23 楼 CjiajiaILOVEU 的回复:
引用 22 楼 flight_lcf 的回复:
LZ,你双击提示的错误,就可以找到出现错误的所在行。



你不要一直在说废话行不

不行和LZ争辩,只是希望自己多多思考,可以自己解决。

建议自己先多看看书吧,包括C++语法,调试等等
另外,提问方法也建议进行一下提高。
[/Quote]

我也希望你能提高一下你的回答水平
太乙 2008-09-16
  • 打赏
  • 举报
回复
当然,还得加上析构函数,要不内存泄露了!




//header.h
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
#include <iostream>
using namespace std;
class Person
{
private:
static const int LIMIT;
string lname;
char* fname; //这么用!
public:
Person()
{
fname = new char[LIMIT];
lname=" ";
fname[0]='\0';
}
~Person()
{
delete [] fname;
}
Person(const string & ln,const char * fn="Heyyou");
void Show() const;
void FormalShow() const;
};

#endif


//main.cpp

#include <cstring>
#include "String.h"
using namespace std;
const int Person::LIMIT=25;
Person::Person(const string & ln,const char * fn)
{
fname = new char[LIMIT];
lname=ln;
strncpy(fname,fn,24);
fname[24]='\0';
}

void Person::Show() const
{
cout <<"Fullname: " <<fname <<" " <<lname <<endl <<endl;
}

void Person::FormalShow() const
{
cout <<"Formal show name: " <<lname <<" " <<fname <<endl <<endl;
}



我就是明明 2008-09-16
  • 打赏
  • 举报
回复
好像不能在类中这样赋值到一个static int吧
[Quote=引用 13 楼 flight_lcf 的回复:]
引用 12 楼 cwc270 的回复:
这里有错
class Person
{
...
static const int LIMIT=25;

...
}
静态变量不是这么赋初值的

应该是
class Person
{
...
static const int LIMIT;

...
}
然后在cpp文件开头
const int Person ::LIMIT=25;

static const int LIMIT=25; 这样也是可以的。
[/Quote]
flight_lcf 2008-09-16
  • 打赏
  • 举报
回复
[Quote=引用 23 楼 CjiajiaILOVEU 的回复:]
引用 22 楼 flight_lcf 的回复:
LZ,你双击提示的错误,就可以找到出现错误的所在行。



你不要一直在说废话行不
[/Quote]
不行和LZ争辩,只是希望自己多多思考,可以自己解决。

建议自己先多看看书吧,包括C++语法,调试等等
另外,提问方法也建议进行一下提高。
太乙 2008-09-16
  • 打赏
  • 举报
回复


//header.h
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
#include <iostream>
using namespace std;
class Person
{
private:
static const int LIMIT;
string lname;
char* fname; //这么用!
public:
Person()
{
fname = new char[LIMIT];
lname=" ";
fname[0]='\0';
}
Person(const string & ln,const char * fn="Heyyou");
void Show() const;
void FormalShow() const;
};

#endif


//main.cpp

#include <cstring>
#include "String.h"
using namespace std;
const int Person::LIMIT=25;
Person::Person(const string & ln,const char * fn)
{
fname = new char[LIMIT];
lname=ln;
strncpy(fname,fn,24);
fname[24]='\0';
}

void Person::Show() const
{
cout <<"Fullname: " <<fname <<" " <<lname <<endl <<endl;
}

void Person::FormalShow() const
{
cout <<"Formal show name: " <<lname <<" " <<fname <<endl <<endl;
}



xf_pan 2008-09-16
  • 打赏
  • 举报
回复
我给你调试过,
头文件中添加 using namespace std;
就没问题了,
vs2005..
wangchacha 2008-09-16
  • 打赏
  • 举报
回复
static const int LIMIT=25;将留下声明,把赋值放到.cpp文件里的类外试试。
太乙 2008-09-16
  • 打赏
  • 举报
回复

//header.h
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
#include <iostream>
using namespace std;
class Person
{
private:
static const int LIMIT;
string lname;
char fname[LIMIT]; //最好不要这么用!
public:
Person()
{
lname=" ";
fname[0]='\0';
}
Person(const string & ln,const char * fn="Heyyou");
void Show() const;
void FormalShow() const;
};

#endif


//main.cpp

#include <cstring>
#include "String.h"
using namespace std;
const int Person::LIMIT=25;
Person::Person(const string & ln,const char * fn)
{
lname=ln;
strncpy(fname,fn,24);
fname[24]='\0';
}

void Person::Show() const
{
cout <<"Fullname: " <<fname <<" " <<lname <<endl <<endl;
}

void Person::FormalShow() const
{
cout <<"Formal show name: " <<lname <<" " <<fname <<endl <<endl;
}
wangdeqie 2008-09-16
  • 打赏
  • 举报
回复

//改成这样
//person.h
#ifndef PERSON_H_
#define PERSON_H_
#include <string>
using namespace std;

class Person
{
private:
static const int LIMIT;
string lname;
char *fname;
public:
Person()
{
fname=new char[LIMIT];
lname=" ";
fname[0]='\0';
}
Person(const string & ln,const char * fn="Heyyou");
void Show() const;
void FormalShow() const;
};
const int Person::LIMIT=25;

#endif





//person.cpp
#include <iostream>
#include <string>
#include <cstring>
#include"person.h"
using namespace std;
Person::Person(const string & ln,const char * fn)
{
lname=ln;
strncpy(fname,fn,24);
fname[24]='\0';
}

void Person::Show() const
{
cout <<"Fullname: " <<fname <<" " <<lname <<endl <<endl;
}

void Person::FormalShow() const
{
cout <<"Formal show name: " <<lname <<" " <<fname <<endl <<endl;
}
void main()
{
Person p;
}
飘荡的心 2008-09-16
  • 打赏
  • 举报
回复
[Quote=引用 22 楼 flight_lcf 的回复:]
LZ,你双击提示的错误,就可以找到出现错误的所在行。
[/Quote]


你不要一直在说废话行不
flight_lcf 2008-09-16
  • 打赏
  • 举报
回复
LZ,你双击提示的错误,就可以找到出现错误的所在行。
ncy_wisdom 2008-09-16
  • 打赏
  • 举报
回复
你可以用
#define LIMIT 25
zclever 2008-09-16
  • 打赏
  • 举报
回复
#endif是结束#ifndef的。不过最好放在#define 后面。
另外,

private:
static const int LIMIT=25;
string lname;
char fname[LIMIT];


static const int LIMIT=25;也是可以的,最新的VS2008支持,但是VC6.0不支持。

飘荡的心 2008-09-16
  • 打赏
  • 举报
回复
--------------------Configuration: zhidao1 - Win32 Debug--------------------
Compiling...
zhidao1.cpp
e:\cjiajiaread\person.h(20) : error C2258: illegal pure syntax, must be '= 0'
e:\cjiajiaread\person.h(20) : error C2252: 'LIMIT' : pure specifier can only be specified for functions
e:\cjiajiaread\person.h(22) : error C2065: 'LIMIT' : undeclared identifier
e:\cjiajiaread\person.h(22) : error C2057: expected constant expression
e:\cjiajiaread\person.h(22) : warning C4200: nonstandard extension used : zero-sized array in struct/union
e:\cjiajiaread\zhidao1.cpp(11) : error C2664: 'strncpy' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
执行 cl.exe 时出错.

zhidao1.obj - 1 error(s), 0 warning(s)
飘荡的心 2008-09-16
  • 打赏
  • 举报
回复
11 12的楼的有自己调式过吗,不行啊
herman~~ 2008-09-16
  • 打赏
  • 举报
回复
上点编译器提示吧
wangchacha 2008-09-16
  • 打赏
  • 举报
回复
缺少对void Show() const;
void FormalShow() const; 的声明吧?
whuyotc 2008-09-16
  • 打赏
  • 举报
回复
#ifndef
#define

//头文件的声明程序

#endif


这样是防止重复包括

[Quote=引用 3 楼 lann64 的回复:]
.h文件也需要
using namespace std;

还有那个#endif是干嘛的?
[/Quote]
flight_lcf 2008-09-16
  • 打赏
  • 举报
回复
fname[24]='\0';
应该变更为
fname[25]='\0';
加载更多回复(14)

64,685

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧