I am very new to Microsoft visual studio C++ as well as std::unique_ptr. On CodeReview I was recommended to rewrite using std::unique_ptr. You can find the question I am referencing here.
Here are the following errors I am receiving:
1>main.cpp
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(26): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(61): note: see reference to class template instantiation 'SingleLinkedList<T>' being compiled
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(65): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(104): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(104): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(120): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(120): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(136): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(136): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(145): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(145): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(152): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(152): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(164): error C2760: syntax error: unexpected token 'identifier', expected ';'
1>c:\dev\linkedlist\linkedlist\singlelinkedlist.h(164): error C7510: 'make_unique': use of dependent type name must be prefixed with 'typename'
1>Done building project "LinkedList.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Here is the header file:
#ifndef SingleLinkedList_h
#define SingleLinkedList_h
#include <iostream>
template <class T>
class SingleLinkedList {
private:
struct Node {
T data;
std::unique_ptr<Node> next = nullptr;
Node(T x) : data(x), next(nullptr) {}
};
std::unique_ptr<Node> head = nullptr;
std::unique_ptr<Node> tail = nullptr;
// This function is for the overloaded operator <<
void display(std::ostream &str) const {
for (std::make_unique<Node> loop = head; loop != nullptr; loop = loop->next) {
str << loop->data << "\t";
}
str << "\n";
}
public:
// Constructors
SingleLinkedList() = default; // empty constructor
SingleLinkedList(SingleLinkedList const &source); // copy constructor
// Rule of 5
SingleLinkedList(SingleLinkedList &&move) noexcept; // move constructor
SingleLinkedList& operator=(SingleLinkedList &&move) noexcept; // move assignment operator
~SingleLinkedList();
// Overload operators
SingleLinkedList& operator=(SingleLinkedList const &rhs);
friend std::ostream& operator<<(std::ostream &str, SingleLinkedList &data) {
data.display(str);
return str;
}
// Memeber functions
void swap(SingleLinkedList &other) noexcept;
void push(const T &theData);
void push(T &&theData);
void display() const;
void insertHead(const T &theData);
void insertTail(const T &theData);
void insertPosition(int pos, const T &theData);
void deleteHead();
void deleteTail();
void deletePosition(int pos);
bool search(const T &x);
};
template <class T>
SingleLinkedList<T>::SingleLinkedList(SingleLinkedList<T> const &source) {
for(std::make_unique<Node> loop = source->head; loop != nullptr; loop = loop->next) {
push(loop->data);
}
}
template <class T>
SingleLinkedList<T>::SingleLinkedList(SingleLinkedList<T>&& move) noexcept {
move.swap(*this);
}
template <class T>
SingleLinkedList<T>& SingleLinkedList<T>::operator=(SingleLinkedList<T> &&move) noexcept {
move.swap(*this);
return *this;
}
template <class T>
SingleLinkedList<T>::~SingleLinkedList() {
while (head != nullptr) {
deleteHead();
}
}
template <class T>
SingleLinkedList<T>& SingleLinkedList<T>::operator=(SingleLinkedList const &rhs) {
SingleLinkedList copy{ rhs };
swap(copy);
return *this;
}
template <class T>
void SingleLinkedList<T>::swap(SingleLinkedList &other) noexcept {
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
}
template <class T>
void SingleLinkedList<T>::push(const T &theData) {
std::make_unique<Node> newNode = Node(theData);
if (head == nullptr) {
head = newNode;
tail = newNode;
newNode = nullptr;
}
else {
tail->next = newNode;
tail = newNode;
}
}
template <class T>
void SingleLinkedList<T>::push(T &&theData) {
std::make_unique<Node> newNode = Node(std::move(theData));
if (head == nullptr) {
head = newNode;
tail = newNode;
newNode = nullptr;
}
else {
tail->next = newNode;
tail = newNode;
}
}
template <class T>
void SingleLinkedList<T>::display() const {
std::make_unique<Node> newNode = head;
while (newNode != nullptr) {
std::cout << newNode->data << "\t";
newNode = newNode->next;
}
}
template <class T>
void SingleLinkedList<T>::insertHead(const T &theData) {
std::make_unique<Node> newNode = Node(theData);
newNode->next = head;
head = newNode;
}
template <class T>
void SingleLinkedList<T>::insertTail(const T &theData) {
std::make_unique<Node> newNode = Node(theData);
tail->next = newNode;
tail = newNode;
}
template <class T>
void SingleLinkedList<T>::insertPosition(int pos, const T &theData) {
}
template <class T>
void SingleLinkedList<T>::deleteHead() {
std::make_unique<Node> old = head;
head = head->next;
delete old;
}
template <class T>
void SingleLinkedList<T>::deleteTail() {
}
template <class T>
void SingleLinkedList<T>::deletePosition(int pos) {
}
template <class T>
bool SingleLinkedList<T>::search(const T &x) {
}
#endif /* SingleLinkedList_h*/
Here is the main.cpp file:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <ostream>
#include <iosfwd>
#include "SingleLinkedList.h"
int main(int argc, const char * argv[]) {
///////////////////////////////////////////////////////////////////////
///////////////////////////// Single Linked List //////////////////////
///////////////////////////////////////////////////////////////////////
SingleLinkedList<int> obj;
obj.push(2);
obj.push(4);
obj.push(6);
obj.push(8);
obj.push(10);
std::cout<<"\n--------------------------------------------------\n";
std::cout<<"---------------displaying all nodes---------------";
std::cout<<"\n--------------------------------------------------\n";
std::cout << obj << std::endl;
//
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"-----------------Inserting At End-----------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.insertTail(20);
// std::cout << obj << std::endl;
//
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"----------------Inserting At Start----------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.insertHead(50);
// std::cout << obj << std::endl;
//
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"-------------Inserting At Particular--------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.insertPosition(5,60);
// std::cout << obj << std::endl;
//
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"----------------Deleting At Start-----------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.deleteHead();
// std::cout << obj << std::endl;
//
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"----------------Deleting At End-----------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.deleteTail();
// std::cout << obj << std::endl;
//
//
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"--------------Deleting At Particular--------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.deletePosition(4);
// std::cout << obj << std::endl;
// std::cout << std::endl;
//
// obj.search(8) ? printf("Yes"):printf("No");
std::cin.get();
}
I assume most of the errors are syntax errors or that I just made very careless mistakes. Thanks.