请帮忙(新手) [英] please help (novice)

查看:63
本文介绍了请帮忙(新手)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我是C ++初学者,我自己从一本书中学习,而且我已经遇到了很多问题。如果可能的话,那里有人可能

帮助我..


问题如下......我试过创建一对字符串

类/函数..并且它们确实编译..但是当我运行它们时..窗口

突然报错...因为我只是一个初学者在C ++中,没有人

在我身边帮忙..如果有人可以帮助我,我会很感激!


简单(简化)代码是以下内容:

#include< iostream.h>

#include< stdio.h>

#include< stdlib.h> ; // for EXIT_SUCCESS

#include< string.h>


class string {

public:

string(void){characters = 0; pt = NULL;}

string(char * s);

private:

int characters;

char * pt;

};


string :: string(char * s){

characters = strlen(s);

pt = new char [characters];

if(pt = NULL){

cerr<< 无法分配string.\ n;

退出(EXIT_FAILURE);

}

memcpy(pt,s,characters) ;

}


void main()

{

string s1(" My first string");

}

如果你编译它,你会看到编译器没有报告任何

错误。但是当它运行时,你会看到你突然收到一个

windows错误报告..


有人知道这个错误的来源是什么关于上面的代码可能是什么以及怎么回事?


谢谢!!

解决方案
"测距仪" < SG **** @ msn.com>在消息中写道

news:ab ************************** @ posting.google.c om ...



我是C ++的初学者,从书中自学,我已经遇到了一些问题..如果可能的话有人在那里可能会帮我解决..

问题如下......我试图创建一些字符串
类/函数..和他们确实编译..但是当我运行它们时..窗口
突然报告错误..因为我只是一个C ++的初学者,没有人在我身边帮助..我会感谢有人可以帮助我!

简单(简化)代码如下:

#include< iostream.h>
#include< stdio.h>
#include< stdlib.h> //对于EXIT_SUCCESS
#include< string.h>

类字符串{
public:
string(void){characters = 0; pt = NULL;}
string(char * s);
私人:
int characters;
char * pt;
};

string :: string(char * s){
characters = strlen(s);
pt = new char [characters];
if(pt = NULL){
cerr << 无法分配string.\ n;
退出(EXIT_FAILURE);
}
memcpy(pt,s,characters);
}

void main()
{
字符串s1(我的第一个字符串);
}

如果你编译它,你会看到编译器没有报告任何错误..但是在运行它时,你会看到你突然收到一个
windows错误报告..

有人知道吗这个错误的来源可能是什么,以及如何改变上面的代码?

谢谢!




我'很遗憾告诉你这个,但你需要一本更新/更好的书。来自

您显示的代码,有很多错误。在这里处理太多

,基本上没有重写你拥有的一切。作为一个初学者,你不应该使用大部分你正在使用的东西。基础知识首先,你知道。

我会建议你忘记你认为你知道什么,得到一本更好的书(你用什么

,BTW?)另一方面,如果这是某种巨魔---做得好!然后离开。

-

Gary


ranger写道:


[...]

string :: string(char * s){
characters = strlen(s);
pt = new char [characters];
if(pt = NULL){
^^^

这是一项作业,而不是比较。

cerr<< 无法分配string.\ n;
退出(EXIT_FAILURE);
}
memcpy(pt,s,characters);
}


[...]

如果你编译它,你会发现编译器没有报告任何错误..但是在运行时,你会看到你突然收到一个
Windows错误报告..




增加编译器的警告级别。然后你可能会看到一个关于在比较环境中做作业的

警告。


-

Dirk


(PGP keyID:0x448BC5DD - http://www.gnupg .org - http://www.pgp.com


..oO°死亡是一种不可屏蔽的中断。 °Oo。


*游侠:



我是C ++的初学者,在我身上学习拥有一本书,我已经遇到了一些问题..如果可能的话,那里的某个人可能会帮助我......

问题如下......我试图创建几个字符串
类/函数..然后它们会编译..但是当我运行它们时... windows
突然报告错误..因为我只是一个C ++的初学者,没有人在我身边帮忙..如果有人可以帮助我,我会很感激!

简单(简化)代码如下:

#include< iostream.h>


#include< iostream>


< iostream.h>不是标准的C ++标题。

#include< stdio.h>
#include< stdlib.h> //对于EXIT_SUCCESS
#include< string.h>


#include< cstdio>

#include< cstdlib>

#include< cstring>


是这三个标题的首选样式。

类字符串{
public:
string(void){characters = 0; pt = NULL;}


''void''作为参数列出一个C''主义;不要用它。


使用初始化列表而不是赋值,


string():characters(0),pt( NULL){}


string(char * s);


参数应该是常量:


string(char const * s);


你还需要一个析构函数来解除分配字符串。


你需要一个赋值运算符来复制字符串,并且

a复制构造函数。


private:
int characters;
char * pt;
};

string :: string(char * s){


string :: string(char const * s)

characters = strlen(s);
pt = new char [characters];
if(pt = NULL){


在标准C ++中,此测试将始终失败。运营商''新''报告

通过std :: bad_alloc异常失败,而不是返回NULL,

所以即使它正确地反映了什么,测试总是会失败

你打算写。但它甚至不是那样的:它是一项任务,

不是比较。


cerr<< 无法分配string.\ n;


这里你需要使用''std''命名空间来限定:


std :: cerr<< 无法分配字符串。\ n;;


但是,向用户报告失败并不是一个好主意;

而不是使用例外来向失败者报告失败。


退出(EXIT_FAILURE);
}
memcpy(pt,s,characters);


如果你似乎不打算在你的字符串表示中有一个终止的

零字节,这是可以的。但是,这就是你b $ b遇到崩溃的地方。这是由于上面提到的早期错误。

}

void main()


''main''必须有返回类型''int''。

{
string s1(" My first string");
}



-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么这么糟糕?

A:热门发布。

问:usenet和电子邮件中最烦人的事情是什么?


Hi,

I''m a beginner with C++, studying on my own from a book and I''ve
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I''ve tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I''m just a beginner in C++, with noone
around me to help.. I''d appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>

class string{
public:
string(void){characters = 0; pt = NULL;}
string(char *s);
private:
int characters;
char *pt;
};

string::string(char *s){
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);
}

void main()
{
string s1("My first string");
}
If you compile it, you''ll see that the compiler doesn''t report any
error.. but when running it, you''ll see that you suddenly get a
windows error report..

Does someone know what the source of this fault might be and what to
change about the code above?

Thanks!!

解决方案

"ranger" <sg****@msn.com> wrote in message
news:ab**************************@posting.google.c om...

Hi,

I''m a beginner with C++, studying on my own from a book and I''ve
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I''ve tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I''m just a beginner in C++, with noone
around me to help.. I''d appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>

class string{
public:
string(void){characters = 0; pt = NULL;}
string(char *s);
private:
int characters;
char *pt;
};

string::string(char *s){
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);
}

void main()
{
string s1("My first string");
}
If you compile it, you''ll see that the compiler doesn''t report any
error.. but when running it, you''ll see that you suddenly get a
windows error report..

Does someone know what the source of this fault might be and what to
change about the code above?

Thanks!!



I''m really sorry to tell you this, but you need a newer/better book. From
the code you show, there are many things wrong. Too many to deal with here
without basically rewriting everything you have. As a beginner you shouldn''t
be using most of what you are using. Basics come first, you know.
I''d advise you to forget what you think you know, get a better book (what
are you using, BTW?) and start fresh.

On the other hand, if this is some kind of troll --- Well Done! and go away.
--
Gary


ranger wrote:

[...]

string::string(char *s){
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){ ^^^
This is an assignment, not a comparison.
cerr << "Failed to allocate string.\n";
exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);
}
[...]
If you compile it, you''ll see that the compiler doesn''t report any
error.. but when running it, you''ll see that you suddenly get a
windows error report..



Increase the warning level of your compiler. Then you''ll probably see a
warning about doing an assignment in a comparison context.

--
Dirk

(PGP keyID: 0x448BC5DD - http://www.gnupg.org - http://www.pgp.com)

..oO° Death is a non-maskable interrupt. °Oo.


* ranger:

Hi,

I''m a beginner with C++, studying on my own from a book and I''ve
encoutered quite some problems.. If possible, someone out there might
help me out..

The problem is the following.. I''ve tried to create a couple of string
class/functions.. and they do compile.. but when I run them.. windows
suddenly reports an error.. As I''m just a beginner in C++, with noone
around me to help.. I''d appreciate if someone might help me out!

The simple (reduced) code is the following:
#include <iostream.h>
#include <iostream>

<iostream.h> is not a standard C++ header.
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>

is the preferred style for these three headers.
class string{
public:
string(void){characters = 0; pt = NULL;}
''void'' as argument list a C''ism; don''t use it.

Use an initializer list instead of assignment,

string(): characters(0), pt(NULL) {}

string(char *s);
Argument should be const:

string( char const* s );

You also need a destructor to deallocate the string.

And you need an assignment operator to copy the string, and
a copy constructor.

private:
int characters;
char *pt;
};

string::string(char *s){
string::string( char const* s )
characters = strlen(s);
pt = new char[characters];
if (pt = NULL){
In standard C++ this test will always fail. Operator ''new'' reports
failure by means of std::bad_alloc exception, not by returning NULL,
so the test would always fail even if it correctly reflected what
you intended to write. But it doesn''t even that: it''s an assignment,
not a comparision.

cerr << "Failed to allocate string.\n";
Here you need to qualify using the ''std'' namespace:

std::cerr << "Failed to allocate string.\n";

But it''s not a good idea to do i/o to report failure to the user;
instead use exceptions to report the failure up to the caller.

exit(EXIT_FAILURE);
}
memcpy(pt, s, characters);
This is OK if, as seems likely, you don''t intend to have a terminating
zero byte in your string representation. However, this is where you
get a crash. That''s due to the earlier error noted above.
}

void main()
''main'' must have return type ''int''.
{
string s1("My first string");
}



--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


这篇关于请帮忙(新手)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆