C++问题 高手帮帮忙

来源:百度知道 编辑:UC知道 时间:2024/09/25 14:40:39
下面程序出错了

#include<iostream>
#include<cstddef>
using namespace std;

template<class T,int sz=1> class PWrap {
T* ptr;
public:
class RangeError {}; // Exception class
PWrap (){
ptr = new T[sz];
cout << "PWrap constructor" << endl;
}
~PWrap() {
delete[] ptr;
cout << "PWrap destutctor" << endl;
}
T& operator[](int i) throw(RangeError) {
if(i >=0 && i < sz) return ptr[i];
throw RangeError();
}
};

class Cat {
public:
Cat() {cout<< "Cat()" << endl; }
~Cat() {cout << "~Cat()" << endl;}
void g(){}
};

class Dog {
public:
void* operator new[](size_t) {
cout << "Allocating a Dag" << endl;
throw 47;
}
void operator delete[](void* P) {
cout <<

::operator delete[](P); /*这句出错了。。说是全局名字空间没有成员函数delete[] 这是怎么回事?*/
把这句改成
delete[](P);就行了

delete并不是全局函数
给你看个例子
int main()
{
int *p = new int(0);
delete(p);
}
这个程序可以运行 而不需要头文件 也没有申明域名
说以用::来调用delete[]是错的