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++: multiple definition...?

Hallo, ich habe in C++ einen Header erstellt, aber bei der Complierung kommt immer diese Fehlermeldung:

C:\Users\Admin\AppData\Local\Temp\ccdaLGOT.o:lexer.cpp:(.text+0x111): undefined reference to `Token::content[abi:cxx11]() const'
...(Zu lang zum abschicken)
collect2.exe: error: ld returned 1 exit status

Kann mir jemand sagen was der Fehler ist und wie ich ihn behebe?

token.hpp:

#ifndef TOKEN_HPP_
#define TOKEN_HPP_

#include <string>

class Token {
public:
    enum class Type {
        STRING,
        NUMBER,
        BOOLEAN,
        IDENTIFIER,
        EQUAL,
        PLUS,
        MINUS,
        TIMES,
        DIVIDE,
        LESS_THAN,
        GREATER_THAN,
        COMMENT,
        DOT,
        COMMA,
        SEMICOLON,
        CURLY_BRACKET_RIGHT,
        CURLY_BRACKET_LEFT,
        APOSTROPHE,
        COLON,
        UNEXPECTED
    };

    Token                (Type type)              noexcept;

    Type type            ()                       const noexcept;

    bool is                (Type type)               const noexcept;

    bool is_not            (Type type)               const noexcept;

    std::string content ()                       const noexcept;

    void change_content (std::string content)  const noexcept;

    void add_to_content (std::string content)  const noexcept;

    void clear_content  ()                       const noexcept;

    void change_type    (Type type)            const noexcept;

private:
    Type m_type;
    std::string m_content;
};

#endif

token.cpp:

#include "token.hpp"

#include <string>

Token::Token(Type type) noexcept : m_type{type} {};

Token::Type Token::type() const noexcept {
    return m_type;
}

bool Token::is(Type type) const noexcept {
    return m_type == type;
}

bool Token::is_not(Type type) const noexcept {
    return m_type != type;
}

std::string Token::content() const noexcept {
    return m_content;
}

void Token::change_content(std::string content) const noexcept {
    m_content = content;
}

void Token::add_to_content(std::string content) const noexcept {
    m_content = m_content + content;
}

void Token::clear_content() const noexcept {
    m_content.clear(); //Die clear Funktion geht auch nicht
}

void Token::change_type(Type type) const noexcept {
    m_type = type;
}

Ich hoffe jemand kann mir dabei helfen!

LG

PC, Computer, Software, programmieren, compiler, Cplusplus, CPP, development, Informatik, Informatiker, Programmiersprache, C (Programmiersprache), Cpp Programierung
C++: Fehler beim schreiben einer .tiff Datei mit Libtiff?

Hi,

das ist hoffentlich meine letzte Frage zu dem Thema :).

Ich habe ein Array mit allen RGBA-Werten für ein Bild im Byte Format (z.B.: 4282989356).
Nun möchte mit diesem Array eine neue Bilddatei vom Format .tiff schreiben und verwende dafür die Bibliothek libtiff.

Ich verwende folgenden Code:

auto image = new uint32[20976 * 20976];
for (uint32 row = 0; row < height; row++)
	{
		TIFFWriteScanline(tif, image, row, 0);
	}

Ich habe hier noch die Dokumentation für TIFFWriteScanline().

Das Programm schreibt eine neue .tif Datei, die sogar eine Dateigröße hat die ich erwarte. Allerdings sieht die Datei geöffnet leider so aus.

Ich bin mir nicht sicher ob ich mein Array einfach so übergeben kann. TIFFWriteScanline() erwartet eine Variable vom Datentyp tdata_t, was auch immer das sein soll.

Es gibt ein Tutorial, welches den Vorgang etwas anders implementiert. Im Tutorial werden ein image und buf Array deklariert, beide vom Typ char.

char *image=new char [width*height*sampleperpixel];
unsigned char *buf = NULL; 

Es wird die benötigte Speicher für eine Zeile festgelegt und für buf initialisiert.

tsize_t linebytes = sampleperpixel * width; 
buf =(unsigned char *)_TIFFmalloc(linebytes);

Schließlich wird wie bei mir über jede Zeile iteriert und dabei zunächst die Bilddaten in buf geschrieben und von buf in die .tif Datei.

for (uint32 row = 0; row < h; row++)
{
memcpy(buf, &image[(h-row-1)*linebytes], linebytes);   
TIFFWriteScanline(out, buf, row, 0)
}

Wobei ehrlich gesagt im Tutorial image nur deklariert aber nicht initialisiert wird. Wahrscheinlich muss es noch initialisiert werden. Aber kann ich meine RGBA Daten in Byte codes in ein Char array schreiben? Und wieso werden die Daten überhaupt zuerst in den buf geschrieben? Ist das wichtig und ggf. die Erklärung wieso mein Bild so bescheiden aussieht?
Ich bin gefühlt so kurz vorm Ziel was dieses kleine Projekt angeht und würde mich über Hilfen und Tipps sehr freuen :)

Bild zu Frage
programmieren, CPP, TIFF, Cpp Programierung
C++: Ursache für Lesezugriffsverletzung?

Hallo,

ich komme an einer Stelle nicht weiter und könnte noch einmal Hilfe gebrauchen. Ich nutze die Bibliothek Libtiff um die Farbwerte von einem Bild in einen Vector zu speichern bekomme aber eine Lesezugriffsverletzung. Anbei der Code:

void GetImage(TIFF* tif)
{
	uint32 npixels = width * height * sizeof(uint32);
	uint32* raster;
	std::vector <uint32> image;
	raster = (uint32*)_TIFFmalloc(npixels);
	if (TIFFReadRGBAImageOriented(tif, width, height, raster, ORIENTATION_TOPLEFT, 0) == 1)
	{
		std::cout << npixels << std::endl;
		for (uint32 i = 0; i < npixels; i++)
		{
			image.push_back(raster[i]);
		}
		std::cout << image[0] << std::endl;
	};
}

TIFFReadRGBAImageOriented

speichert die Pixelwerte von einem Bild in die Variable "raster" mit der Größe von "npixels". Ich möchte diese Werte später verwenden und sie deshalb global abspeichern. Deshalb habe ich den Vector

std::vector <uint32> image;

deklariert und möchte mit der for-Schleife die Werte von raster in image kopieren.

Ich bekomme leider folgende Fehlermeldung:

Ausgelöste Ausnahme: Lesezugriffsverletzung

std::forward<unsigned int const & __ptr64>(...) hat 0x22A6E503000 zurückgegeben.

npixels = 482241600. Wenn ich bei der for-Schleife anstelle von npixels 48224160 schreibe (also eine 0 weglasse), bekomme ich keine Fehlermeldung mehr. "raster" scheint also mehr Werte zu haben, als man in den Vector "image" hineinschreiben kann. Zumindest interpretiere ich das Problem so.

Ist meine Interpretation richtig und wenn ja, was kann ich tun um die Werte von raster irgendwie global abzuspeichern.

Viele Grüße

CPP, 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 Programierung