Thread: Using cout in template implementation

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    Using cout in template implementation

    I have a question about trying to debug my C++ program. I'm programming in Eclipse and can't get the debugger to work, probably because of spaces in my project name. That's fine for now.

    I'm trying to just use cout to debug by printing out things. I have a file called test.cc which contains my main method and a line in it that says list2 = list1; I have a file dlist.h which defines the template, and a file dlist.cc which implements it. It works fine until the line list2=list1 in test.cc. I am using operator overloading like so:
    dlist.c
    Code:
    template <typename T>
    Dlist<T>& Dlist<T>::operator=(const Dlist &l)
    {
    	cout << "COPY" <<endl;
    	if(this!=&l)
    		copyAll(l);
    	return *this;
    }
    My thought is, because I put a cout here, I should get COPY printed when I do list2=list1 even if I implemented copying incorrectly, but I don't ever see this. The program compiles and isn't crashing or anything. I #include <iostream> and using namespace std; in my dlist.cc

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by jcafaro10 View Post
    I have a file dlist.h which defines the template, and a file dlist.cc which implements it.
    You can't split templates up like that. Everything has to be in the header file.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I'm new to C++ but I'm pretty sure that thats not the problem. The .h file includes the .cc file and this works. It's my overloading of the assignment operator thats wrong but for now I'd just like to know why I can't print using cout

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    If it is compiling, and your linker is including an up-to-date object from dlist.c, then you have to conclude that the function isn't being called. I'm curious, does Eclipse support the export keyword? What cpjust said is true.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I guess the function isn't being called but how do you call the assignment operator if not simply with "=" ? Every other function works/gets called when I call it except for the = one.
    dlist.h
    Code:
    #ifndef __DLIST_H__
    #define __DLIST_H__
    
    class emptyList
    {
        // OVERVIEW: an exception class
    };
    
    template <typename T>
    class Dlist
    {
        // OVERVIEW: contains a double-ended list of Objects
    
     public:
    
        // Operational methods
    
        bool isEmpty();
        // EFFECTS: returns true if list is empy, false otherwise
    
        void insertFront(T *o);
        // MODIFIES this
        // EFFECTS inserts o at the front of the list
    
        void insertBack(T *o);
        // MODIFIES this
        // EFFECTS inserts o at the back of the list
    
        T *removeFront();
        // MODIFIES this
        // EFFECTS removes and returns first object from non-empty list
        //         throws an instance of emptyList if empty
    
        T *removeBack();
        // MODIFIES this
        // EFFECTS removes and returns last object from non-empty list
        //         throws an instance of emptyList if empty
    
        // Maintenance methods
        Dlist();                                   // ctor
        Dlist(const Dlist &l);                     // copy ctor
        Dlist &operator=(const Dlist &l);          // assignment
        ~Dlist();                                  // dtor
    
     private:
        // A private type
        struct node
    	{
    		node   *next;
    		node   *prev;
    		T      *o;
        };
    
        node   *first; // The pointer to the 1st node (NULL if none)
        node   *last;  // The pointer to the 2nd node (NULL if none)
    
        // Utility methods
    
        void makeEmpty();
        // EFFECT: called by constructors/operator= to establish empty
        // list invariant
    
        void removeAll();
        // EFFECT: called by destructor/operator= to remove and destroy
        // all list elements
    
        void copyAll(const Dlist &l);
        // EFFECT: called by copy constructor/operator= to copy elements
        // from a source instance l to this instance
    };
    
    /*
    Note: this is here *only* because the gnu compiler needs to see the
    "templatized" versions of your methods.  This is the *only* instance
    in which it is acceptable to #include a cc file.
    */
    
    #include "dlist.cc"
    
    #endif /* __DLIST_H__ */

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Include the contents of the source file in this header file and tell us what happens.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I don't understand? Do you want me to post the dlist.cc file? Or put the contents IN the header file? Isn't that the point of the include? Isn't that what include does?

    Either way, I copied the source of dlist.cc and pasted it into dlist.h (removing the include dlist.cc) and got the same result.

    This may sound strange but I actually DO believe that the method is getting called. Here's why:
    Code:
    Dlist<int> *ilist1 = new Dlist<int>();
    Dlist<int> *ilist2 = new Dlist<int>();
    ilist2 = ilist1;
    delete ilist1;
    delete ilist2;
    This gives an error "Delete called on bad pointer/corrupted block" which makes me think that ilist2=ilist1 is doing something, probably calling my incorrect operator overloading method, but just not printing what I said to. If I comment out the ilist2=ilist1, everything deletes fine.

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Other cc files see the header's declaration, but they aren't able to look for the particular generated function in another compilation unit. I don't know why. Take the code from the cc file and put it into the header file, and do away with the cc file for now and see what happens.
    In general, templates, definitions and enums go in headers, while everything else goes in source files.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jcafaro10 View Post
    I don't understand? Do you want me to post the dlist.cc file? Or put the contents IN the header file? Isn't that the point of the include? Isn't that what include does?

    Either way, I copied the source of dlist.cc and pasted it into dlist.h (removing the include dlist.cc) and got the same result.

    This may sound strange but I actually DO believe that the method is getting called. Here's why:
    Code:
    Dlist<int> *ilist1 = new Dlist<int>();
    Dlist<int> *ilist2 = new Dlist<int>();
    ilist2 = ilist1;
    delete ilist1;
    delete ilist2;
    This gives an error "Delete called on bad pointer/corrupted block" which makes me think that ilist2=ilist1 is doing something, probably calling my incorrect operator overloading method, but just not printing what I said to. If I comment out the ilist2=ilist1, everything deletes fine.
    Are you sure that's what you want to do [because the error message is EXACTLY what I expect from this] - you are not copying the object from ilist1 to ilist2, but the address of ilist1 is also stored in ilist2. So when you delete ilist1 and then delete ilist2, you are actually deleting ilist1 again - which of course gives the "double delete" message. As a side-effect of this, you are also leakping the memory of ilist2.

    Perhaps you wanted to copy the object from one to the other?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    I figured it out. I had to dereference both pointers to get it to do the copy.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jcafaro10 View Post
    I figured it out. I had to dereference both pointers to get it to do the copy.
    Yes, that will do something different from what you wrote - but it's often hard to figure out what you actually mean to do from code-snippets like this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by jcafaro10 View Post
    I figured it out. I had to dereference both pointers to get it to do the copy.
    I figured it out?
    Does that mean you experimented to get the desired result or because a mistake that you found and corrected?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implementation of a template class functions
    By 39ster in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2008, 03:36 AM
  2. error: template with C linkage
    By michaels-r in forum C++ Programming
    Replies: 3
    Last Post: 05-17-2006, 08:11 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. What could be wrong about this template?
    By kromozom in forum C++ Programming
    Replies: 10
    Last Post: 12-27-2004, 01:15 AM
  5. queue linked implementation
    By technoXavage in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2001, 04:29 PM