vector3d class problems

Questions regarding game mechanics and graphic programming should go here.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

vector3d class problems

Postby taymo2020 » Sat Jul 07, 2007 1:58 am

In my particle system I use my 3D vector code vector3d (and 2d). Now I'm getting this error:
Code: Select all
unresolved external symbol "public: __thiscall vector3d<float>::vector3d<float>(void)" (??0?$vector3d@M@@QAE@XZ) referenced in function "public: __thiscall CParticle::CParticle(enum P_RENDERMETHOD,float,float)" (??0CParticle@@QAE@W4P_RENDERMETHOD@@MM@Z)


I think it's talking about this constructor:
Code: Select all
template <class T>
vector3d<T>::vector3d(): x(0), y(0), z(0)
{}


Being referenced in this code:
Code: Select all
CParticle::CParticle(P_RENDERMETHOD render,float fadeRate, float life) :
m_renderMethod(render), m_fadeRate(fadeRate), m_life(life)
{}


yet none of those parameters are the vector3d class. Any ideas? Ask to see full code.
taymo2020
 
Posts: 998
Joined: Mon Mar 07, 2005 5:39 pm
Location: On the pot..

Postby MXP » Sat Jul 07, 2007 2:04 am

If you have separated your vector3d class into a .h and .cpp file then this is the problem. When creating templates you must include all of the code in the header file.
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Postby taymo2020 » Sat Jul 07, 2007 3:15 pm

What? Why?
taymo2020
 
Posts: 998
Joined: Mon Mar 07, 2005 5:39 pm
Location: On the pot..

Postby Wizard » Sat Jul 07, 2007 10:04 pm

Because it's really hard for the compiler to implement the other way. Same reason why you need to put a space when using a template of a template:
Code: Select all
foo<bar<int> > bas;
            ^space here

The standard says you don't need a space, but otherwise it's hard to implement, so most compilers don't even try.
User avatar
Wizard
Site Admin
 
Posts: 3250
Joined: Mon Sep 22, 2003 4:52 pm
Location: ON, CA

Postby Emery » Sat Jul 07, 2007 10:38 pm

I don't think the standard does allow the lack of space because I remember reading that it is a proposal for C++ 0x.
--~~~~
User avatar
Emery
 
Posts: 4313
Joined: Sat Mar 19, 2005 9:16 am

Postby MXP » Sun Jul 08, 2007 4:03 am

taymo2020 wrote:What? Why?

Templates are not complete code. They're more like supped-up macros. If I understand correctly, when you instantiate a template the compile needs to know exactly what code to create which it wont have if you move that off into a separate translation unit (.cpp file).
Need information on a function I've posted? Chances are it's at the MSDN.
MXP
 
Posts: 6506
Joined: Mon Sep 22, 2003 5:27 pm

Postby Emery » Sun Jul 08, 2007 7:02 pm

I think "you must include all of the code in the header file" might be misleading. It's not necessarily true unless you need to use the templates in multiple source files. But unlike functions which you can just specify the function header which says how the function is to be called and leave the function body anonymous so that it doesn't need to recompile everything that calls it when the body changes, the body of templates needs to be fully known for its usage because the template arguments are supplemented at compile time. It's a form of metaprogramming. Like Colin said, they are like macros on steroids.

Code: Select all
template<class T>
class foo
{
public:
    T var;
    T get() { return var; }
};


foo<int> would generate this class:
Code: Select all
class foo
{
public:
    int var;
    int get() { return var; }
};


And, in case you're wondering, foo<ROCKETSAUCE> would generate this:

Code: Select all
class foo
{
public:
    ROCKETSAUCE var;
    ROCKETSAUCE get() { return var; }
};


And the compiler would give you errors about ROCKETSAUCE not being defined (assuming it isn't) at T usages in the template class, and that's why templates can produce some of the most obscure errors ever.


But, one reason why they are different from macros is that they do give the compiler some more ability. Template usages of specific arguments will most likely generate the code only once and use it each time.
--~~~~
User avatar
Emery
 
Posts: 4313
Joined: Sat Mar 19, 2005 9:16 am


Return to Games and Graphics

Who is online

Users browsing this forum: No registered users and 2 guests