2014-02-15 1 views
0

저는 C++로 목록 클래스를 구현하고 있습니다. 클래스에 코드를 추가하고 컴파일 할 때마다 컴파일러가 정상적으로 컴파일되는 기본 "Hello World"를 작성하면 충돌이 발생합니다. 다음은 목록의 소스 코드입니다. 누구든지 크게 감사 할 문제를 잘 수 있다면 그것은 단지 나에게 이해가 안가고있어, 고마워!Visual Studio MSC++ 컴파일러 충돌이 발생했습니다.

참고 : 목록이 완료되지 않았으므로 버그와 오류가 표시 되어도이를 지적 할 수 있습니다. 비주얼 스튜디오 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 
+1

그 개념을 촉진하기 위해, 당신은 또한 교체 등의 멤버 초기화 목록을 사용하기 시작한다 예약되지 않은 동작을 초래하는 예약 된 이름 (초기 밑줄 뒤에 대문자가옵니다). 나는 C++에 익숙한 사람이 상상할 수 없다. –

+0

모든 매크로를 제거했는데 여전히 똑같은 아이디어가 있습니까? –

답변

0

그것은 당신의 코드에서 문제 하는는 슬픔 ++ VC를주고 있지만, 가장 눈에 띄는 몇 가지 포함해야 어렵다 :

  1. _Node (초기 밑줄 뒤에 대문자가 오는 것은이 이름이 예약됨을 의미)을 사용하십시오.
  2. IteratorForward_IteratorReverse_Iterator에서 파생 된 것으로 선언하려고 시도했는데, 그 중 기본 클래스를 정의하지 않고 (정의가 아니라 선언 인 것) 정의했습니다.
  3. 실제로 이름을 선언하지 않는 typedef이 있습니다.
  4. 이와 같은

이 같은 경우의 typedef 사용

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

어쨌든 C에서 남아있다. 거의 확실하게 다음을 원할 것입니다 :

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

코드가 고정되어 있으면 코드를 컴파일 할 확률이 훨씬 높아야합니다.

쓸데없는 this-> 노이즈를 없애 버리면 변형 된 Java 버전이 아닌 실제 C++처럼 보이기 시작할 수도 있습니다.

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

을 ...으로 : 당신은 한 무리를 정의하여 시작하고

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

나는 문제를 추론하려고 노력하고 있었고 반복자 인 것처럼 보였으므로 다른 방법으로 구현할 것입니다. 여러분의 노력과 권장 사항에 대해 대단히 고마워요. –

관련 문제