How to define template function within template class in *.inl file
Here's your definition:
template <typename T1, typename T2>
template <typename E>
void SomeClass<T1, T2>::extraTypedMethod(E & e)
{
}
Using template methods inside template classes
Why is it expecting Struct1
Because that is the type it was told to use.
Given the template:
template<typename Type>
class GenericClass
{
private:
Type Identifier;
public:
void setIdentifier(Type Param);
Type getIdentifier();
};
The instantiation with type Struct1
(GenericClass<Struct1>
) generates the following implementation:
// Just for illustration
class GenericClassStruct1
{
private:
Struct1 Identifier;
public:
void setIdentifier(Struct1 Param);
Struct1 getIdentifier();
};
The above class has replaced all occurrences of Type
with Struct1
. Now, it should be easy to see why calling setIdentifier
expects a Struct1
and not an int
.
how do I format it so I can modify the properties in any struct
There is more than one way to do this. What is the "right" way will depend on the constraints of your problem but following example shows one way.
Example
Working Example
#include <iostream>
// Declared but not defined, specializations will provide definitions
template<typename T>
struct GenericTypeTraits;
template<typename T>
class Generic
{
public:
using IdType = typename GenericTypeTraits<T>::IdType;
void setIdentifier(IdType id)
{
mType.mIdentifier = id;
}
IdType getIdentifier() const
{
return mType.mIdentifier;
}
private:
T mType;
};
class Type1
{
public:
int mIdentifier;
};
template<>
struct GenericTypeTraits<Type1>
{
using IdType = int;
};
class Type2
{
public:
std::string mIdentifier;
};
template<>
struct GenericTypeTraits<Type2>
{
using IdType = std::string;
};
int main()
{
Generic<Type1> t1[5];
t1[0].setIdentifier(3);
std::cout << t1[0].getIdentifier() << "\n";
Generic<Type2> t2[5];
t2[0].setIdentifier("3");
std::cout << t2[0].getIdentifier() << "\n";
return 0;
}
Output
3
3
Related Topics
Avoid Warning 'Unreferenced Formal Parameter'
Is a Member of an Rvalue Structure an Rvalue or Lvalue
Difference Between Invalidaterect and Redrawwindow
Purpose of Explicit Default Constructors
The Std::Transform-Like Function That Returns Transformed Container
Virtual Inheritance: Error: No Unique Final Overrider
How to Build Openssl with Mingw in Windows
What Are _Mm_Prefetch() Locality Hints
What's a Good and Stable C++ Tree Implementation
Properties File Library for C (Or C++)
Why Does Enumwindows Return More Windows Than I Expected
Is There Any Reason Not to Make a Member Function Virtual
How to Overload a Function That Can Tell a Fixed Array from a Pointer