帮助调用重载的插入运算符
我尝试调用超载的插入器,但它没有执行应有的操作。
#include <iostream>
#include "SortedLinkedListInt.h"
#include <sstream>
using namespace std;
//CONSTRUCTOR
SortedLinkedListInt::SortedLinkedListInt(){
head = NULL;
size = 0;
}
//DESTRUCTOR
SortedLinkedListInt::~SortedLinkedListInt(){
while (head != NULL) {
Node* ptr = head;
head = head -> next;
delete ptr;
}
}
//COPY CONSTRUCTOR
SortedLinkedListInt::SortedLinkedListInt(const SortedLinkedListInt &obj){
for(Node* n = obj.head; n!=NULL; n=n->next)
add(n->data);
}
void SortedLinkedListInt::add(int newElement){
if (head == NULL){
head = new Node;
head->next = NULL;
head->data = (newElement);
}
else if(head->data > newElement){
Node* node1 = new Node;
node1->data = newElement;
node1->next = head;
head = node1;
}
else{
Node* node2;
for(node2=head; node2->next!= NULL; node2 = node2->next)
if(node2->next->data > newElement)
break;
Node* node = new Node;
node->next = (node2->next);
node->data = (newElement);
node2->next = (node);
++size;
}
}
bool SortedLinkedListInt::exists (int element){
for (Node* n = head; n != NULL; n = n -> next) // how to write n.getElement() in c++
if(element == n->data) //analogous to compareTo (java)
return true;
return false;
}
void SortedLinkedListInt::toString(){
for (Node* n = head; n != NULL; n = n->next){
cout << n->data << endl;
}
cout << "\n";
}
void SortedLinkedListInt::operator <<(const int &sub){
add(sub);
for (Node* n = head; n != NULL; n = n->next){
cout << n->data << endl;
}
cout << "\n";
}
该函数位于上述头文件的底部。下面是main.cpp
#include <iostream>
#include "SortedLinkedListInt.h"
#include <cstdio>
using namespace std;
int main(){
SortedLinkedListInt *sll = new SortedLinkedListInt();
//SortedLinkedList <int> *sll = new SortedLinkedList<int>;
/*SortedLinkedList<int> sll2 = *sll;*/
sll->add(5);
sll->add(1);
sll->add(3);
sll->add(9);
sll->add(2);
sll->add(5);
cout << 5;
cout << 3;
//sll->toString();
int n = 4;
printf("%d does%s exist in list.\n", n, sll->exists(n) ? "": " not");
system("PAUSE");
}
cout << 5 或任何数字都不会调用重载的插入运算符。我想让它执行与 sll->(5) 相同的功能。因此,不用使用 sll->(x),只需使用 cout << x;
I tried calling my overloaded inserter but it's not doing what it's supposed to do.
#include <iostream>
#include "SortedLinkedListInt.h"
#include <sstream>
using namespace std;
//CONSTRUCTOR
SortedLinkedListInt::SortedLinkedListInt(){
head = NULL;
size = 0;
}
//DESTRUCTOR
SortedLinkedListInt::~SortedLinkedListInt(){
while (head != NULL) {
Node* ptr = head;
head = head -> next;
delete ptr;
}
}
//COPY CONSTRUCTOR
SortedLinkedListInt::SortedLinkedListInt(const SortedLinkedListInt &obj){
for(Node* n = obj.head; n!=NULL; n=n->next)
add(n->data);
}
void SortedLinkedListInt::add(int newElement){
if (head == NULL){
head = new Node;
head->next = NULL;
head->data = (newElement);
}
else if(head->data > newElement){
Node* node1 = new Node;
node1->data = newElement;
node1->next = head;
head = node1;
}
else{
Node* node2;
for(node2=head; node2->next!= NULL; node2 = node2->next)
if(node2->next->data > newElement)
break;
Node* node = new Node;
node->next = (node2->next);
node->data = (newElement);
node2->next = (node);
++size;
}
}
bool SortedLinkedListInt::exists (int element){
for (Node* n = head; n != NULL; n = n -> next) // how to write n.getElement() in c++
if(element == n->data) //analogous to compareTo (java)
return true;
return false;
}
void SortedLinkedListInt::toString(){
for (Node* n = head; n != NULL; n = n->next){
cout << n->data << endl;
}
cout << "\n";
}
void SortedLinkedListInt::operator <<(const int &sub){
add(sub);
for (Node* n = head; n != NULL; n = n->next){
cout << n->data << endl;
}
cout << "\n";
}
The function is at the bottom of the above header file. Below is the main.cpp
#include <iostream>
#include "SortedLinkedListInt.h"
#include <cstdio>
using namespace std;
int main(){
SortedLinkedListInt *sll = new SortedLinkedListInt();
//SortedLinkedList <int> *sll = new SortedLinkedList<int>;
/*SortedLinkedList<int> sll2 = *sll;*/
sll->add(5);
sll->add(1);
sll->add(3);
sll->add(9);
sll->add(2);
sll->add(5);
cout << 5;
cout << 3;
//sll->toString();
int n = 4;
printf("%d does%s exist in list.\n", n, sll->exists(n) ? "": " not");
system("PAUSE");
}
cout << 5 or any number wont call the overloaded insertion operator. I wanted to have it do the same function as sll->(5). So instead of using sll->(x) all that will be done is cout << x;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定你想要实现什么目标,但
cout << 5
调用cout标准流的插入运算符。如果你想调用你自己的插入运算符,至少语句的左边部分必须是你的类。
我希望这有帮助。
I am not sure what you are trying to achieve but
cout << 5
calls the insertion operator of cout standard stream. If you want to call your own insertion operator at least left part of the statement must be your class.
I hope this helps.