ВОПРОС · Q&A

Template Single Linked Class

2 ответов 3,1 тыс.
AI-выжимка обсуждения скоро

Краткие тезисы обсуждения со ссылками на ключевые ответы появятся здесь.

Автор вопроса
Выдаёт ошибку, не компилируется (для упрощения сначала сделал без класса итератора).
Компилятор Visual Studio 2013.

Вот сам код:
Для header.h

//Single linked Linear List
#ifndef STL_List_H
#define STL_List_H

#include <iostream>
#include <string>

using std::cout;
using std::endl;

template<class Object>
class List;

template<class Object>
struct Node
{
public:
//actual data of Node
Object data;
//pointer to the next node
Node *next;
public:
//default constructor of class Node
Node()
{
data = NULL;
next = NULL;
}
/*
Node(Object input_data, Node *pointer1)
{
data = input_data;
next = pointer1;
}*/

//Accessors
Node *getPointer() const { return next; }
int getData() const { return data; }

//Mutators
void setPointer(Node input) { next = input; }
void setData(int data1) { data = data1; }
};

template<class Object>
class List
{
private:
Node<Object> *beginning;
public:
//typedef iterator<Object> itr;
List()
{
beginning->data = NULL;
beginning->next = NULL;
}

void AddToEnd(Object input)
{
if ((beginning->next == NULL) && (beginning->data == NULL))
beginning->data = input;
else
{
Node<Object> *temp;
temp = beginning->next;
while (temp->next != NULL)
temp = temp->next;
Node<Object> *new_one = new Node<Object>();
temp->next = new_one;
new_one->data = input;
new_one->next = NULL;
}
}
/*
void RemoveFromEnd()
{
Node *temp;
temp = beginning->next;
while (temp->next != NULL)
temp = temp->next;
delete temp;
return 0;
}

Node<Object> searchList(Object input)
{
Node *temp;
temp = beginning->next;
while (temp->next != NULL)
temp = temp->next;
return temp;
}*/

void display()
{
Node<Object> *temp;
temp = beginning->next;
while (temp->next != NULL)
{
temp = temp->next;
cout << temp->data;
}
}
};
#endif


И для test.cpp

#include "STL_List.h"
#include <string>
#include <iostream>
#include "STL_List.h"

using std::cout;
using std::endl;

int main()
{
List<int> l1;

cout << "ddd";

l1.AddToEnd(10);
}

Заранее спасибо
 

Вложения

  • NewOne.png
    NewOne.png
    13,2 КБ · Просмотры: 854
Поставь breakpoint, запусти отладку, узнай на какой строчке программа падает.