GIDForums  

Go Back   GIDForums > Computer Programming Forums > CPP / C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-May-2006, 22:52
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Testing a template class


Hi all,

I'm inputting an example from my textbook of a template class and am getting run log errors:

Quote:
ZeroLink: unknown symbol '__ZN5dummyIiEC1Ev'

C11.3 L11.3 (dummy class) has exited due to signal 6 (SIGABRT).

Here is the header:
CPP / C++ / C Code:
/*
 *  dummy.h
 *  C11.3 L11.3 (dummy class)
 */

#ifndef DUMMY_H
#define DUMMY_H

template <class T>
class dummy
{
    public:
        //Constructor
        dummy();
        
        //Stores a value of type T
        void setItem(const T&);
        
        //Retrieves a value of type T
        T getItem() const;
    
    private:
        T item;             
        bool defined;
};
#endif

implementation:
CPP / C++ / C Code:
/*
 *  dummy.cpp
 *  C11.3 L11.3 (dummy class)
 */

#include "dummy.h"
#include <iostream>
using namespace std;

//Constructor
template <class T>
dummy<T>::dummy()
{
    defined = false;
}
        
//Stores a value of type T
template <class T>
void dummy<T>::setItem(const T& aVal)
{
    item = aVal;
    defined = true;
}

//Retrieves a value of type T
template <class T>
T dummy<T>::getItem() const
{
    if (defined)
        return item;
    else
        cerr << "Error - no value stored!" << endl;
}
driver:
CPP / C++ / C Code:
#include "dummy.h"
#include <iostream>
#include <string>
using namespace std;


int main () {
   
    dummy<int> numDepend;
    dummy<string> spouseName;
    int num;
    string name;
    
    //Store data in objects numDepend and spouseName
    numDepend.setItem(2);
    spouseName.setItem("Caryn");
    
    //Retrieve and display values stored
    num = numDepend.getItem();
    name = spouseName.getItem();
    cout << num << endl
         << name << endl;
         
    return 0;
}
I'm using XCode 2.2, OS 10.4.5. I am using Debug Build config, have tried turning ZeroLink on and off, and have tried removing "using namespace std;" and replacing with "using std::cout;" etc. I've checked the code at least three times - it's exactly as in the book. Any ideas?
  #2  
Old 12-May-2006, 23:26
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Testing a template class


Well, my book said I might have problems compiling. I ended up replacing the function prototypes in the class definition with the corresponding function definitions and was able to compile and run.

So is this just something wild and wooly that is beyond my ken right now, or does anyone have any ideas for me?

TIA
  #3  
Old 13-May-2006, 13:37
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Testing a template class


FWIW, someone at another forum recommended adding
Quote:
#include "dummy.cpp"
to my driver function, and that also did the trick. I wonder why my textbook didn't mention that as a possible solution.
  #4  
Old 13-May-2006, 14:48
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Testing a template class


Quote:
Originally Posted by earachefl
FWIW, someone at another forum recommended adding
Quote:
#include "dummy.cpp"
to my driver function, and that also did the trick. I wonder why my textbook didn't mention that as a possible solution.
Rotten solution. .cpp source files should not be included in programs. They should be compiled separately and linked into the final excutable.

Looks like you never added dummy.cpp to the compile instructions.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #5  
Old 13-May-2006, 15:01
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Testing a template class


Quote:
Originally Posted by WaltP
Rotten solution. .cpp source files should not be included in programs. They should be compiled separately and linked into the final excutable.

I'm not sure I'm understanding. The other poster meant including the #include directive only. so main.cpp would begin

CPP / C++ / C Code:
#include "dummy.h"
#include "dummy.cpp"
#include <etc>
using namespace whatever;

int main ()

etc......
Is that considered the same as adding the dummy.cpp implementation code directly in the main.cpp file?
Quote:
Looks like you never added dummy.cpp to the compile instructions.

Not sure what you are referring to - in the original post, no; I never have in my limited experience with classes. Or are you referring to including the file in the project?
  #6  
Old 13-May-2006, 15:39
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,234
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Testing a template class


Quote:
Originally Posted by earachefl
I'm not sure I'm understanding. The other poster meant including the #include directive only. so main.cpp would begin

CPP / C++ / C Code:
#include "dummy.h"
#include "dummy.cpp"
#include <etc>
using namespace whatever;

int main ()

etc......
Yes, I know that's what he meant. That is what you should never do!

Quote:
Originally Posted by earachefl
Is that considered the same as adding the dummy.cpp implementation code directly in the main.cpp file?
I suppose so. Buf if you're going to do that, you should include it directly (cut & paste), not by using the #include directive. Very bad habit to get into.



Quote:
Originally Posted by earachefl
Not sure what you are referring to - in the original post, no; I never have in my limited experience with classes. Or are you referring to including the file in the project?
Including the file in the project.
Make sure both files are compiled in the compile command
Make sure both object files are linked in the link command

For example, in my command line compiler, I would specify
Code:
comp driver.cpp dummy.cpp
This will compile both source files creating driver.obj and dummy.obj, and link the two object files together creating the file driver.exe

You will have to figure out how and where to do this in your compiler.
__________________

Cow: You're a lawyer too?
Mooseblood (mosquito): Ma'am, I was already a bloodsucking parasite. All I needed was a briefcase!
  #7  
Old 13-May-2006, 15:46
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: Testing a template class


Quote:
Originally Posted by WaltP
Looks like you never added dummy.cpp to the compile instructions.
Is that what the run log is saying? I'm doing what I always have been doing - adding a C++ class file with header to a project, and adding the #include "header.h" directive to the main .cpp file. Always worked before.
  #8  
Old 13-May-2006, 17:21
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,621
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Testing a template class


Quote:
Originally Posted by earachefl
Is that what the run log is saying? I'm doing what I always have been doing - adding a C++ class file with header to a project, and adding the #include "header.h" directive to the main .cpp file. Always worked before.

Template functions are treated differently by several compilers to which I have access.

For some compulers, if the main file accesses template functions (or classes with template functions), it must have visibility for the entire template definition, not just the header. That is, they apparently don't link everything from a table created from the template definition, but actually use the template implementation code to create code as each template specialization is instantiated. (Or, maybe not, but the point is, as you have observed, it's not enough just to include the header.)

That's why you sometimes see .h files with template function implementations inside, although one of the things that you have been told (up to now) is not to put anything that looks like it might actually generate executable code into a .h file.

Now, as Walt mentioned, it's usually considered bad form to #include "anything.cpp" in a file, so name the file with the template definitions "something.h", or if it's part of a class, you could put the template stuff into the same .h file with the class definitions. Both of these suggestions are different from what you have been doing (and what most of us do for things other than template functions). The important point is to realize that your discovery is not unique or unusual. Sometimes ya just gotta move on.

Sometimes this is covered in books, but it doesn't really mean anything until it reaches up out of the swamp and bites you in the butt.

If your book explanation (or my feeble attempt) doesn't ring your bell, you can find descriptions of whys and wherefores in lots of places.
For example http://www.parashift.com/c++-faq-lit...html#faq-35.12

One of the memorable lines from this FAQ: "Notice that foo-impl.cpp #includes a .cpp file, not a .h file. If that's confusing, click your heels twice, think of Kansas, and repeat after me, 'I will do it anyway even though it's confusing.' "


Regards,

Dave

"Don't look back --- Something may be gaining on you."
Satchell Paige
 

Recent GIDBlogNARMY by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
a tester class and then some. postage Java Forum 1 06-May-2006 15:48
an array class template ap6118 CPP / C++ Forum 1 25-Apr-2005 05:01
beginner::friend template class if13121 CPP / C++ Forum 1 04-Mar-2005 05:56
Error C2146: syntax error : missing ',' before identifier 'C4' mattchew008 CPP / C++ Forum 2 19-Dec-2004 06:06
hashing help saiz66 CPP / C++ Forum 1 06-Jul-2004 06:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.