C++: invalid use of non-static data member ...?

Hallo, ich habe in C++ einen Header für ein Projekt geschrieben aber ich kann innerhalb einer Klasse nicht auf eine Variable zugreifen.

Es kommt immer der Fehler:

..\ast.cpp:61:19: error: invalid use of non-static data member 'Tree::nodes'
 p.set_type(Tree::nodes[id].get_type());

Hier ist der Code:

ast.h:

#ifndef AST_H_
#define AST_H_

class Tree {
public:
    class Node {
    public:
        enum class Type {
            METHOD,
            IF,
            WHILE,
            STATEMENT,
            EXPRESSION
        };

        void set_id(int id);
        void set_id_parent(int id_parent);
        void set_content(const char* content);
        void set_type(Type type);

        Type get_type();
        const char* get_content();
        int get_id();
        int get_parent_id();
        int* get_child_ids();

    private:
        Type m_type{};
        const char* m_content{};
        int  m_id=0;
        int  m_id_parent=-1;
        int* m_child_ids{};
    };

    void create_node(int parent, const char* content);

    Node get_node(int id);

private:
    Node* nodes{};
    int node_amount=0;
};

#endif

ast.cpp:

#include "ast.h"

void Tree::Node::set_id(int id) {
    m_id = id;
}

void Tree::Node::set_id_parent(int id_parent) {
    m_id_parent = id_parent;
}

void Tree::Node::set_content(const char *content) {
    m_content = content;
}

void Tree::Node::set_type(Type type) {
    m_type = type;
}

const char* Tree::Node::get_content() {
    return m_content;
}

Tree::Node::Type Tree::Node::get_type() {
    return m_type;
}

int Tree::Node::get_id() {
    return m_id;
}

int Tree::Node::get_parent_id() {
    return m_id_parent;
}

int* Tree::Node::get_child_ids() {
    return m_child_ids;
}

//

void Tree::create_node(int parent, const char *content) {
    Node p;
    p.set_id(node_amount);
    p.set_id_parent(parent);
    p.set_content(content);
    nodes[node_amount] = p;
    node_amount++;
}

Tree::Node get_node(int id) {
    Tree::Node p;
    p.set_type(Tree::nodes[id].get_type());
    return p;
}

Ich hoffe jemand kann mir sagen warum dieser Fehler kommt

Außerdem wollte ich etwas C benutzen und lernen, also hätte ich noch eine zweite Frage: Kann mir jemand sagen wie ich das ganze nicht in C++ sondern in C mache?

LG

PC, Computer, Software, programmieren, Cplusplus, CPP, development, Informatik, Softwareentwicklung, C (Programmiersprache), cpp lernen, Cpp Programierung
C++ funktion() should have been declared inside namespace?

Hallo, ich arbeite gerade an einer Header Datei in C++. Aber ich kann nun nicht mehr weitermachen weil ständig ein Fehler auftaucht:

meinHeader.h:21:38: error: 'void meinNamespace::draw(int, int)' should have been declared inside 'meinNamespace'

Ich kann im Internet nichts hilfreiches finden und weiß nicht warum der Fehler auftaucht. Hier die betroffenen Codeabschnitte:

#include <Windows.h>
#include <WinUser.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>

#include "meinHeader.h"

#define PI 3.14

enum meinNamespace::color {
    DARKBLUE = 1, 
    DARKGREEN, 
    DARKTEAL, 
    DARKRED,
    DARKPINK,
    DARKYELLOW,
    GRAY, 
    DARKGRAY, 
    BLUE, GREEN, 
    TEAL, 
    RED, 
    PINK, 
    YELLOW, 
    WHITE
};

struct meinNamespace::setColor {
    color _c;
    HANDLE _console_handle;
    setcolor(color c, HANDLE console_handle)
        : _c(c), _console_handle(0)
    { 
        _console_handle = console_handle;
    }
};


void meinNamespace::draw(int x, int y) {
    HWND myconsole = GetConsoleWindow();
    HDC mydc = GetDC(myconsole);

    int pixel =0;

    COLORREF COLOR= RGB(255,255,255); 


    for(double i = 0; i < PI * 4; i += 0.05) {
        SetPixel(mydc,pixel,(int)(50+25*cos(i)),COLOR);
        pixel+=1;
    }
    ReleaseDC(myconsole, mydc);
}

void meinNamespace::drawSquare(int aa, int ab, int ba, int bb) {
    draw(aa,bb);
}

Header:

#ifndef MEINHEADER_H
#define MEINHEADER_H

namespace meinNamespace {
    enum color {
        DARKBLUE=1,DARKGREEN,
        DARKTEAL,DARKRED,
        DARKPINK,DARKYELLOW,
        GRAY, DARKGRAY,
        BLUE,GREEN,
        TEAL,RED,
        PINK,YELLOW,
        WHITE
    };
    struct setcolor;
    void meinNamespace::draw(int x, int y);
    void meinNamespace::draw(int a, int b);
}

#endif

Die Funktionen sollen in ein Konsolenfenster Pixel schreiben, ob sie funktionieren weiß ich noch nicht und sie sind auch noch nicht fertig. Ich weiß nicht wo der Fehler verursacht wird. Ich hoffe jemand kann mir da helfen

LG

Software, IT, programmieren, CPP, development, Informatik, cpp lernen, Cpp Programierung

Meistgelesene Fragen zum Thema Cpp lernen