3 / 3 / 0
Регистрация: 02.04.2017
Сообщений: 273
|
|
1
|
Ошибка при выводе из "хранилища объектов"
09.11.2017, 20:32. Показов 565. Ответов 3
Само хранилище это такая динамическая структура - стек в дереве.
Выглядит так:
Кликните здесь для просмотра всего текста
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| #include <memory>
#include <cstring>
#include "btree.h"
#include "stack.h"
#include "criteria.h"
template <class T>
class Container
{
public:
void add(const std::shared_ptr<T>& item);
void erase(const Criteria<T>& criteria);
template <class K>
friend std::ostream& operator << (std::ostream& os, const Container<K>& container);
private:
Btree<Stack<T>> m_container;
}; |
|
На оператор <<
Кликните здесь для просмотра всего текста
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
| template <class K>
std::ostream& operator << (std::ostream& os, const Container<K>& container)
{
if (container.m_container.size() == 0)
{
os << "================" << std::endl;
os << "Container is empty" << std::endl;
}
else
{
unsigned int containerCnt1 = 1;
for (auto subCont : container.m_container)
{
unsigned int containerCnt2 = 1;
os << "================" << std::endl;
os << "Container #" << (containerCnt1++) << ":" << std::endl;
for (auto subItem : *subCont)
{
os << "================" << std::endl;
os << "Item #" << (containerCnt2++) << ":" << std::endl;
subItem->print();
os << "Area: " << subItem->area() << std::endl;
}
}
}
return os;
} |
|
Компилятор ругается такими ошибками
Кликните здесь для просмотра всего текста
Bash | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| btree_impl.cpp: In instantiation of ‘Iterator<BTreeItem<T>, T> Btree<T>::end() const [with T = Stack<Figure>]’:
container_impl.cpp:93:3: required from ‘std::ostream& operator<<(std::ostream&, const Container<K>&) [with K = Figure; std::ostream = std::basic_ostream<char>]’
main.cpp:176:17: required from here
btree_impl.cpp:21:28: error: conversion from ‘const std::shared_ptr<BTreeItem<Stack<Figure> > >’ to non-scalar type ‘std::shared_ptr<Stack<Figure> >’ requested
std::shared_ptr<T> cur=m_root;
^
btree_impl.cpp:22:41: error: no matching function for call to ‘Iterator<BTreeItem<Stack<Figure> >, Stack<Figure> >::Iterator(std::shared_ptr<Stack<Figure> >&)’
return Iterator<BTreeItem<T>, T>(cur);
^
btree_impl.cpp:22:41: note: candidates are:
In file included from btree.h:6:0,
from main.cpp:1:
iterator.h:8:2: note: Iterator<N, T>::Iterator(const std::shared_ptr<_Tp1>&) [with N = BTreeItem<Stack<Figure> >; T = Stack<Figure>]
Iterator(const std::shared_ptr<N>& item);
^
iterator.h:8:2: note: no known conversion for argument 1 from ‘std::shared_ptr<Stack<Figure> >’ to ‘const std::shared_ptr<BTreeItem<Stack<Figure> > >&’
iterator.h:5:7: note: Iterator<BTreeItem<Stack<Figure> >, Stack<Figure> >::Iterator(const Iterator<BTreeItem<Stack<Figure> >, Stack<Figure> >&)
class Iterator
^
iterator.h:5:7: note: no known conversion for argument 1 from ‘std::shared_ptr<Stack<Figure> >’ to ‘const Iterator<BTreeItem<Stack<Figure> >, Stack<Figure> >&’
iterator.h:5:7: note: Iterator<BTreeItem<Stack<Figure> >, Stack<Figure> >::Iterator(Iterator<BTreeItem<Stack<Figure> >, Stack<Figure> >&&)
iterator.h:5:7: note: no known conversion for argument 1 from ‘std::shared_ptr<Stack<Figure> >’ to ‘Iterator<BTreeItem<Stack<Figure> >, Stack<Figure> >&&’
In file included from btree.h:32:0,
from main.cpp:1:
btree_impl.cpp: In member function ‘Iterator<BTreeItem<T>, T> Btree<T>::end() const [with T = Stack<Figure>]’:
btree_impl.cpp:23:1: error: control reaches end of non-void function [-Werror=return-type]
} |
|
На всякий случай добавлю все файлы, которые перечислялись выше
btree_impl.cpp
Кликните здесь для просмотра всего текста
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
| template <class T>
Btree<T>::Btree()
{
m_root=nullptr;
m_size = 0;
}
template <class T>
Iterator<BTreeItem<T>, T> Btree<T>::begin() const
{
return Iterator<BTreeItem<T>, T>(m_root);
}
template <class T>
Iterator<BTreeItem<T>, T> Btree<T>::end() const
{
std::shared_ptr<T> cur=m_root;
return Iterator<BTreeItem<T>, T>(cur);
}
template <class T>
void insert_helper(std::shared_ptr<BTreeItem<T>> m_root,const std::shared_ptr<T>& figure)
{
std::shared_ptr<BTreeItem<T>> node = m_root;
if(*figure == *(node->getFigure())){ std::cout<<*figure<<"\n"; std::cout<<*(node->getFigure())<<"\n"; std::cout<<"БЫЛО"<<"\n";}
else{
if(*figure < *(node->getFigure())) {
if(node->getLeft()==nullptr)
{
//std::cout<<"INSERT_LEFT";
std::shared_ptr<BTreeItem<T>> newl=std::make_shared<BTreeItem<T>>(figure);
node->setLeft(newl);
}
else
{
std::shared_ptr<BTreeItem<T>> left = node->getLeft();
insert_helper(left,figure);
}
}
else if(*figure > *(node->getFigure())) {
if(node->getRight()==nullptr)
{
//std::cout<<"INSERT_RIGHT";
std::shared_ptr<BTreeItem<T>> newr=std::make_shared<BTreeItem<T>>(figure);
node->setRight(newr);
}
else
{
std::shared_ptr<BTreeItem<T>> right = node->getRight();
insert_helper(right,figure);
}
}
}
}
template <class T>
void Btree<T>::bstInsert(const std::shared_ptr<T>& figure)
{
if(m_root==nullptr)
{
m_root = std::make_shared<BTreeItem<T>>(figure);//std::cout<<"INSERT_ROOT";
}
else
{insert_helper(m_root,figure);
++m_size;
}
}
template <class T>
void remove_helper(std::shared_ptr<BTreeItem<T>> & item, const std::shared_ptr<T>& figure)
{
std::shared_ptr<BTreeItem<T>> repl = nullptr, parent = nullptr, tmp = item;
while (tmp != nullptr && !(*(tmp->getFigure()) == *figure))
{
parent = tmp;
if (*figure < *(tmp->getFigure()))
{
tmp = tmp->getLeft();
}
else
{
tmp = tmp->getRight();
}
}
if (tmp == nullptr)
{
return;
}
if (tmp->getLeft() != nullptr && tmp->getRight() == nullptr)
{
if (parent != nullptr)
{
if (parent->getLeft() == tmp)
parent->setLeft(tmp->getLeft());
else
parent->setRight(tmp->getRight());
}
else
item = tmp->getLeft();
}
else if (tmp->getLeft() == nullptr && tmp->getRight() != nullptr)
{
if (parent != nullptr)
{
if (parent->getLeft() == tmp)
parent->setLeft(tmp->getLeft());
else
parent->setRight(tmp->getRight());
}
else
item = tmp->getRight();
}
else if (tmp->getLeft() != nullptr && tmp->getRight() != nullptr)
{
repl = tmp->getRight();
if (repl->getLeft() == nullptr) {
tmp->setRight(repl->getRight());
}
else
{
while (repl->getLeft() != nullptr)
{
parent = repl;
repl = repl->getLeft();
}
parent->setLeft(repl->getRight());
}
*(tmp->getFigure()) = *(repl->getFigure());
}
else
{
if (parent != nullptr)
{
if (parent->getLeft() == tmp)
parent->setLeft(nullptr);
else
parent->setRight(nullptr);
}
else
item = nullptr;
}
return;
}
template <class T>
void Btree<T>::bstRemove(const std::shared_ptr<T>& figure)
{
remove_helper(m_root, figure);
--m_size;
}
template <class T>
unsigned int Btree<T>::size() const
{
return m_size;
}
template <class T>
void print2(const std::shared_ptr<BTreeItem<T>> &node)
{
if(node == nullptr) return;
print2(node->getLeft());
std::cout << *(node->getFigure())<< std::endl;
print2(node->getRight());
}
template <class B>
std::ostream& operator<<(std::ostream& os, const Btree<B> &btree)
{
std::shared_ptr<BTreeItem<B>> item=btree.m_root;
print2(item);
return os;
} |
|
iterator.h
Кликните здесь для просмотра всего текста
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| template <class N, class T>
class Iterator
{
public:
Iterator(const std::shared_ptr<N>& item);
std::shared_ptr<N> getItem() const;
std::shared_ptr<T> operator * ();
std::shared_ptr<T> operator -> ();
Iterator left ();
Iterator right ();
Iterator operator ++ ();
//Iterator operator ++ (int index);
bool operator == (const Iterator& other) const;
bool operator != (const Iterator& other) const;
private:
std::shared_ptr<N> m_item;
}; |
|
0
|