Sign in to follow this  
Ncyphe

template to define a template

Recommended Posts

I have a small problem.

I'm trying to define a templated class called SectorMap for the purpose of acting like a grid (2D or 3D). I want to use a vector to maintain all of the data, but I keep running into problems.


using std::vector;

template<class T>
class SectorMap
{
private:
vector<T> VecChart;

...



basically, the compiler keeps throwing errors with vector<T>, and I've been trying to come up with a way to by pass that . . . and yet failed each time.

Is there anyway around this?

Note: The reason I'm building this is for a portfolio project I'm working on. I plan to use this data structure to store information about entities in each 3D sector.

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
You may need to write it as:
typename std::vector< T > VecChart;


I don't think that would be the problem. typename is only needed for dependent names.

To the O.P: can you compile and run this:


#include <vector>

template<typename T>
struct S
{
std::vector<T> v;
};

int main()
{
S<int> s;
return 0;
}


But as Wyrframe says, the actual error message would be the most helpful aid at this point.

Share this post


Link to post
Share on other sites
I'm sorry, I was in such a hurry that I didn't even think about the error code.

Here's the snippet from the compuile log:

1>------ Build started: Project: Testing_Grounds, Configuration: Debug Win32 ------
1>Build started 10/14/2010 7:45:12 AM.
1>InitializeBuildStatus:
1> Touching "Debug\Testing_Grounds.unsuccessfulbuild".
1>ClCompile:
1> main.cpp
1> CSectorMap.cpp
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1307): error C2036: 'T *' : unknown size
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\vector(1301) : while compiling class template member function 'void std::vector<_Ty>::_Tidy(void)'
1> with
1> [
1> _Ty=T
1> ]
1> c:\users\ben\documents\visual studio 2010\projects\testing_grounds\testing_grounds\csectormap.h(24) : see reference to class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=T
1> ]
1> c:\users\ben\documents\visual studio 2010\projects\testing_grounds\testing_grounds\csectormap.cpp(3) : see reference to class template instantiation 'CSectorMap<T>' being compiled
1> with
1> [
1> T=T
1> ]
1> Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.65
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Hope that helps in figuring out a solution.

Share this post


Link to post
Share on other sites
How are you using your template class?

I would wager (from the error code) that you're using it as, for instance:
CSectorMap<T> map;
In which case you need to put an actual type there like 'int' instead of T.

Edit:
or it seems you're putting member functions in a source file, e.g.,:
void CSectorMap<T>::foo()
{
}
This doesn't work. All template member functions must be in the header file.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mike nl
How are you using your template class?

I would wager (from the error code) that you're using it as, for instance:
CSectorMap<T> map;
In which case you need to put an actual type there like 'int' instead of T.

Edit:
or it seems you're putting member functions in a source file, e.g.,:
void CSectorMap<T>::foo()
{
}
This doesn't work. All template member functions must be in the header file.


So, I cannot use a .cpp file to define functions for a templated class?

I'll try that and see what it gets me.

Share this post


Link to post
Share on other sites
Thank You Everyone.

I wanted to post my (near) final code for my CSectorMap


//
//Description:
// A sector map is a data structure designed to store information on an [x,y,z] grid.
// Its primary utilization is in tracking object placement in a 3D grid
//
// CSectorMap can be used to represent bot a 2D or 3D grid
//
// The individual chart elements are designed to be directly accessable;
// however, direct access to the vector is not allowed.


#ifndef CSECTORMAP_H
#define CSECTORMAP_H

#include <vector>
#include <map>

using std::vector;
using std::map;

template <class T>
class CSectorMap
{
private:

vector<T> VecChart;

int iElementCount; //Total numbe of elements

int iWidth; //width of the chart
int iDepth; //Depth of the chart
int iHeighth; //Heighth of the chart

int iXMod; //modifies the index representing the first X-Element
int iYMod; //modifies the index representing the first Y-Element
int iZMod; //modifies the index representing the first Z-Element

public:
CSectorMap();
CSectorMap(int pWidth, int pDepth, int pHeight);
CSectorMap(int pWidth, int pXIdxStart, int pDepth, int pYIdxStart, int pHeighth, int pZIdxStart);

~CSectorMap();

T& at(int x, int y, int z); //acces chart based on 3D idx
T& at(int idx); //For direct access to the vector

int size(); //Return the total number of elements


};

template<class T> CSectorMap<T>::CSectorMap()
{
iElementCount = 0;
iWidth = 0;
iDepth = 0;
iHeighth = 0;

}

template<class T> CSectorMap<T>::CSectorMap(int pWidth, int pDepth, int pHeighth = 0)
{
iElementCount = pWidth * pDepth * pHeighth;
iWidth = pWidth;
iDepth = pDepth;
iHeighth = pHeighth;

VecChart = vector<T>(iElementCount);
}

template<class T> CSectorMap<T>::CSectorMap(int pWidth, int pXIdxStart, int pDepth, int pYIdxStart, int pHeighth = 0, int pZIdxStart = 0)
{
iElementCount = pWidth * pDepth * pHeighth;
iWidth = pWidth;
iDepth = pDepth;
iHeighth = pHeighth;

iXMod = pXIdxStart;
iYMod = pYIdxStart;
iZMod = pZIdxStart;

ecChart = vector<T>(iElementCount);
}

template<class T> CSectorMap<T>::~CSectorMap()
{
}

template<class T> T& CSectorMap<T>::at(int x, int y, int z = 0)
{
return VecChart.at(x + (y*iWidth) + (z*iWidth*iHeighth));
}

template<class T> T& CSectorMap<T>::at(int idx)
{
return VecChart.at(idx);
}

template<class T> int CSectorMap<T>::size()
{
return (int)VecChart.size();
}


#endif



As I mentioned before, I built this for the purpose of storing information in a 3D grid. Because of that, I may rename it to Grid. A few other accessors will be added to obtain the grid width, height, and depth as well.

The main purpose of this is I'm a Games and Simulations Programming student at DeVry, on my final year. I'm an excellent programmer, it's just I haven't done everything, and a lot of my knowledge will only come from years of experience in my future workplace.

But I plan on using this in a simple base simulator, and this storage container will hold information related to each cell. The game will be played in a 2D style, but keeping it 3D will make it easily reusable.

Again, thank y'all for your help, I really appreciate it.

Share this post


Link to post
Share on other sites
Be worth adding const versions of the at method:


T &at(int x,int y,int z);
const T &at(int x,int y,int z) const;


So you can still read-only access if the instance of the SectorMap is constant. Your size() method should also be const for the same reason.

Also, convention would normally be to overload operator() for these operations.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this