0

How can I access elements from my Node pointer array? The cout at the bottom returns an address "0xb7738ff4". Even if I make a constructor and set each element to NULL, I cannot modify any of them later on.

#include <iostream>
using namespace std;

class Node
{
    public:
    char character;
};
class Tree
{
    public:
    Node* nodes[26];
};
int main() {
 Tree t;
 //t.nodes[0]->character = 'a';
 cout << "\"" << (t.nodes[0]) << "\"";
}

http://ideone.com/XvLme9

3
  • nodes is a pointer to an array... Commented Dec 12, 2014 at 4:31
  • nodes is an array of pointers. Using t-> is wrong because t is not a pointer, using t. is correct. Commented Dec 12, 2014 at 4:31
  • and where is the constructor?? and what you want it to print? Commented Dec 12, 2014 at 4:35

1 Answer 1

2

t.nodes[0] is returning a Node* pointer. You are passing that pointer as-is to cout, that is why it is printing a memory address (and a random one at that, because you are not initializing the array). If you want to print the character of a node, you have to dereference the Node* pointer, exactly like your commented out code is doing (which is the correct way to do it):

t.nodes[0]->character

You just have to make sure that nodes[0] returns a valid pointer to a real Node object to begin with:

Tree t;
t.nodes[0] = new Node; // <-- here
t.nodes[0]->character = 'a';
std::cout << "\"" << t.nodes[0]->character << "\"" << std::endl;

Don't forget to delete the node when you are done using it. Tree should have a destructor that frees the nodes it owns.

Try something more like this:

#include <iostream>
#include <stdexcept>

class Node
{
public:
    char character;

    Node(char c = 0);
};

class Tree
{
private:
    Node* nodes[26];
    int count;
public:
    Tree();
    ~Tree();
    Node* add(char c);
    Node* get(int idx);
};

Node::Node(char c)
    : character(c)
{
}

Tree::Tree()
    : count(0)
{
    for (int i = 0; i < 26; ++i)
        nodes[i] = NULL;
}

Tree::~Tree()
{
    for (int i = 0; i < count; ++i)
        delete nodes[i];
}

Node* Tree::add(char c)
{
    if (count == 26)
        throw std::runtime_error("nodes array is at its max capacity");

    Node *node = new Node(c);
    nodes[count++] = node;
    return node;
}

Node* Tree::get(int idx)
{
    if ((idx < 0) || (idx >= count))
        throw std::out_of_range("invalid index");
    return nodes[idx];
}

int main()
{
    Tree t;
    t.add('a');
    std::cout << "\"" << t.get(0)->character << "\"" << std::endl;
}

With that said, you should use std::list or std::forward_list instead of writing your own tree class:

#include <list>

int main()
{
    std::list<char> l;
    l.push_back('a');
    std::cout << "\"" << l.front() << "\"" << std::endl;
}

Or:

#include <list>

class Node
{
public:
    char character;
    // other things here...

    Node(char c = 0);
    Node(const Node &src);
    Node& operator=(const Node &rhs);
};

Node::Node(char c)
    : character(c)
{ 
}

Node::Node(const Node &src)
    : character(src.character)
{
}

Node& Node::operator=(const Node &rhs)
{
    character = src.character;
    return *this;
}

int main()
{
    std::list<Node> l;
    l.push_back('a');
    std::cout << "\"" << l.front().character << "\"" << std::endl;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for helping me, I made those changes but it segfaults: codepad.org/xsH60WVD why is this?
You did not make the changes I told you to make. Look more carefully at my example. I told you to new a Node object and store it in the array, but you new'ed a Tree object instead (and are not delete'ing it), and are still not initializing the array at all. You are segfaulting when you try to dereference an uninitialized Node* pointer.
Thanks for helping me, this really helps. I finally got it to work. I won't implement the whole thing yet with private variables and proper destuctors, but thanks again codepad.org/n9jIC3XS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.