简体   繁体   中英

C++ overloaded << operation only prints memory address of LinkedList

I am trying to make a Linked List Container LLC and overload the << operator as a friend. It seems like no mater what I do it either prints the memory address or throws a segmentation fault. I dont totally understand c++ yet so it is probably something obvious.

in LLC.cpp

ostream& operator<<(ostream& os, const LLC* list){
   Node *curr = list.first;
   for(curr; curr != NULL; curr= curr -> next){
   os << curr -> data << "\n";
}
return os;
}

int main(int argc, char *argv[]){
  string arr [] = {"a","b","c","d","e","f"};
  LLC* link = new LLC(arr);
  cout<<"testing: ";
  cout<<(link);
}

in LLC.h

struct Node {
 std::string data;
 Node *next;
};
class LLC {

private:
  Node *first;
  Node *last;
public:
    int main(int argc, char *argv[]);
    LLC(){
      first=NULL;
      last=NULL;
    }
    friend std::ostream& operator<<(std::ostream& os, const LLC*);

std::ostream already has an operator<< that takes a pointer (via void* ) as input. That is why you get hex output. Your operator<< overload should be taking in the LLC object by const reference, not by const pointer, to avoid ambiguities.

You did not post enough code to demonstrate the segfault error, but I suspect it is because you are not managing your node instances correctly inside the list, so when your operator<< tries to access them, it encounters a bad pointer along the way and crashes.

Try something more like this:

LLC.h

#ifndef LLCH
#define LLCH

#include <iostream>
#include <string>

struct Node {
    std::string data;
    Node *next;

    Node(const std::string& d);
};

class LLC {
private:
    Node *first;
    Node *last;

public:
    LLC();

    template<size_t N>
    LLC(std::string (&arr)[N])
        : first(NULL), last(NULL) {
        for(size_t i = 0; i < N; ++i) {
            addToBack(arr[i]);
        }
    }

    // other constructors, destructor, etc...

    void addToBack(const std::string &s);

    // other methods ...

    friend std::ostream& operator<<(std::ostream& os, const LLC& list);

    // other operators ...
};

#endif

LLC.cpp

#include "LLC.h"

Node::Node(const std::string& d)
    : data(d), next(NULL) {
}

LLC::LLC()
    : first(NULL), last(NULL) {
}

// other constructors, destructor, etc...

void LLC::addToBack(const std::string &s) {
    Node *n = new Node(s);
    if (!first) first = n;
    if (last) last->next = n;
    last = n;
}

// other methods ...

std::ostream& operator<<(std::ostream& os, const LLC& list) {
    for(Node *curr = list.first; curr != NULL; curr = curr->next) {
        os << curr->data << "\n";
    }
    return os;
}

// other operators ...

Main.cpp

#include <iostream>
#include <string>
#include "LLC.h" 

int main(int argc, char *argv[]) {
    std::string arr[] = {"a", "b", "c", "d", "e", "f"};

    LLC* link = new LLC(arr);
    cout << "testing: " << *link;
    delete link;

    /* or simply:
    LLC link(arr);
    cout << "testing: " << link;
    */

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM