Вопрос

Disclaimer: I have looked at other questions that could be familiar to this situation however I have found no solution

Situation: I am attempting to create a linked list of the type Levelnode (a struct). upon creating the new node it should create a new level (level class).

Problem: I receive an error on line 18 (The .cpp file, not the header file) with -

Error   2   error C2512: 'Levelnode' : no appropriate default constructor available c:\users\timi\desktop\ludum dare\ld27\nodes.cpp 18  1   LD27

I do have a default constructor in the header file so I really don't see why this error is occuring.

here is the Nodes.h header file:

    #ifndef NODES_H
#define NODES_H

#include <iostream>
#include <string>
#include "structs.h"
#include "Level.h"


using namespace std;

class Nodes 
{
    private:

        Levelnode * p;
        Level * level;

    public:
        Nodes()
        {

        }

        ~Nodes();
        Levelnode * CreateNode(Level * newLevel, SDL_Surface *screen1, string levelName, SDL_Rect camera1);
        void InsertAfter(Levelnode * p, Levelnode * newNode);
        Levelnode * InsertFirst(Levelnode * p, Levelnode * newNode);
        void DeleteNode(Levelnode * p);
        void PrintList(Levelnode * p);
};
#endif

here is the .cpp file:

#include <Windows.h>
#include <iostream>
#include <string>
#include "Nodes.h"
#include "structs.h"
#include "Level.h"


    Nodes::~Nodes()
    {


    }

    Levelnode * Nodes::CreateNode(Level * newLevel, SDL_Surface *screen1, string levelName, SDL_Rect camera1)
    {
        Levelnode * newNode;
        newNode = new Levelnode;
        level = new Level(screen1, levelName, camera1);
        level->draw();
        newNode->L = *newLevel;
        newNode->next = NULL;
        return newNode;
    }

    void Nodes::InsertAfter(Levelnode * p, Levelnode * newNode)
    {
        newNode->next = p->next;
        p->next = newNode;
    }

    Levelnode * Nodes::InsertFirst(Levelnode * p, Levelnode * newNode)
    {
        newNode->next = p;
        return newNode;
    }

    void Nodes::DeleteNode(Levelnode * p)
    {
        Levelnode * pTemp;
        while ( p != NULL)
        {
            pTemp = p;
            p = p->next;
            delete pTemp;

        }
    }

    void Nodes::PrintList(Levelnode * p)
    {
        while (p != NULL)
        {
            p->L.draw();
            p = p->next;
        }

    }

struct header file:

#ifndef STRUCTS_H
#define STRUCTS_H
#include <Windows.h>
#include <string>
#include "Level.h"

using namespace std;

struct position
{
    int x;
    int y;
};


struct Levelnode
{
    Level L;
    Levelnode * next;
};

#endif
Это было полезно?

Решение

The most likely cause is that since your Levelnode struct:

struct Levelnode
{
    Level L;
    Levelnode * next;
};

has a Level member, and Level lacks a default ctor, the default, compiler-generated ctor for Levelnode cannot initialize the Levelnode::L member. The result is that the compiler does not actually generate a default Levelnode ctor at all. If you try to create a Levelnode:

Levelnode node;

you will get an error about Levelnode not having a default ctor.

You need to provide your own default ctor for Levelnode which should initialize Levelnode::L using an appropriate L ctor. Like:

struct Levelnode
{
    Level L;
    Levelnode * next;

    Levelnode()
        : L(ctor arguments)
    { }
};

Another solution would be to provide a default ctor for Level, if that makes sense for that class.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top