Search code examples
c++vectorcompiler-errorsstdvector

C++ Vector datatype compiler error issue with a member of my defined Class


I'm trying to learn C++ as an interest, and I'm coming up with an issue trying to incorporate a Vector container.

I'm trying to incorporate a vector iterator into my Class, but I'm getting a compile error on this line std::cout << nodeIterator->data;, my code snippets below.

Error I'm getting:

error: request for member 'data' in '* nodeIterator. __gnu_cxx::__normal_iterator::operator-> [with _Iterator = const FibTree::Node**, _Container = std::vector >]()', which is of non-class type 'const FibTree::Node*'
class Node {
    public:
        int data;
        Node const* left;
        Node const* right;
        Node const* parent;
        int n;
        int level;
        int index;
    
        Node (void);
    
};

// Get root method
Node const* getRoot(void) {
    return this->root;
}

void start(Node const* root) {
    std::vector<Node const*> setsList;
    std::cout << root->data; 
writeSets(setsList, root);
}

writeSets(std::vector<Node const*> &setsList, Node const* cur) {

    std::vector<Node const*>::iterator nodeIterator;

    // Displays all preceding left values
    for (nodeIterator = setsList.begin();nodeIterator != setsList.end(); nodeIterator++)
    {
        std::cout << nodeIterator->data; //*** Get Compile error this line ***
    }

    std::cout << cur->left->data;
    std::cout << cur->right->data;

    setsList.push_back(cur->left);
    writeSets(setsList,cur->right);
    setsList.pop_back();
}

Could anyone give me any suggestions?


Solution

  • Here's the culprit:

    Iterator = const FibTree::Node**
    

    so when you write nodeIterator->data you're requesting data from a const FibTree::Node** which has no such member hence the error. you need

    (*nodeIterator)->data
    

    edit Apart from that your code as shown doesn't compile: writeSets has no return type, getRoot tries to access this which cannot be done in non-member functions and start is declared before writeSets.