Derived classes from templates

J

John

Hi,

I'm having trouble with the code posted below. When I derive my R3
class from X3<PolicyR3>, I do not seem to be able to access any of the
public functions defined at the X3<PolicyR3> level from an object of
type R3. When I compile, the line

b.Set(t);

give the error "no matching function for call to `R3::Set(double[3])'"

What am I doing wrong?

Thanks,
John


// ******************************************************************
class PolicyR3
{
protected:
~PolicyR3() {}

public:
enum { LENGTH = 3 };
enum { DATA_TYPE = 1 };
};

// ******************************************************************
template <class TypePolicy> class X3 : public TypePolicy
{
protected:
using TypePolicy::LENGTH;
using TypePolicy::DATA_TYPE;
enum { VLENGTH = LENGTH * DATA_TYPE };

public:
double m_v[VLENGTH];

X3() : TypePolicy() {}

~X3() {}

void Set(const double *v)
{ for (int i=0; i<VLENGTH; i++) m_v = v; }

};

class R3 : public X3<PolicyR3>
{
public:

R3() : X3<PolicyR3>() {}

/// Set R3 position i to value x
void Set(int i, double x) {m_v = x;}

};
// ******************************************************************
int main ()
{
R3 b;

double t[3] = {1.,2.,3.};

b.Set(1,2.0);
b.Set(t);

return 0;
}
 
J

joseph cook

Hi,

I'm having trouble with the code posted below.  When I derive my R3
class from X3<PolicyR3>, I do not seem to be able to access any of the
public functions defined at the X3<PolicyR3> level from an object of
type R3.  When I compile, the line

Your problem is unrelated to templates entirely. You cannot have
overloaded functions in two different scopes (one in Base scope, and
one in Derived scope).

In your derived class, you can move these Base functions to your
derived scope, however.

Add:
using X3<PolicyR3>::Set;
to your class R3

HTH,
Joe
 
J

John

Your problem is unrelated to templates entirely. You cannot have
overloaded functions in two different scopes (one in Base scope, and
one in Derived scope).

In your derived class, you can move these Base functions to your
derived scope, however.

Add:
using X3<PolicyR3>::Set;
to your class R3

Thanks for the explanation and solution. I'll go with the "using"
statement since the base class is used by various derived classes and I
don't want to repeat the same code in every derived class.

Thanks,
John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top