的瓦楞纸自己的容器的析构函数的char *成员,直到然后我用来应付,在这里我试图建立我自己的容器类使用向量。我需要它为我的工作。我使用代码块10.05崩溃,如果删除使用矢量
class myclass
{
public :
vector<myclass> array;
char * classname;
...
的问题是,我的我的类数据如果删除不析构函数发生的以及显示在屏幕上。如果我删除classname,我的函数show()将显示奇数字符。我认为它来自我的方法来建立对象和范围的问题,当我通过他们作为参数。
myclass::~myclass()
{
//if(classname) delete [] classname;
}
这是它是如何在初始化的构造函数:
myclass::myclass(long lvl = 0, const char name[] = "undefined")
:ID(++ ID_counter)
{
level = lvl;
int namelength = strlen(name);
classname = new char[namelength + 1];
strcpy(classname, name);
}
add_content(常量MyClass的& C)应该使c.array考核的元素的副本, “push_back”他们在this-> array 我发现每个机会我们可以把一个没有名字的对象作为参数:mycontainer.add_content(myclass(3,5)); 它的工作原理,但我怀疑有关的范围应该有
> int main()
> {
> myclass mycontainer(0);
> mycontainer.add_content(myclass(3,5));
> ...
这里是的全码:
#include <vector>
#include <iostream>
using namespace std;
class myclass
{
public :
vector<myclass> array;
static long ID_counter;
long ID;
long level;
char * classname;
myclass(int n, long lvl, const char name[]); //push_back n elements, lvl = 0, name = "undefined"
myclass(long lvl, const char name[]); //lvl = 0, name = "undefined"
myclass::~myclass();
void set_level(long lvl); //recursive function, go down the tree
void add(int n); //push_back n elements
void add(const myclass & c); //push_back and set back the levels
void add_content(const myclass & c); //push_back all the c.array[i] and set back the levels
void show();
template <typename T> myclass & operator[](const T it){ return array[it]; }
};
long myclass::ID_counter = 0;
myclass::myclass(long lvl = 0, const char name[] = "undefined")
:ID(++ ID_counter)
{
level = lvl;
int namelength = strlen(name);
classname = new char[namelength + 1];
strcpy(classname, name);
}
myclass::myclass(int n, long lvl, const char name[] = "undefined")
:ID(++ ID_counter)
{
level = lvl;
int namelength = strlen(name);
classname = new char[namelength + 1];
strcpy(classname, name);
for(int i = 0; i < n; i++) array.push_back(myclass(this->level + 1));
}
myclass::~myclass()
{
//if(classname) delete [] classname; //there is the point !
}
void myclass::add(int n = 1)
{
for(int i = 0; i < n; i++) array.push_back(myclass(level + 1));
}
void myclass::add(const myclass & c)
{
array.push_back(c);
array[array.size() - 1].set_level(level + 1);
}
void myclass::add_content(const myclass & c)
{
for(int i = 0; i < c.array.size(); i++)
{
array.push_back(c.array[i]);
array[array.size() - 1].set_level(level + 1);
}
}
void myclass::set_level(long lvl)
{
level = lvl;
for(int i = 0; i < array.size(); i++) array[i].set_level(level + 1);
}
void myclass::show()
{
cout << "ID : " << ID << "\tLvl : " << level << "\t Classname : " << classname << endl;
}
int main()
{
myclass mycontainer(0); //ground level
mycontainer.add_content(myclass(3,5)); //the 3 elements level 5 should be reset to 0+1
mycontainer.show();
for(int i = 0; i < mycontainer.array.size(); i++)
{
mycontainer[i].show();
for(int j= 0; j < mycontainer[i].array.size(); j++)
mycontainer[i][j].show();
}
cout << "\ncheckpoint\n";
system("PAUSE"); //press any key to crash!
return 0;
}
这里在析构函数没有删除*类名中,程序发现这个:
ID : 1 Lvl : 0 Classname : undefined
ID : 3 Lvl : 1 Classname : undefined
ID : 4 Lvl : 1 Classname : undefined
ID : 5 Lvl : 1 Classname : undefined
checkpoint
Appuyez sur une touche pour continuer...
与缺失和一些不好的字符显示了一个崩溃:
ID : 1 Lvl : 0 Classname : undefined
ID : 3 Lvl : 1 Classname : ░(?
ID : 4 Lvl : 1 Classname : ░(?
ID : 5 Lvl : 1 Classname : ░(?
checkpoint
Appuyez sur une touche pour continuer...
Process returned -1073741819 (0xC0000005) execution time : 29.651 s
Press any key to continue.
我想回去学习有关参数和指针的一些原则,但这种语言是太经验和没有意义的。感谢您的帮助。
也许对[The Rule of Three](http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming))的评论可能揭示这一点,特别是对'myclass'(如果这不明显)。 – WhozCraig