Skip to main content
gamedev.net
🔒 Locked

Problems while creating a template vector class!!

Started by SmD 15 replies 250+ views
Original Post
SmD
SmD
I just started a new project using OpenGL, and I want to create a vector class that will support integers and floating-point numbers. On that note, I decided to implement templates in my class but I've never used templates before. Does anyone know if it's possible to code a constructor definition (of a template class) in another file (i.e. vector.cpp instead of vector.h)? For some reason I always get a LNK2001 error when I try to use my vector template library on VisualC++. On the other hand, if I use a non-template version of my code, or I define my functions in the header file, I don't receive any errors (my dependencies are all correct). here's the code:
  
// Vector.h

#ifndef __VECTOR_H__
#define __VECTOR_H__

template < class T >
class cVector
{
    cVector();
...
};

#endif

***************************

// Vector.cpp

#include"vector.h"

template < class T >
cVector::cVector(): x(0), y(0) { }
...

        
[edited by - SmD on January 8, 2003 8:43:58 AM]
SmD
SmD
Thanks man... I don''t have the code with me at the moment (I''m at work) but I''ll try it out tonight!
MadKeithV
MadKeithV
I wouldn't do that! You'd get a circular include (vector.h includes vector.cpp includes vector.h) [edit: ifdef guards MAY solve that particular issue, I'm too lazy to try, but generally it's bad form anyway].

Just move the contents of the CPP file into the header, and delete the CPP file. Separate compilation is not supported for templates for any compiler but one(using the export statement).

Either produce the template implementation code inline, or out-of-class but in the same header file.


[edited by - MadKeithV on January 8, 2003 9:09:33 AM]
It's only funny 'till someone gets hurt.And then it's just hilarious.Unless it's you.
null_pointer
null_pointer
(The board just ate my post for the third time this week...)

quote:
Does anyone know if it''s possible to code a constructor definition (of a template class) in another file (i.e. vector.cpp instead of vector.h)?


I would not bother with it.

quote:
For some reason I always get a LNK2001 error when I try to use my vector template library on VisualC++


IIRC, Visual C++ 6.0 had problems with constructor templates of class templates. What is/are your constructor(s) definition(s)? What version of Visual C++ are you using? What is the exact error text from the LNK2001 error?
Qw3r7yU10p!
Qw3r7yU10p!
Might as well put everything in the one header file. I can''t think of a single good reason not to.

[OT]
If the board eats your post, push the back button and post it again, it usually works for me.
[/OT]
null_pointer
null_pointer
quote:
[OT]
If the board eats your post, push the back button and post it again, it usually works for me.
[/OT]


Until IE forgets the text in the text box...
civguy
civguy
quote:
Original post by null_pointer
Until IE forgets the text in the text box...
That''s why one should use Opera (among many other good reasons)
Stoffel
Stoffel
I''ve had the same problem, have the latest IE version & all critical patches. If I don''t see my post go through within 1 second of when I submit, I go back to the post and hit Ctrl+A Ctrl+C. If it''s a long post, I keep a backup on a text document on my desktop.

Think Liberally.
fup
fup
That''s strange. I used to have exactly the same problem. But I have recently been forced to reinstall win2000 (and all the updates etc) and now any text I type in the message field remains when get an error and hit back in IE. I just assumed it was fixed. Seems like MS''s playful poltergeist still finds a home in IE...




ai-junkie.com
null_pointer
null_pointer
quote:
That''s why one should use Opera (among many other good reasons)


Opera 7.0 (beta) makes my eyes bleed! Whoever created the new GUI must have a monitor with *very* low contrast. I sure hope that this is fixed in the release version.

quote:
Null: I haven''t had a problem with the latest versions of IE eating text


I think that it has something to do with the amount of time between seeing the error message and hitting the back button. If IE tries to reload the page from its cache, everything is hunky-dory. If IE tries to reload the page from the Internet, KA-BOOM! no more text.

quote:
I''ve had the same problem, have the latest IE version & all critical patches. If I don''t see my post go through within 1 second of when I submit, I go back to the post and hit Ctrl+A Ctrl+C. If it''s a long post, I keep a backup on a text document on my desktop.


I developed a similar strategy with the difference being that I type my replies in notepad first and then Select All/Copy/Paste the text.
Qw3r7yU10p!
Qw3r7yU10p!
is it related to security settings?

i use mozilla, it looks lovely and works fine.
civguy
civguy
quote:
Original post by null_pointer
Opera 7.0 (beta) makes my eyes bleed!
Just use the good old reliable 6.05

Shadowdancer
Shadowdancer
Back on topic.

The problem is that writing C++ compilers for templates is one of the most complicated tasks in that field. As a temporary solution, you can put the implementation inside the class definition. It will most probably work then.

The "correct" approach (as seen in Stroustrups "The C++ Programming Language", chapter 13.7 in ) is to "export" the implementation. To quote the book:


  
// out.h

template<class T> void out(const T& t);

//out.c

#include<iostream>
#include "out.h"

export template<class T> void out(comst T& t) { std::cerr << t; }


I don''t know however if this would work with template classes.

By the way, why aren''t you using STL vectors? You''re probably reinventing the wheel here.
Qw3r7yU10p!
Qw3r7yU10p!
As far as I know the 'export' keyword is currently only supported by one compiler and it's not vc++ (see first item of 'what's new?')

quote:
I don't know however if this would work with template classes


vc++ doesn't work with the example you've given let alone classes.

export is a keyword to be used with templated functions and classes.

[edited by - petewood on January 9, 2003 6:34:28 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.