简体   繁体   中英

call to non-static member function without an object argument, C++ nodes

This appears to be covered in depth on this website but after searching for a while I can't seem to find what my problem specifically is, as I'm getting the error with a void function.

I'm attempting to code a Huffman Tree and I'm trying to build a min heap. In order to do that I need to be able to swap nodes in the tree. I keep receiving the above error on the line where I call swapNode for the first time.

Node.cpp offending code

void Node::swapNode(Node &a, Node &b) {
    Node t = a;
    a = b;
    b = t;
}
Node**minHeapify(Node **array, int index) { 
    int root = index;
    int left = 2 * index + 1;
    int right = 2 * index + 2;
    if (array[left]->frequency < array[root]->frequency) {
        root = left;
    }
    if (array[right]->frequency < array[root]->frequency) {
        root = right;
    } 
    if(root != index) {
        Node::swapNode(&array[root], &array[index]);
        minHeapify(array, root);
    }
}

Node.h

#ifndef _listnode_h
#define _listnode_h

#include <string>

class Node {
    public: 
        Node(char character, int frequency, int ascii);
        Node(int frequency);
        Node();
        ~Node();
        int getFrequency();
        char getCharacter();
        std::string getCode();
        Node *getLeft();
        Node *getRight();
        Node **minHeapify(Node *, int);
        void swapNode(Node &a, Node &b);    
        void setCode(std::string s);

//  private:
        int frequency;
        char character; 
        int ascii;
        std::string code;
        int leaf = 0;
        Node* left, *right; 
};

#endif 

The array I'm passing in is an array of tree nodes that I want to sort into a min heap. Any and all help is appreciated, thanks.

If you want to call swapNode without invoking it directly on an actual object (which is reasonable) and you want it to be a class member, then you need to declare the function static. Or you can just use std::swap

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM