![]() |
|
#1
|
|||
|
|||
Testing a template classHi all,
I'm inputting an example from my textbook of a template class and am getting run log errors: Quote:
Here is the header: CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
|
|
#2
|
|||
|
|||
Re: Testing a template classWell, 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
|
|||
|
|||
Re: Testing a template classFWIW, someone at another forum recommended adding
Quote:
|
|
#4
|
||||
|
||||
Re: Testing a template classQuote:
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
|
|||
|
|||
Re: Testing a template classQuote:
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:
Quote:
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
|
||||
|
||||
Re: Testing a template classQuote:
Quote:
Quote:
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:
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
|
|||
|
|||
Re: Testing a template classQuote:
|
|
#8
|
|||
|
|||
Re: Testing a template classQuote:
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 GIDBlog
NARMY by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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