473,508 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

polymorph object - why doesn't it work???

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
Jul 22 '05 #1
9 1445
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.
Jul 22 '05 #2

"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

Jul 22 '05 #3
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$1@darkstar...
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.

Jul 22 '05 #4
Mika Vainio wrote:
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];


Whatz this. Why do you need to have an array of 'Cell' pointers ( as
in the base class) and again 26 pointers again here. I guess thatz
against polymorphism as such.

Also the classes storing the data and modelling ought to be separate
( decoupled , to get the right term from the literature , that is).

That means, There might be a hierarchy of data classes, exhitinhg
polymorphism, but there ought to be a single class (that would have 26
pointers) that would be used an user to create the tree etc.

HTH

Karthik

------

Human Beings please 'removeme' for my email.
Jul 22 '05 #5
hi john,
i just tried your idea but the last letter is still a cell. this is what i
wrote:

void Trie::insert(char *word, int page)
{
int iword = ((int)word[0])-97;

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

Cell* Trie::insert_one(char *word, int page)
{
Cell* new_cell = 0;

if (((int)word[1])-97 < 0) { // Node anlegen
new_cell = new Node();
cout << typeid(new_cell).name() << endl;
new_cell->display();
}
else
new_cell = new Cell;

for (int i = 0; i<26; i++)
new_cell->children[i] = NULL;

new_cell->letter = word[0];

int iword = ((int)word[1])-97;
if (iword >= 0) {
new_cell->children[iword] = insert_one(word+1, page);
}

return new_cell;
}

output: class Cell *

best regards,
mika
Jul 22 '05 #6
ops, you're right - this comes from an earlier version where i used node for
all letters. i killed this line but that didn't change anything - the last
letter is still a cell...
and did i understand you correctly: the pointer vector should be changed
into 26 single cell pointers in a separate class?
best regards,
mika
"Karthik" <re*******************@yahoo.com> schrieb im Newsbeitrag
news:408c473b$1@darkstar...
Mika Vainio wrote:
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];


Whatz this. Why do you need to have an array of 'Cell' pointers ( as
in the base class) and again 26 pointers again here. I guess thatz
against polymorphism as such.

Also the classes storing the data and modelling ought to be separate
( decoupled , to get the right term from the literature , that is).

That means, There might be a hierarchy of data classes, exhitinhg
polymorphism, but there ought to be a single class (that would have 26
pointers) that would be used an user to create the tree etc.

HTH

Karthik

------

Human Beings please 'removeme' for my email.

Jul 22 '05 #7

"Mika Vainio" <mi************@vainio.de> wrote in message
news:c6*************@news.t-online.com...
hi john,
i just tried your idea but the last letter is still a cell. this is what i
wrote:


I think I'm going to have to see a complete compilable program to help with
this one.

john
Jul 22 '05 #8
Mika Vainio wrote:
Cell* Trie::insert_one(char *word, int page)
{
Cell* new_cell = 0;

if (((int)word[1])-97 < 0) { // Node anlegen
new_cell = new Node();
cout << typeid(new_cell).name() << endl;
Here use "cout << typeid(*new_cell).name() << endl; instead of
typeid(new_cell). Whithout dereferencing the pointer, you get the type
of the *pointer*, which is correct (Cell*).
new_cell->display();


which display() method is called? the one in Cell, or the one in Node. I
prefer printing a text in the derived method than using typeid...

hth,
Iuli
Jul 22 '05 #9
<original message snipped to pieces>

// Cell.h
//-----------------
class Cell {
public:
Cell() {}
virtual ~Cell();
virtual void display() const;
Look at this declaration of display,
char letter;
Cell* children[26];
};
// Node.h
//----------------
class Node : public Cell {
public:
Node() {}
int page;
void display();
and compare with this one, please.
Node* children[26];
};


You will see that the virtual function in the base class is a const one,
but the function declared in the derived class is not. Since this is C++,
not Java, the declaration in the derived class doesn't override that in the
base class, but rather hides it.

To take part in dynamic dispatch, the function in the derived class must have
exactly the same signature as the base class function. (Apart from the return
type, wich can be of a derived type if your compiler indeed supports "covariant
returns".)

HTH,

Uwe
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2751
by: rgrandidier | last post by:
I am looking for an example using a summary element to have both a see and seealso tag within the summary to get them to show up as links in the Object Browser. The following is what I am using...
30
3690
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
2
1350
by: Frank Jeseit | last post by:
Hi, I have two methods, with the same name but different params: MyMethod(string) MyMethod(string, string, string) I can build the service but the call in IE or a client app fails, because...
14
2551
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
4
1615
by: marcus hall | last post by:
I am considering a strategy for implementation of a finite state machine. What I would like to do is to use derived classes to represent the state of the machine, so the vtable pointer is the state...
2
1479
by: Fred | last post by:
I've got the following code: #include <iostream> class Base{ private: virtual void f(int) { std::cout << "f in Base" << std::endl; } };
1
7078
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
1
4443
by: MindWrapper | last post by:
boost serialization of polymorph classes from DLLs Folks, Let's consider following code -------------------------------- // base.h // abstract base class class IBase {
275
12037
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5049
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.