Visual Studio MSC++编译器崩溃



我正在C++中实现一个列表类,我向该类添加了一些代码,每次构建编译器时,编译器似乎都会崩溃,如果我编写一个基本的"Hello World",它会干净地编译。这是列表的源代码。如果有人能解决这个问题,我们将不胜感激,这对我来说毫无意义,谢谢!

注意:该列表尚未完成,因此如果您看到错误和错误,请随时指出。

使用Visual Studio 2013旗舰版平台工具集:v120

#ifndef _LIST_HPP_
#define _LIST_HPP_
#include <initializer_list>
template<typename ty_> 
class List final {
public:
using value_type = ty_;
private:
using uint32_t = unsigned;
using init_list= std::initializer_list<value_type>;
typedef struct _Node {
_Node* _prevNode;
_Node* _nextNode;
value_type _value;
};
_Node* _begin; //begin points to another Node [delimeter] for iterators
_Node* _end;   //end points to another Node [delimeter] for iterators
uint32_t _size; //hold the size of the current list
public:
//Insert Enumerations
enum Location {
BEG,
MID,
END
};
class Forward_Iterator;
class Reverse_Iterator;
class Iterator final : public Reverse_Iterator, public Forward_Iterator;
//Constructors
List();                        //Initialize the List with null's
List(const List<value_type>&); //Copy constructor
List(const init_list&);        //Pre-Allocate with values to the List
~List();
//Methods
auto push_back(const value_type&) -> void;  //push 1 value on the back
auto push_back(const init_list&) -> void;   //push n values on the back
auto pop_back() -> void;                    //remove 1 value from the back
auto pop_back(const uint32_t&) -> void;     //remove n values from the back
auto push_front(const value_type&) -> void; //push 1 value on the front
auto push_front(const init_list&) -> void;  //push n values on the front
auto pop_front() -> void;                   //remove 1 value from the front
auto pop_front(const uint32_t&) -> void;    //remove n values from the front
//insert various values in a specified location based on an enumeration
auto emplace(const init_list&, const Location& = END) -> void;
auto insert(const uint32_t&, const value_type&)->Iterator&; //insert 1 value return added address
auto insert(const uint32_t&, const init_list&)->Iterator&;  //insert multiple values get the ending inserte address
auto insert(const uint32_t&, const Iterator&)->Iterator&;   //Insert 1 Node and get the inserted address
auto insert(const Iterator&, const Iterator&)->Iterator&;   //Insert 1 Node and get the inserted address
auto erase(const Iterator&)->Iterator&;    //erase and element by Iterator address
auto erase(const uint32_t&)->Iterator&;    //erase and element by poition in list
auto find(Iterator&)->Iterator&;     //find an element by Iterator address
auto find_first_of(const value_type&)->Iterator&;    //find the first of a specific value
auto find_last_of(const value_type&)->Iterator&;     //find the last of a specific value
auto empty() const -> bool;                  //check if empty
auto size() const->uint32_t;                 //get the size
auto begin()->Forward_Iterator&;             //beginning
auto end()->Forward_Iterator&;           //ending
auto cbegin() const->Forward_Iterator&;  //const beginning
auto cend() const->Forward_Iterator&;        //const ending
auto rbegin()->Reverse_Iterator&;            //reverse beginning
auto rend()->Reverse_Iterator&;          //reverse ending
auto crbegin() const->Reverse_Iterator&;     //const reverse beginning
auto crend() const->Reverse_Iterator&;   //const reverse ending
};
#pragma region List_Constructors
//Returns: Nothing
//Purpose: Initializes the private data members.
template<typename ty_>
List<ty_>::List() {
this->_begin = nullptr; 
this->_end = nullptr; 
this->_size = 0;
}
//Returns: Nothing
//Purpose: Initializes a new list with the data of another list.
template<typename ty_>
List<ty_>::List(const List<value_type>& CPY_LIST) {
this->_begin = nullptr;
this->_end = nullptr;
this->_size = 0;
_Node* currentNode = CPY_LIST._begin;
while (currentNode && (currentNode != CPY_LIST._end->_nextNode)){
this->push_back(currentNode->_value);
currentNode = currentNode->_nextNode;
}
}
//Returns: Nothing
//Purpose: Initialize n positions with specified values VIA initializer list.
template<typename ty_>
List<ty_>::List(const init_list& I_LIST) {
this->_begin = nullptr;
this->_end = nullptr;
this->_size = 0;
for (const auto& VAL : I_LIST)
this->push_back(VAL);
}
#pragma endregion
#pragma region List_Destructor
template<typename ty_>
List<ty_>::~List() {
if (this->_begin->_prevNode)
delete this->_begin->_prevNode;
_Node* current = this->_begin;
while (current && (current != this->_begin->_nextNode)){
_Node* toDelete = current;
current = current->_nextNode;
delete toDelete;
}
if (this->_end->_nextNode)
delete this->_end->_nextNode;
}
#pragma endregion
#pragma region Push_Methods
//Returns: void
//Purpose: Adds a new value to the end of the list.
template<typename ty_>
auto List<ty_>::push_back(const value_type& VAL) -> void {
if (!this->_begin){
//create the beginning node.
_Node* begNode = new _Node{ nullptr, this->_end, VAL };
begNode->_prevNode = new _Node{ nullptr, begNode, 0 };
this->_begin = begNode;
}
else if (!this->_end){
//create the ending node
_Node* endNode = new _Node{ this->_begin, nullptr, VAL };
endNode->_nextNode = new _Node{ endNode, nullptr, 0 };
this->_end = endNode;
this->_begin->_nextNode = this->_end;
}
else {
//extend the ending
this->_end->_nextNode->_value = VAL;
this->_end->_nextNode->_nextNode = new _Node{ this->_end->_nextNode, nullptr, 0 };
this->_end = this->_end->_nextNode;
}
++this->_size;
}
//Returns: void
//Purpose: Adds new values to the end of the list.
template<typename ty_>
auto List<ty_>::push_back(const init_list& I_LIST) -> void {
for (const auto& VAL : I_LIST)
this->push_back(VAL);
}
//Returns: void
//Purpose: Adds a new value to the beginning of the list.
template<typename ty_>
auto List<ty_>::push_front(const value_type& VAL) -> void {
if (!this->_begin){
//create the beginning node.
_Node* begNode = new _Node{ nullptr, this->_end, VAL };
begNode->_prevNode = new _Node{ nullptr, begNode, 0 };
this->_begin = begNode;
}
else if (!this->_end){
//create the ending to be the previous beginning
this->_begin->_nextNode = new _Node{ this->_begin, nullptr, this->_begin->_value };
this->_end = this->_begin->_nextNode;
this->_end->_nextNode = new _Node{ this->_end, nullptr, 0 };
this->_begin->_value = VAL;
}
else {
//Extend the beginning
this->_begin->_prevNode->_value = VAL;
this->_begin->_prevNode->_prevNode = new _Node{ nullptr, this->_begin->_prevNode, 0 };
this->_begin = this->_begin->_prevNode;
}
++this->_size;
}
//Returns: void
//Purpose: Adds new values to the beginning of the list.
template<typename ty_>
auto List<ty_>::push_front(const init_list& I_LIST) -> void {
for (const auto& VAL : I_LIST)
this->push_front(VAL);
}
#pragma endregion
#pragma region Pop_Methods
//Returns: void
//Purpose: Erases 1 element from the end of the list
template<typename ty_>
auto List<ty_>::pop_back() -> void {
}
//Returns: void
//Purpose: Erases n elements from the end of the list
template<typename ty_>
auto List<ty_>::pop_back(const uint32_t& POP_COUNT) -> void {
}
//Returns: void
//Purpose: Erases 1 element from the beginning of the list
template<typename ty_>
auto List<ty_>::pop_front() -> void {
}
//Returns: void
//Purpose: Erases n element from the beginning of the list
template<typename ty_> 
auto List<ty_>::pop_front(const uint32_t& POP_COUNT) -> void {
}
#pragma endregion
#pragma region Insert_Methods
template<typename ty_> 
auto List<ty_>::emplace(const init_list& I_LIST, const Location& LOC) -> void {
}
#pragma endregion
//Returns: uint32_t or unsigned int
//Purpose: Get the current list's size.
template<typename ty_> 
auto List<ty_>::size() const -> uint32_t {
return this->_size;
}
//Returns: Boolean
//Purpose: Checks whether the current list is empty.
template<typename ty_> 
auto List<ty_>::empty() const -> bool {
return ((this->_size) ? (true) : (false));
}
#pragma region Positional_Methods
//template<typename ty_> auto List<ty_>::begin() -> Forward_Iterator& {
//  return Forward_Iterator(this->_begin->_prevNode);
//}
//
//template<typename ty_> auto List<ty_>::cbegin() const -> Forward_Iterator& {
//  return Forward_Iterator(this->_begin->_prevNode);
//}
//
//template<typename ty_> auto List<ty_>::end() -> Forward_Iterator& {
//  return Forward_Iterator(this->_end->_nextNode);
//}
//
//template<typename ty_> auto List<ty_>::cend() const -> Forward_Iterator& {
//  return Forward_Iterator(this->_end->_nextNode);
//}
//
//template<typename ty_> auto List<ty_>::rbegin() -> Reverse_Iterator& {
//  return Reverse_Iterator(this->_begin->_prevNode);
//}
//
//template<typename ty_> auto List<ty_>::crbegin() const -> Reverse_Iterator& {
//  return Reverse_Iterator(this->_begin->_prevNode);
//}
//
//template<typename ty_> auto List<ty_>::rend() -> Reverse_Iterator& {
//  return Reverse_Iterator(this->_end->_nextNode);
//}
//
//template<typename ty_> auto List<ty_>::crend() const -> Reverse_Iterator& {
//  return Reverse_Iterator(this->_end->_nextNode);
//}
#pragma endregion
#endif

很难确定代码中的哪个问题给VC++带来了如此大的痛苦,但其中一些最明显的问题包括:

  1. 使用_Node(首字母下划线后跟大写字母表示此名称为保留名称)
  2. 尝试将Iterator声明为从Forward_IteratorReverse_Iterator派生而来,而不定义这两个基类中的任何一个(您所拥有的是声明,而不是定义)
  3. 拥有一个实际上没有声明名称的typedef

例如:

typedef struct _Node {
_Node* _prevNode;
_Node* _nextNode;
value_type _value;
};

对这样的情况使用typedef无论如何都是C的遗留问题。你几乎肯定只想要:

struct _Node {
_Node* _prevNode;
_Node* _nextNode;
value_type _value;
};

有了这些修复,代码至少应该有更好的编译机会。

如果你也去掉了无用的this->噪音,它甚至可能开始看起来像真正的C++,而不是Java或其他东西的扭曲版本。

为了进一步推进这一概念,您还应该开始使用成员初始化列表,例如替换:

template<typename ty_>
List<ty_>::List() {
_begin = nullptr; 
_end = nullptr; 
_size = 0;
}

带有:

template<typename ty_>
List<ty_>::List() : _begin(nullptr), _end(nullptr), _size(0)
{}

最新更新