14.12.2019, 00:33. Показов 2209. Ответов 1
Программа работает, за исключением совместной работы моего односвязного списка List и MyString, понять не могу, что не так.
Из мейна все потер, что не относится к проблеме
main.cpp:
| C++ |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| #include <iostream>
#include "MyString.h"
#include "List.h"
using namespace std;
int main()
{
List<MyString> lst;
MyString a1;
cin >> a1;
lst.push(a1);
return 0;
} |
|
MyString.h:
| 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
| #pragma once
#include <fstream>
using namespace std;
class MyString
{
public:
//конструктор без параметров
MyString();
//конструктор с параметром
MyString(const char* str);
//конструктор копирования
MyString(const MyString& other);
//перегрузка присваивания
MyString& operator=(const MyString& other);
MyString& operator=(const char* ch);
//перегрузка сложения
MyString& operator +(const MyString& other);
//перегрузка оператора +=
MyString& operator +=(const char c);
//перегрузка >
bool operator >(const MyString& other);
//перегрузка <
bool operator< (const MyString& other);
//перегрузка равенства
bool operator ==(const MyString& other);
//перегрузка неравенства
bool operator !=(const MyString& other);
//перегрузка индексирования
char& operator [](int i);
//вывод
friend ostream& operator<< (ostream& out, const MyString& str1);
//ввод
friend istream& operator>> (istream& in, MyString& point);
//длина строки
int MyStringLength();
//считывание строки из файла
void myGetline(istream& in, MyString& str1, const char e);
//деструктор - освобождение памяти
~MyString();
private:
char* str;
int length;
}; |
|
MyString.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
| #include "MyString.h"
#include <iostream>
#include <fstream>
using namespace std;
MyString::MyString()
{
str = nullptr;
length = 0;
}
MyString::MyString(const char* str)
{
length = strlen(str);
this->str = new char[length + 1];
for (int i = 0; i < length; i++)
{
this->str[i] = str[i];
}
this->str[length] = '\0';
}
MyString::MyString(const MyString& other)
{
/*if (this->str != nullptr)
{
delete[] str;
}*/
length = strlen(other.str);
this->str = new char[length + 1];
for (int i = 0; i < length; i++)
{
this->str[i] = other.str[i];
}
this->str[length] = '\0';
}
MyString& MyString::operator=(const MyString& other)
{
if (this->str != nullptr)
{
delete[] str;
}
length = strlen(other.str);
this->str = new char[length + 1];
for (int i = 0; i < length; i++)
{
this->str[i] = other.str[i];
}
this->str[length] = '\0';
return *this;
}
MyString& MyString::operator=(const char* ch)
{
if (this->str != nullptr)
{
delete[] str;
}
length = strlen(ch);
this->str = new char[length + 1];
for (int i = 0; i < length; i++)
{
this->str[i] = ch[i];
}
this->str[length] = '\0';
return *this;
}
MyString& MyString::operator +(const MyString& other)
{
MyString result;
int firstL = strlen(this->str);
int secondL = strlen(other.str);
result.length = firstL + secondL;
result.str = new char[firstL + secondL + 1];
int i = 0;
for (; i < firstL; i++)
{
result.str[i] = this->str[i];
}
for (int j = 0; j < secondL; j++, i++)
{
result.str[i] = other.str[j];
}
result.str[firstL + secondL] = '\0';
return result;
}
MyString& MyString::operator +=(const char c)
{
MyString result;
result.length = length + 1;
result.str = new char[length + 2];
for (int i = 0; i < length; i++)
result.str[i] = str[i];
result.str[length] = c;
result.str[length + 1] = '\0';
*this = result;
return *this;
}
bool MyString::operator >(const MyString& other)
{
return(!operator<(other));
}
bool MyString::operator< (const MyString& other)
{
return (strcmp(this->str, other.str) < 0);
}
bool MyString::operator ==(const MyString& other)
{
if (this->length == other.length)
{
for (int i = 0; i < this->length; i++)
{
if (this->str[i] != other.str[i]) return false;
}
return true;
}
else return false;
}
bool MyString::operator !=(const MyString& other)
{
return !(this->operator==(other));
}
char& MyString::operator[](int i)
{
return this->str[i];
}
ostream& operator<< (ostream& out, const MyString& str1)
{
out << str1.str;
return out;
}
istream& operator>> (istream& in, MyString& str1)
{
str1.myGetline(in, str1, '\n');
in.clear();
return in;
}
void MyString::myGetline(istream& in, MyString& str1, const char e)
{
char r;
while (in.get(r))
{
if (r == e)
break;
str1 += r;
}
}
int MyString::MyStringLength()
{
return length;
}
MyString::~MyString()
{
delete[] this->str;
} |
|
List.h:
| 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
| #pragma once
#include <iostream>
using namespace std;
template<typename T>
class List
{
public:
List();
~List();
//удаление первого элемента в списке
void pop_front();
//добавление элемента в конец списка
void push_back(T data_);
//добавление с сортировкой
void push(T data_);
// очистить список
//void push(T data_);
void clear();
// получить количество елементов в списке
int GetSize() { return Size; }
// перегруженный оператор []
T& operator[](const int index);
//добавление элемента в начало списка
void push_front(T data_);
//добавление элемента в список по указанному индексу
void insert(T data_, int index);
//удаление элемента в списке по указанному индексу
void removeAt(int index);
//удаление последнего элемента в списке
void pop_back();
friend void sort(List l);
private:
template<typename T>
class Node
{
public:
Node* pNext;
T data_;
Node(T data_ = T(), Node* pNext = nullptr)
{
this->data_ = data_;
this->pNext = pNext;
}
};
int Size;
Node<T>* head;
}; |
|
List.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
| #include "List.h"
template<typename T>
List<T>::List()
{
Size = 0;
head = nullptr;
}
template<typename T>
List<T>::~List()
{
clear();
}
template<typename T>
void List<T>::pop_front()
{
Node<T>* temp = head;
head = head->pNext;
delete temp;
Size--;
}
template<typename T>
void List<T>::push_back(T data_)
{
if (head == nullptr)
{
head = new Node<T>(data_);
}
else
{
Node<T>* current = this->head;
while (current->pNext != nullptr)
{
current = current->pNext;
}
current->pNext = new Node<T>(data_);
}
Size++;
}
//////////////////////////////////////PUSH_START
template<typename T>
void List<T>::push(T data_)
{
if (head == nullptr)
{
head = new Node<T>(data_);
}
else
{
Node<T>* current = this->head;
while (current->pNext != nullptr && current->pNext->data_ < data_)
current = current->pNext;
Node<T>* node = new Node<T>(data_);
node->pNext = current->pNext;
current->pNext = node;
}
Size++;
}
//////////////////////////////////////PUSH_END
template<typename T>
void List<T>::clear()
{
while (Size)
{
pop_front();
}
}
template<typename T>
T& List<T>::operator[](const int index)
{
int counter = 0;
Node<T>* current = this->head;
while (current != nullptr)
{
if (counter == index)
{
return current->data_;
}
current = current->pNext;
counter++;
}
}
template<typename T>
void List<T>::push_front(T data_)
{
head = new Node<T>(data_, head);
Size++;
}
template<typename T>
void List<T>::insert(T data_, int index)
{
if (index == 0)
{
push_front(data_);
}
else
{
Node<T>* previous = this->head;
for (int i = 0; i < index - 1; i++)
{
previous = previous->pNext;
}
Node<T>* newNode = new Node<T>(data_, previous->pNext);
previous->pNext = newNode;
Size++;
}
}
template<typename T>
void List<T>::removeAt(int index)
{
if (index == 0)
{
pop_front();
}
else
{
Node<T>* previous = this->head;
for (int i = 0; i < index - 1; i++)
{
previous = previous->pNext;
}
Node<T>* toDelete = previous->pNext;
previous->pNext = toDelete->pNext;
delete toDelete;
Size--;
}
}
template<typename T>
void List<T>::pop_back()
{
removeAt(Size - 1);
}
template <typename T>
void sort(List <T> l)
{
for (int i = 1; i < l.GetSize; i++)
{
for (int j = i; j > 0 && l[j - 1] > l[j]; j--)
{
List a;
a = l[j - 1];
l[j - 1] = l[j];
l[j] = a;
}
}
} |
|
Выдает 2 unresolved externals:
1)unresolved external symbol "public: __thiscall List<class MyString>::List<class MyString>(void)" (??0?$List@VMyString@@@@QAE@XZ) referenced in function _main
2)unresolved external symbol "public: __thiscall List<class MyString>::~List<class MyString>(void)" (??1?$List@VMyString@@@@QAE@XZ) referenced in function _main