Jump to content

  • Log In with Google      Sign In   
  • Create Account


Using nested structure as template


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 haansn08   Members   -  Reputation: 172

Like
0Likes
Like

Posted 01 August 2013 - 03:23 AM

Hello there,

I'm trying to build a class that is responsible for loading and managing a vertex shader.

The idea is to have a separate class for managing the constant buffer (the data a vertex shader needs.)

This class looks like this:

 

dxman_cbuffer.h

#pragma once
#include "dxman_common.h"
#include <D3D11.h>

template <typename Buffer>
class ConstantBuffer
{
private:
	ID3D11Buffer *_buffer;
	const Buffer *_content;
public:
	HRESULT Init(ID3D11Device* dev, Buffer* initialData);
	void Exit();
	ID3D11Buffer* GetBuffer() const;
	HRESULT SetBufferData (ID3D11DeviceContext* devcon, Buffer *content);
	const Buffer* GetBufferData() const;
};

The problem occours in this class:

 

ColorShader.h

#pragma once
#include "common.h"
#include "dxman_shader.h"
#include "dxman_cbuffer.h"

class VSColor : public IVertexShader
{
private:
	struct SMatrixBuffer
	{
		XMMATRIX worldViewProj;
	};
	ConstantBuffer<SMatrixBuffer> _cbPerObject;
public:
	virtual HRESULT Init (ID3D11Device *dev); //Loads the shader & creates the constant buffer
	virtual void Exit(); //Releases all resources 
	virtual void Activate (ID3D11DeviceContext* devcon); //Sets the shader and the constant buffer to the Vertex Shader stage
	virtual HRESULT UpdateMatrixBuffer (ID3D11DeviceContext *devcon, XMMATRIX &worldViewProj); //Updates the buffer data
};

Since the template class ConstantBuffer<SMatrixBuffer> is never declared, the linker can't find any symbols. The problem is that I can't just write ConstantBuffer<SMatrixBuffer> in dxman_cbuffer.cpp (where the ConstantBuffer class is) since SMatrixBuffer is declared as private in another class. (And I want to keep both files separated!)



Sponsor:

#2 Katie   Members   -  Reputation: 1235

Like
0Likes
Like

Posted 01 August 2013 - 04:44 AM

Shouldn't it be "ConstantBuffer<struct SMatrixBuffer> _cbPerObject;" ?



#3 SiCrane   Moderators   -  Reputation: 9124

Like
2Likes
Like

Posted 01 August 2013 - 05:03 AM

Long story short, you can't put template function definitions in a source file without special compiler support that only a very few compilers have ever had. You need to move the template function definitions from dxman_cbuffer.cpp to the header. 



#4 BrentChua   Crossbones+   -  Reputation: 1066

Like
0Likes
Like

Posted 01 August 2013 - 06:21 AM

Just an addition to what SiCrane said, the error you're having about missing symbols is because the compiler cannot find the function definitions when places in the .cpp. So move all your code in the .cpp in the header file. But if all you're after is just a physical file separation, you could create a separate file call it something like dxman_cbuffer.hpp. Put all your function definitions there then include that .hpp file at the bottom of the header file of your dxman_cbuffer.h.


Edited by BrentChua, 01 August 2013 - 06:22 AM.


#5 haansn08   Members   -  Reputation: 172

Like
0Likes
Like

Posted 01 August 2013 - 06:42 AM

OK, it works. Altough I really have no clue why... happy.png



#6 Matt-D   Crossbones+   -  Reputation: 1405

Like
0Likes
Like

Posted 03 August 2013 - 04:11 PM

Since you're only using "Buffer" type indirectly, i.e., as a "Buffer *" (ptr-to-Buffer), I suppose you could try a workaround (if you're willing to change your ConstantBuffer class), with forward declaration of "SMatrixBuffer" and using "SMatrixBuffer *" as a template argument in the instantiation. Beforehand, you'd need an explicit template instantiation in the same translation unit (extra or existing file, whatever you find most convenient) that has access to the forward-declared template argument.

 

For more details / examples, see:

http://www.network-theory.co.uk/docs/gccintro/gccintro_60.html

http://publib.boulder.ibm.com/infocenter/macxhelp/v6v81/index.jsp?topic=%2Fcom.ibm.vacpp6m.doc%2Flanguage%2Fref%2Fclrc16explicit_instantiation.htm

http://en.cppreference.com/w/cpp/language/class_template#Explicit_instantiation

 

Since you're only ever going to use incomplete types, this will even work with smart pointers, like std::unique_ptr (using which you should strongly consider, since it's zero-overhead anyway -- otherwise, by leaving raw pointers everywhere you're keeping your ownership semantics assumptions unstated, undocumented, and unenforced, which is rather bad):

http://stackoverflow.com/questions/10065384/instantiation-of-a-list-with-an-incomplete-type-in-a-typedef

 

If it sounds involved, it's because it is ;-) If you're fine with moving to a header-only template implementation, it's probably the simpler / recommended way.

In any case, reconsider using raw pointers, http://klmr.me/slides/modern-cpp/ ;-)


Edited by Matt-D, 03 August 2013 - 04:12 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS