polymorph对象 - 为什么不工作? [英] polymorph object - why doesn't it work???

查看:59
本文介绍了polymorph对象 - 为什么不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在解决以下问题:我需要构建一个26-nary树来保存数据字典
。单词的每个字母都由一个单元格表示(单元格

有一个指针 - 矢量单元格* children [26])。最后一个字母是一个节点

(节点:单元格,附加属性pagenr)。

现在:我理解多态对象的原理并且它运行良好

与ususal例子。但在我的情况下,它没有!没有

编译或链接错误,并且在运行时没有错误 - 至少没有显示...

这里是一些代码。也许有人可以给我一些提示...

// Trie.cpp

// ----------

(...)

void Trie :: insert(char * word,int page)

{

//第一个int值字母

int iword =((int)word [0]) - 97;


insert_one(root-> children [iword],word,page );

}


void Trie :: insert_one(Cell * new_cell,char * word,int page)

{

if(new_cell == NULL){

if(((int)word [1]) - 97< 0){// last letter

new_cell = new Node();


// new_cell-> page = page;

}

else

new_cell = new Cell;


cout<< typeid(new_cell).name()<< " - " ;;

new_cell-> display(); //单元格中的虚函数,节点中的cout pagenr

(...)


最好的问候,

mika

解决方案

Mika Vainio写道:

大家好,
我在做以下工作问题:我需要构建一个26-nary树来保存数据字典。单词的每个字母都由一个单元格表示(单元格
有一个指针 - 矢量单元格* children [26])。最后一个字母是一个节点
(节点:单元格,附加属性pagenr)。
现在:我理解了多态对象的原理,并且它与ususal示例一起工作正常。但在我的情况下,它没有!没有
编译或链接错误,并且在运行时没有错误 - 至少没有显示......
这里有一些代码。也许有人可以给我一些提示...

// Trie.cpp
// ----------
(...)
void Trie :: insert(char * word,int page)
{
//第一个字母的int值
int iword =((int)word [0]) - 97;
尝试使用宏而不是常量,如97中所示。
insert_one(root-> children [iword],word,page);
}

void Trie :: insert_one(Cell * new_cell,char * word,int page)
{
if(new_cell == NULL){
if((int)word [1]) - 97 < 0){//最后一封信
new_cell = new Node();

// new_cell-> page = page;
}
else
new_cell = new Cell;

cout<< typeid(new_cell).name()<< " - " ;;
new_cell-> display(); //单元格中的虚函数,节点中的cout pagenr
(...)
您是否在''Node''

类中拥有自己的显示功能实现。如果你能清楚地提到班级层次结构,那将是更好的

在这里更好地了解事情。

最好的问候,
mika


-

Karthik


------


人类请为我的电子邮件删除。




" Mika Vainio" < MI ************ @ vainio.de>在消息中写道

新闻:c6 ************* @ news.t-online.com ...

大家好,<我正在研究以下问题:我需要构建一个26-nary树到
保存数据字典。单词的每个字母都由一个单元格表示
(单元格有一个指针 - 矢量单元格* children [26])。最后一个字母是一个节点
(节点:单元格,附加属性pagenr)。
现在:我理解了多态对象的原理,并且它与ususal示例一起工作正常。但在我的情况下,它没有!没有
编译或链接错误,并且在运行时没有错误 - 至少没有显示
...这里是一些代码。也许有人可以给我一些提示...

// Trie.cpp
// ----------
(...)
void Trie :: insert(char * word,int page)
{
//第一个字母的int值
int iword =((int)word [0]) - 97;

insert_one(root-> children [iword],word,page);

void Trie :: insert_one(Cell * new_cell,char * word ,int page)
{
if(new_cell == NULL){
if(((int)word [1]) - 97< 0){// last letter
new_cell = new Node();

// new_cell-> page = page;
}
其他
new_cell = new Cell;

cout<< typeid(new_cell).name()<< " - " ;;
new_cell-> display(); //单元格中的虚函数,节点中的cout pagenr
(...)
最好的问候,
mika


很难从给出的片段说起,但我会说问题是

错误地使用指针而不是多态。

insert_one(root-> children [iword],word,页);
void Trie :: insert_one(Cell * new_cell,char * word,int page)
{




想想那段代码。我想你假设它会给root-> children [iword]写一个值

但是它不会。你需要这样的东西


root-> children [iword] = insert_one(word,page);


Cell * Trie :: insert_one(char * word,int page)

{

...

返回new_cell;

}


john


嗨karthik,

是的,我有自己的显示器( )实现(cout<<""                 这是我的头文件。

最好的问候,

mika


// Cell.h

// -----------------

class Cell {

public:

Cell(){}

virtual~Cell();

virtual void display()const;


char letter;

Cell * children [26];

};

// Node.h

// - --------------

class节点:public Cell {

public:

Node(){ }

int page;

void display();

Node * children [26];

};

// Trie.h

// --------------------------- -------

#include" Node.h"


class Trie {

public:

Trie(){

root = NULL;

root = new Cell;

for(int i = 0; i< 26; i ++)

root-> children [i] = NULL;

// root-> page = 1;

root-> letter =''\ 0'';

}

void display();
void insert(char *,int);

int search(char *);

void erase(char *);


私人:

Cell * root;

char * word;

void insert_one(Cell * new_cell,char * word,int page);

void display_one(Cell * top,char * prefix);

int search_one(Cell *& new_cell,char * word);

void erase_one(Cell *& new_cell,char * word);

};


" Karthik" <再******************* @ yahoo.com> schrieb im Newsbeitrag

新闻:408c2f40


hi everybody,
i''m working on the following problem: i need to build a 26-nary tree to save
a data dictionary. every letter of the words is represented by a cell (cell
has a pointer-vector cell* children[26]). the last letter is a node
(node:cell, additional property "pagenr").
now: i understood the principals of polymorph objects and it worked fine
with the ususal examples. but in my case it does not! there is no
compilation or linking error and no error at runtime - at least not shown...
here''s some code. maybe someone can give me some hints...
// Trie.cpp
//----------
(...)
void Trie::insert(char *word, int page)
{
// int value of first letter
int iword = ((int)word[0])-97;

insert_one(root->children[iword], word, page);
}

void Trie::insert_one(Cell* new_cell, char *word, int page)
{
if (new_cell == NULL) {
if (((int)word[1])-97 < 0) { // last letter
new_cell = new Node();

// new_cell->page = page;
}
else
new_cell = new Cell;

cout << typeid(new_cell).name() << " - ";
new_cell->display(); // virtual function in cell, cout pagenr in node
(...)

best regards,
mika

解决方案

Mika Vainio wrote:

hi everybody,
i''m working on the following problem: i need to build a 26-nary tree to save
a data dictionary. every letter of the words is represented by a cell (cell
has a pointer-vector cell* children[26]). the last letter is a node
(node:cell, additional property "pagenr").
now: i understood the principals of polymorph objects and it worked fine
with the ususal examples. but in my case it does not! there is no
compilation or linking error and no error at runtime - at least not shown...
here''s some code. maybe someone can give me some hints...
// Trie.cpp
//----------
(...)
void Trie::insert(char *word, int page)
{
// int value of first letter
int iword = ((int)word[0])-97; Try using macros instead of constants, as in 97 here.
insert_one(root->children[iword], word, page);
}

void Trie::insert_one(Cell* new_cell, char *word, int page)
{
if (new_cell == NULL) {
if (((int)word[1])-97 < 0) { // last letter
new_cell = new Node();

// new_cell->page = page;
}
else
new_cell = new Cell;

cout << typeid(new_cell).name() << " - ";
new_cell->display(); // virtual function in cell, cout pagenr in node
(...) Do you have your own implementation of display function in ''Node''
class. It would be nice if you can mention the class hierarchy clearly
out here to understand things better.

best regards,
mika


--
Karthik

------

Human Beings please ''removeme'' for my email.



"Mika Vainio" <mi************@vainio.de> wrote in message
news:c6*************@news.t-online.com...

hi everybody,
i''m working on the following problem: i need to build a 26-nary tree to save a data dictionary. every letter of the words is represented by a cell (cell has a pointer-vector cell* children[26]). the last letter is a node
(node:cell, additional property "pagenr").
now: i understood the principals of polymorph objects and it worked fine
with the ususal examples. but in my case it does not! there is no
compilation or linking error and no error at runtime - at least not shown... here''s some code. maybe someone can give me some hints...
// Trie.cpp
//----------
(...)
void Trie::insert(char *word, int page)
{
// int value of first letter
int iword = ((int)word[0])-97;

insert_one(root->children[iword], word, page);
}

void Trie::insert_one(Cell* new_cell, char *word, int page)
{
if (new_cell == NULL) {
if (((int)word[1])-97 < 0) { // last letter
new_cell = new Node();

// new_cell->page = page;
}
else
new_cell = new Cell;

cout << typeid(new_cell).name() << " - ";
new_cell->display(); // virtual function in cell, cout pagenr in node
(...)

best regards,
mika

Hard to say from the snippets given, but I would say that the problem is
incorrect use of pointers not polymorphism.
insert_one(root->children[iword], word, page); void Trie::insert_one(Cell* new_cell, char *word, int page)
{



Think about that code. I think you are assuming that it will write a value
to root->children[iword] but it doesn''t. You need something like this

root->children[iword] = insert_one(word, page);

Cell* Trie::insert_one(char *word, int page)
{
...
return new_cell;
}

john


hi karthik,
yes, i have my own display() implementation (cout << "Pagenr: " <<
new_cell->page << endl;). and here are all my header files.
best regards,
mika

// Cell.h
//-----------------
class Cell {
public:
Cell() {}
virtual ~Cell();
virtual void display() const;

char letter;
Cell* children[26];
};
// Node.h
//----------------
class Node : public Cell {
public:
Node() {}
int page;
void display();
Node* children[26];
};
// Trie.h
//----------------------------------
#include "Node.h"

class Trie {
public:
Trie() {
root = NULL;
root = new Cell;
for (int i = 0; i<26; i++)
root->children[i] = NULL;
// root->page = 1;
root->letter = ''\0'';
}
void display();
void insert(char*, int);
int search(char*);
void erase(char*);

private:
Cell *root;
char *word;
void insert_one(Cell* new_cell, char *word, int page);
void display_one(Cell *top, char* prefix);
int search_one(Cell *&new_cell, char *word);
void erase_one(Cell *&new_cell, char *word);
};

"Karthik" <re*******************@yahoo.com> schrieb im Newsbeitrag
news:408c2f40


这篇关于polymorph对象 - 为什么不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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