Jump to content
  • Advertisement
Sign in to follow this  
Zmurf

Templates, Classes, Structs Problems

This topic is 4623 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having problems using templates with classes and structs.
template <class T> class MyClass
{
public:
template <class U> struct MyStruct
{
val1;
val2;
};

vector <T> m_Vector;

void addtovector(T v1, T v2);
};

MyClass<class T>::addtovector(T v1, T v2){
MyStruct<T> temp = {v1, v2};
m_Vector.push_back(temp)
}
How can I get something like this to work?

Share this post


Link to post
Share on other sites
Advertisement

template <class T> class MyClass
{
public:

template <class U> struct MyStruct
{
U val1;
U val2;
};

vector <MyStruct <T> > m_Vector;

void addtovector(T v1, T v2)
{
MyStruct<T> temp = {v1, v2};
m_Vector.push_back(temp)
}
};


Share this post


Link to post
Share on other sites
Also, you should change the 'class' template parameter type to 'typename', so that it works with simple types like ints, floats, etc...

Share this post


Link to post
Share on other sites
Quote:
Original post by Eddycharly
Also, you should change the 'class' template parameter type to 'typename', so that it works with simple types like ints, floats, etc...

Quote:
C++ Standard, Final Draft, Section 14.1, Paragraph 1
There is no semantic difference between class and typename in a template-parameter.

Enigma

Share this post


Link to post
Share on other sites
Still getting errors.

error C2079: 'MyClass<T>::MyStruct<U>::val1' uses undefined class 'T'

Share this post


Link to post
Share on other sites

#include <utility> // std::pair
#include <vector>

template < typename Tp >
struct foo {

typedef std::pair<Tp, Tp> identical_pair;
typedef std::vector<identical_pair> vec_type;

private:

vec_type m_Vector;

public:

void addtovector(const Tp& v1, const Tp& v2) {
m_Vector.push_back(identical_pair(v1, v2));
}
};

Share this post


Link to post
Share on other sites
Quote:
Original post by Zmurf
Still getting errors.

error C2079: 'MyClass<T>::MyStruct<U>::val1' uses undefined class 'T'


...

Eddycharly allready got it. Adding the include and "using namespace std;", as well as adding a semicolon after the push_back, and it compiles.

#include <vector>
using namespace std;

template <class T> class MyClass
{
public:

template <class U> struct MyStruct
{
U val1;
U val2;
};

vector <MyStruct <T> > m_Vector;

void addtovector(T v1, T v2)
{
MyStruct<T> temp = {v1, v2};
m_Vector.push_back(temp);
}
};





It works. Of course, if you're still having problems, things that could really help are:

1) Whatever you're actually trying to compile, not something obviously wrong in twenty bazillion manners (missing types, includes, using declerations, ...) nor the code that, compiling fine, you obviously arn't using.

2) Not psuedo-typed out, %#^*(&ing copy and pasted. CTRL+C and CTRL+V. That way I feel less like I'm trying to navigate paris with a new york subway map. I get a copy and can see everything from the typeo in line 2347589235 of module 13, to the fact that you're trying to call eat( steel_rebarb & ). It's customary to try to simplify the problem to see what exactly is triggering it of course, but it's not going to help if you inadvertantly eliminated all traces of the problem. The way to prevent this is to actually try compiling the simplification to make sure it still illustrates the problem.

3) The line the actual error occurs on. This is given to you by your compiler for a reason: to point out where the problem is occuring. Also, all errors - many often occur as a chain reaction of one mistake, and it's not terribly uncommon for them to cause problems twenty bazillion lines away.

Share this post


Link to post
Share on other sites
Quote:

C++ Standard, Final Draft, Section 14.1, Paragraph 1
There is no semantic difference between class and typename in a template-parameter.


That's strange, i wonder why we have two keywords for the same thing then...
Annyway, thanks for pointing it out.

Share this post


Link to post
Share on other sites
Quote:
Original post by Eddycharly
Quote:

C++ Standard, Final Draft, Section 14.1, Paragraph 1
There is no semantic difference between class and typename in a template-parameter.


That's strange, i wonder why we have two keywords for the same thing then...
Annyway, thanks for pointing it out.

For historical reasons, mostly. typename is a relatively recent addition to the language. Note that when introducing template template parameters, class is the only valid qualifier.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!