不会编译,解析错误? [英] won't compile, parse error?

查看:80
本文介绍了不会编译,解析错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从g ++指向我的捕获行得到一个解析错误,并且不能
弄清楚这段代码有什么问题:


#包括BigPosInt.h

#include< iostream>

#include< new>

#include< assert.h> ;


BigPosInt :: BigPosInt(int init_max_digits)

{

断言(init_max_digits> 0);


试试

{

digitsArray = new int [init_max_digits];

}

catch(bad_alloc a)

{

const char * temp = a.what();

std :: cout< ;<" \ n \ n"<< temp<<" \ n" ;;

std :: cout<<"可用内存不足。 \ n \\ n'n" ;;

}


//后置条件:构造的BigPosInt已初始化为

//代表值0并且可以容纳任何

// BigPosInt最多有init_max_digits数字

}


任何想法?

thanx,

克里斯托弗

I am getting a parse error from g++ pointing at my catch line and can''t
figure out whats wrong with this code:

#include "BigPosInt.h"
#include <iostream>
#include <new>
#include <assert.h>

BigPosInt::BigPosInt(int init_max_digits)
{
assert(init_max_digits > 0);

try
{
digitsArray = new int[init_max_digits];
}

catch(bad_alloc a)
{
const char* temp = a.what();
std::cout<<"\n\n"<<temp<<"\n";
std::cout<<"Insufficient free memory. \n\n";
}

// Postcondition: constructed BigPosInt has been initialized to
// represent the value 0 and can accommodate any
// BigPosInt that has up to init_max_digits digits
}

Any ideas?
thanx,
Christopher

推荐答案

2003年9月24日星期三04:17:17 GMT ,克里斯托弗, < CP *** @ austin.rr.com>写道:
On Wed, 24 Sep 2003 04:17:17 GMT, "Christopher" <cp***@austin.rr.com> wrote:
我从g ++指向我的捕获线得到一个解析错误,并且无法弄清楚这段代码是什么错误:

#include" BigPosInt.h"
#include< iostream>
#include< new>
#include< assert.h>


为了养成习惯,请尝试使用< cassert>而不是< assert.h>。

因为''断言''是一个宏,它实际上是相同的。但不是为了

其他旧的C库标题。

BigPosInt :: BigPosInt(int init_max_digits)


这里我会用到的'init_max_digits''的无符号类型。但是,大约50%(据我所知)社区不同意这一点。

50%左右的人同意认为''unsigned''传达了

必须是非负面的要求更清楚。


{
断言(init_max_digits> 0);


对于''unsigned''你想要这个''断言(init_max_digits< = INT_MAX)'',

只是为了为了安全起见。


尝试
; NumbersArray = new int [init_max_digits];


假设''digitsArray''是会员变量。

}

catch(bad_alloc a)


使''catch(std :: bad_alloc const& a)''


{
const char * temp = a.what() ;


这里你不需要一个临时变量。


std :: cout<<" \ n\\\
" ;<<<<<" \ n";
std :: cout<<"可用内存不足。 \\\
\\\
英寸;


请记住重新抛出异常!


你可能也会在这里冲洗std :: cout更好。

std :: cout<< std :: flush;

throw;

}

//后置条件:构造的BigPosInt已初始化为
//代表值0并且可以容纳任何
// BigPosInt最多为init_max_digits数字


如果你不重新抛出异常,你将违反
$ b如果发生异常,$ b后置条件。

}
I am getting a parse error from g++ pointing at my catch line and can''t
figure out whats wrong with this code:

#include "BigPosInt.h"
#include <iostream>
#include <new>
#include <assert.h>
Just to get into the habit try using <cassert> instead of <assert.h>.
Since ''assert'' is a macro it''s practically the same. But not for
other old C library headers.
BigPosInt::BigPosInt(int init_max_digits)
Here I would have used unsigned type for ''init_max_digits''. However,
about 50% (as far as I can determine) of the community disagrees.
The 50% or so who agree think that ''unsigned'' communicates the
"must be non-negative" requirement more clearly.

{
assert(init_max_digits > 0);
For ''unsigned'' you''d want to make this ''assert( init_max_digits <= INT_MAX )'',
just to be on the safe side.

try
{
digitsArray = new int[init_max_digits];
Assuming ''digitsArray'' is a member variable.
}

catch(bad_alloc a)
Make that ''catch( std::bad_alloc const& a )''

{
const char* temp = a.what();
You don''t need a temp variable here.

std::cout<<"\n\n"<<temp<<"\n";
std::cout<<"Insufficient free memory. \n\n";
Remember to rethrow the exception!

You''d probably also be better off flushing std::cout here.
std::cout << std::flush;
throw;
}

// Postcondition: constructed BigPosInt has been initialized to
// represent the value 0 and can accommodate any
// BigPosInt that has up to init_max_digits digits
If you don''t rethrow the exception you will violate that
postcondition in case of an exception.
}






文章< hV **** ****************@twister.austin.rr.com> ;,克里斯托弗写道:
In article <hV********************@twister.austin.rr.com>, Christopher wrote:
我从g ++指向我的捕获线得到一个解析错误并且不能弄明白这段代码有什么问题:
我不是一个挑剔的人,但你可能想要通过完整的

诊断错误下一次。
#include" BigPosInt.h"
#include< iostream>
#include< new>
#include< assert.h>

BigPosInt :: BigPosInt(int init_max_d igits)
{
断言(init_max_digits> 0);

尝试
{digits / new int [init_max_digits];
}

catch(bad_alloc a)
catch(std :: bad_alloc a)

//我看不到使用命名空间std :: bad_alloc

//或使用命名空间std;等等。

{
const char * temp = a.what();
std :: cout<<" \ n \ n"<< ; temp<<<" \ n";
std :: cout<<"可用内存不足。 // @后置条件:构造的BigPosInt已初始化为
//代表值0并且可以容纳任何
/ / BigPosInt最多有init_max_digits数字
}

任何想法?


有一个适用于你/ b
比克斯,
克里斯托弗
I am getting a parse error from g++ pointing at my catch line and can''t
figure out whats wrong with this code: I''m not one to be nit-picky but you might want to past the complete
diagnostic error next time.
#include "BigPosInt.h"
#include <iostream>
#include <new>
#include <assert.h>

BigPosInt::BigPosInt(int init_max_digits)
{
assert(init_max_digits > 0);

try
{
digitsArray = new int[init_max_digits];
}

catch(bad_alloc a) catch(std::bad_alloc a)
// I see no using namespace std::bad_alloc
// or using namespace std; etc..
{
const char* temp = a.what();
std::cout<<"\n\n"<<temp<<"\n";
std::cout<<"Insufficient free memory. \n\n";
}

// Postcondition: constructed BigPosInt has been initialized to
// represent the value 0 and can accommodate any
// BigPosInt that has up to init_max_digits digits
}

Any ideas?
There''s one for ya
thanx,
Christopher



希望有所帮助,

Chris Johnson

-

echo" qr ********* @ rkpvgr.pbz" | rot13

----- =通过Newsfeeds.Com发布,未经审查的Usenet新闻= -----
http://www.newsfeeds.com - 世界排名第一的新闻组服务!

----- ==超过100,000新闻组 - 19种不同的服务器! = -----


Hope that helps,
Chris Johnson
--
echo "qr*********@rkpvgr.pbz" | rot13
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


Alf P. Steinbach写道:

[SNIP]
Alf P. Steinbach wrote:
[SNIP]
BigPosInt :: BigPosInt(int init_max_digits)
这里我会使用''init_max_digits''的无符号类型。然而,大约50%(据我所知)社区不同意。
50%左右同意认为''未签名''传达
必须是非负"要求更清楚。
BigPosInt::BigPosInt(int init_max_digits)
Here I would have used unsigned type for ''init_max_digits''. However,
about 50% (as far as I can determine) of the community disagrees.
The 50% or so who agree think that ''unsigned'' communicates the
"must be non-negative" requirement more clearly.




大多数平台都不同意你的看法。他们不能分配或处理比bd :: ptrdiff_t的最大值更多的字节,并且std :: ptrdiff_t已签名。



Most of the platforms don''t agree with you. They cannot allocate or address
more bytes than the maximum of std::ptrdiff_t, and std::ptrdiff_t is signed.

std :: cout<<" \ n \ n"<< temp<<" \ n";
std :: cout<<"空闲内存不足。 \ n \\ nnn ;;
std::cout<<"\n\n"<<temp<<"\n";
std::cout<<"Insufficient free memory. \n\n";



请记住重新抛出异常!

你可能还会更好地在这里刷新std :: cout。



Remember to rethrow the exception!

You''d probably also be better off flushing std::cout here.




为什么不简单地使用endl;以上?或者更好地关闭cerr?

std :: cout<< std :: flush;
throw;



Why not simply use endl; above? Or better off cerr?
std::cout << std::flush;
throw;




Rethrowing是个好点。


-

Attila aka WW



Rethrowing is a good point.

--
Attila aka WW


这篇关于不会编译,解析错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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