Pointer Problems

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);
}
ofstrings.print();

You need to use -> instead of .
I got "Bus error (core dumped)"
Bump?
Yo Gulopey, did you mean for the print part to print the strings starting the at the last string you entered?
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.
consider templates.

1
2
3
4
5
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.
Lol fixed, just had to initialise the temp1 and temp2 lists. Thank God for compiler warnings

1
2
list * temp1(new list());
list * temp2(new list());


E: Why is it that when you have space seperated strings, it treats them as seperate strings instead of one?

E2: nvm, just used getline
Last edited on
Hey I did what you suggested smac89, but all it did was convert the error from bus error core dumped to Segmentation fault (core dumped).
Thanks for your help guys I was able to work it out.
Topic archived. No new replies allowed.