Verkettete Liste mit interner Klasse ...****?

Termos

Mitglied
Guten Morgen Community,

Die Methode void enqueue(prod p) -> soll eine neue Instanz "Elem" mit p als letztes Element an die Liste hängen.

Ich versteh es einfach gerade nicht, ist die Verkettung an sich richtig?
In welcher Form benötige ich den Standard Konstruktor von "Elem" um
Code:
Elem *first = new Elem;
ohne Fehler zum laufen zu bekommen.

Code:
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <iomanip>
#include <string>
#define N 3

 using namespace std ;

class prod
{
	private:
		string pn;
		double preis;
		unsigned long anz ;
	public:
		prod(string,double,unsigned long);
		~prod();
		void ein();
		void out();
		string getPN() {return pn;}
		double getPreis() {return preis;}
		unsigned long getAnz() {return anz;}
		void setPN(string pn) {pn = pn ;}
		void setPreis(double preis) {preis = preis;}
		void setAnz (unsigned long anz) {anz = anz;}
};

prod::prod(string pn="",double preis=0.0, unsigned long anz=0ul):pn(pn),preis(preis),anz(anz)
{
	cout<<"Produktname: "<<pn<<endl;
	cout<<"Preis: "<<preis<<endl;
	cout<<"Anzahl: "<<anz<<endl;
}

void prod::out() 
 {
	cout<<"Produktname: "<<pn<<endl;
	cout<<"Preis: "<<preis<<endl;
	cout<<"Anzahl: "<<anz<<endl;
}


prod::~prod()
 {
	cout<<"Produktname: "<<pn<<endl;
	cout<<"Preis: "<<preis<< endl;
 	cout<<"Anzahl: "<<anz<<endl;
 }

 void prod::ein()
 {
	cout<<"Eingabe Produktname: ";
	cin>>pn;
	cin.clear();cin.ignore(INT_MAX,'\n');
	cout<<"Eingabe Preis: ";
	cin>>preis;
	cin.clear();cin.ignore(INT_MAX,'\n');
	cout<<"Eingabe Anzahl : ";
	cin>>anz;
	cin.clear();cin.ignore(INT_MAX,'\n');
 }


 class Queue
 {

 public:
	
	 
	//####    Innere klasse Elem ####
	 class Elem
	 {
	 public:
		 prod p;
		 Elem *next;
		 Elem(prod p(prod), Elem *next=0){}
		 



	 };
	

	 Queue(Elem *first=0,Elem *last=0){} //Konstruktor


	 void enqueue(prod p)
		 {
			Elem *first = new Elem;
			first->p = p.ein();
			first->next=last;
			last=first;
		 }


 private:
	 Elem *first;
	 Elem *last;
 };


int main()
 {
	 system("PAUSE");
	 return 0;
 }
 
Ich weiß jetzt nicht genau, ob ich die Frage richtig verstanden habe, aber du hast ja nur einen Konstruktor geschrieben, der 2 Argumente bekommt. Beim new übergibst du aber keine Argumente, das heißt der Compiler sucht nach einem Konstruktor, der 0 Argumente übergeben bekommt. Entweder übergibst du beim new-Aufruf Argumente oder du schreibst einen weiteren Konstruktor.

Zur Lektüre, wie wäre es mit "Thinking in C++" von Bruce Eckel? Ich lerne selbst damit und es ist wirklich ein gutes Buch.

Lg
 
Zurück