使用std时候遇到的问题

dahuatttt 2008-05-14 10:36:38
先谢谢各位,一直以来在CSDN上受到帮助。

写个栈的类模板
//stack.h
#ifndef _STACK_TTTT_H_
#define _STACK_TTTT_H_
namespace cpp3
{
#include <string.h>
//#include <iostream>
//using namespace std;

template <class Type>
class Stack
{
int stackLarger;
int num;
Type * pS;
public:
Stack(int large=50);
bool initial();
bool push(Type &);
bool pop(Type &);
bool pop();
bool isEmpty();
~Stack();
};

template <class Type>
Stack<Type>::Stack(int large)
{
stackLarger=large;
num=0;
pS=NULL;
}

template <class Type>
bool Stack<Type>::initial()
{
pS=new Type[stackLarger];
if(pS==NULL)
return false;
return true;
}

template <class Type>
bool Stack<Type>::push(Type & t)
{
if (stackLarger==num)
return false;
memcpy((void *)(pS+stackLarger-num-1),&t,sizeof(Type));
num++;
return true;
}

template <class Type>
bool Stack<Type>::pop(Type & t)
{
if (num==0)
return false;
memcpy(&t,(void *)(pS+stackLarger-num),sizeof(Type));
num--;
return true;
}

template <class Type>
bool Stack<Type>::pop()
{
if (num==0)
return false;
num--;
return true;
}

template <class Type>
bool Stack<Type>::isEmpty()
{
return (bool)num;
}

template <class Type>
Stack<Type>::~Stack()
{
if(pS!=NULL)
{
//if (num!=0)
// throw std::runtime_error("The stack is not empty,it will be error when the stack has the member of point!please check it again.");
delete pS;
}
}
}
#endif

1:想在~Stack中使用抛异常,但是一写就报错,参见注释掉的这几行:
//#include <iostream>
//using namespace std;
//if (num!=0)
// throw std::runtime_error("The stack is not empty,it will be error when the stack has the member of point!please check it again.");

2:使用以下方式包含上面的模板类后的问题
#include "Stack\stack.h"
using namespace cpp3;

在main中使用的时候
Stack<int> p;
VC6.0下p后面只能点出私有成员列表,却点不出成员函数列表,不知道是我写得问题大还是VC有问题?
...全文
257 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
dahuatttt 2008-05-30
  • 打赏
  • 举报
回复
还是没有结果
fengying616 2008-05-18
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 dahuatttt 的回复:]
throw runtime_error("The stack is not empty,it will be error when the stack has the member of point!please check it again.");
在我把这句话换成下面这句后就好了。。。
throw(runtime_error("The stack is not empty,it will be error when the stack has the member of point!please check it again."));

难道throw runtime_error("XX");的写法不对么?
必须用throw(runtime_error(""));这个写法吗?
[/Quote]

runtime_error是在标准命名空间std下,当然的加个std::
至于throw(runtime_error("The stack is not empty,it will be error when the stack has the member of point!please check it again."))能通过编译,我就看不懂了。
dahuatttt 2008-05-15
  • 打赏
  • 举报
回复
throw runtime_error("The stack is not empty,it will be error when the stack has the member of point!please check it again.");
在我把这句话换成下面这句后就好了。。。
throw(runtime_error("The stack is not empty,it will be error when the stack has the member of point!please check it again."));

难道throw runtime_error("XX");的写法不对么?
必须用throw(runtime_error(""));这个写法吗?
机智的呆呆 2008-05-15
  • 打赏
  • 举报
回复
使用c++模板建议使用vc8.0或就9。0,6.0对模板的支持不是很好~~~~~~~~~
xushengcn 2008-05-15
  • 打赏
  • 举报
回复
vc6好像对ANIS/ISO的标准支持的不是很好吧,是否跟这个有关系,应该不是语法问题。
dahuatttt 2008-05-14
  • 打赏
  • 举报
回复
谢谢fengying616
放在外面后错误提示变成了如下:
d:\linuxwork\test\stack\stack.h(86) : error C2065: 'runtime_error' : undeclared identifier
d:\develop\microsoft visual studio\vc98\include\xstring(133) : while compiling class-template member function '__thiscall cpp3::Stack<int>::~cpp3::Stack<int>(void)'

在namespace外面已经写了#include <stdexcept>
fengying616 2008-05-14
  • 打赏
  • 举报
回复
把include预编译指令放在你定义的命名空间外面
fengying616 2008-05-14
  • 打赏
  • 举报
回复
把头文件放在你定义的命名空间外面,一切编译错误都没有了。
dahuatttt 2008-05-14
  • 打赏
  • 举报
回复
谢谢Treazy的指正
我修改成下面的代码
#include <stdexcept>
throw runtime_error("The stack is not empty,it will be error when the stack has the member of point!please check it again.");

不知道对不对?我运行了后出现了下面的问题
d:\develop\microsoft visual studio\vc98\include\exception(63) : error C2039: 'exception' : is not a member of '`global namespace''
d:\develop\microsoft visual studio\vc98\include\exception(63) : error C2873: 'exception' : symbol cannot be used in a using-declaration
Treazy 2008-05-14
  • 打赏
  • 举报
回复
另外如果使用runtime_error类
需要包含头文件
#include <stdexcept>
Treazy 2008-05-14
  • 打赏
  • 举报
回复
是你写的问题!

class runtime_error : public exception {
public:
runtime_error(const string& message);
};
这是runtime_error的原形定义

你这么写std::runtime_error(...)是什么玩意?

1、本课程是一个干货课程,主要讲解如何封装服务器底层,使用Tcp/ip长连接,IDE使用vs2019 c++开发以及使用c++11的一些标准,跨平台windows和linux,服务器性能高效,单服务器压力测试上万无压力,服务器框架是经历过上线产品的验证,框架简单明了,不熟悉底层封装的人,半个小时就能完全掌握服务器框架上手写业务逻辑。2、本课程是一个底层服务器框架教程,主要是教会学员在windows或linux下如何封装一个高效的,避免踩坑的商业级框架,服务器底层使用初始化即开辟内存的技术,使用内存池,服务器运行期间内存不会溢出,非常稳定,同时服务器使用自定义哈希hashContainer,在处理新的连接,新的数据,新的封包,以及解包,发包,粘包的过程,哈希容器性能非常高效,增、删、查、改永远不会随着连接人数的上升而降低性能,增、删、查、改的复杂度永远都是恒定的O(1)。3、服务器底层封装没有使用任何第三方网络库以及任何第三方插件,自由度非常的高,出了任何BUG,你都有办法去修改,查找问题也非常方便,在windows下使用iocp,linux下使用epoll.4、讲解c++纯客户端,主要用于服务器之间通信,也就是说你想搭建多层结构的服务器,服务器与服务器之间使用socket通信。还可以使用c++客户端做压力测试,开辟多线程连接服务器,教程提供了压力测试,学员可以自己做压力测试服务器性能。5、赠送ue4和unity3d通信底层框架以及多人交互demo,登录,注册,玩家离开,同步主要是教会学员服务器与客户端如何交互。6、赠送c++连接mysql数据库框架demo,登录,注册,玩家离开数据持久化.7、服务器教程使用自定义通信协议,同时也支持protobuf,选择权在开发者自己手里,想用什么协议都可以,自由度高。8、服务器教程使用手动敲代码逐句讲解的方式开展教学课程。非喜勿喷,谢谢大家。9、服务器教程提供源码,大家可以在平台提供的地址下载或者联系我,服务器使用c++11部分标准,std::thread,条件变量,线程锁,智能指针等,需要学员具备一定c++知识,购买前请慎重考虑。

64,662

社区成员

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

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