I'm trying to fix this problem that I have with pointers but I'm getting the message (error: request for member `print' in `ofstrings', which is of non-class type `list*'). Could someone tell me if it's possible to use a function that returns a pointer as a method for the type that the pointer is used for? Please don't ask about any elements like static methods that I wouldn't understand. I'm still trying to learn this stuff.
#include <iostream>
struct list {
struct Link {
string data;
Link * next;
Link(string a, Link * n = NULL);
};
Link * first;
Link * last;
list();
void add_to_end(string s);
void print();
};
list :: Link :: Link( string a, Link * n) {
data = a;
next = n;
}
list :: list() {
first = NULL;
last = NULL;
}
void list :: add_to_end(string s){
if (last != NULL) {
first = new Link(s,first);
} else {
first = new Link(s, NULL);
last = first;
}
}
void list :: print() {
if (first != NULL) {
Link * curr = first;
while (curr != NULL) {
cout << (curr -> data) <<"\n";
curr = curr -> next;
}
}
}
void cut_in_half(list * original, list * half1, list * half2) {
list :: Link * curr = original->first;
while (curr != NULL) {
half1->add_to_end(curr->data);
curr = curr -> next;
if (curr != NULL) {
half2->add_to_end(curr->data);
curr = curr -> next;
}
}
}
list * read() {
list * ofstrings = new list();
string temp;
cout << "Give me a string \n";
cin >> temp;
cout << "\n";
while (temp != "end") {
ofstrings->add_to_end(temp);
cout << "Give me another \n";
cin >> temp;
}
cout << "\n List constructed \n";
return ofstrings;
}
void main() {
list * ofstrings = read();
ofstrings.print();
list * temp1;
list * temp2;
cut_in_half(ofstrings, temp1, temp2);
}
It doesn't particularly matter for my assignment. My only problem is that somehow the cut_in_half function creates a "Buss error (core dumped) error. I can't figure out what I did wrong.
template <class T>
T ReturnPointer(T Type)
{
return Type;
};
This is fairly common with data structures, as the programmer only wants to create a system once, but reuse it for many different PDT. Templates work well for this.