friend ostream problem

This is a discussion on friend ostream problem within the C++ Programming forums, part of the General Programming Boards category; I have a class something like this Code: template class<T> class A{ private: T m; T n; List<T> *start; List<T> ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    5

    friend ostream problem

    I have a class something like this

    Code:
    template class<T>
    class A{
         private:
                    T m;
                    T n;
                    List<T> *start;
                    List<T> *end;
         public:
                    ...
                    friend ostream & operator<<(ostream &os, A<T> & dy);
    }

    Code:
    template class<T>
    ostream & operator<<(ostream &os, A<T> & dy)
    {
         os<<dy.start->x<<dy.start->y<<dy.start->value;
         return os;
    }
    x,y and value are some public elements of List<T>

    I do some operations on list and when I want to print the list elements , I wrote something like in implementation of operator<<, but i get segmentation error. How can i reach those x,y,and value in operator<< implementation?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    I note that the overload should be declared as:
    Code:
    friend std::ostream & operator<<(std::ostream &os, const A<T> & dy);
    That aside, the problem is probably with some other code, e.g., you forgot to initialise something, or you accessed something out of bounds, or you dereferenced a null pointer, etc.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    yeah i think i make one of those, but for example when i write below in other functions
    Code:
    cout<<start->x<<start->y<<start->value;
    i get what i want ; but in operator<< implementation this does not work :S

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,451
    Sorry, but there is just not enough information for me to help you. I suggest that you post the smallest and simplest compilable program that demonstrates the error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    From the looks of it, this should be relevant, but it is unclear why you don't get a linker error.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    5
    I do some stuff there to avoid linker errors
    how should i use this at there?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. ostream problem...
    By Ruchikar in forum C++ Programming
    Replies: 3
    Last Post: 10-18-2002, 10:53 AM
  5. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21