Indirect recursion and templates?

S

Steven T. Hatton

Is there a way to forward declare a template? I've been trying to resolve a
problem resulting from a recursive templet reference. The problem comes
about when I attempt to #include a file which itself has a #include of a
file with a function defined in it. I've simplified the code to the point
that the reason for arranging things as I have is completely lost, as is
all the functionality. I started out with this:
http://www.research.att.com/~bs/matrix.c

I decided to try to refactor it into something I could use in my own
program. I can get it to work as long as I leave everyting in headers. If
I were dealing with regular classes rather than templates, I would probably
be able to use forward references and break the loop. I can't figure out a
way to do that with templates. Any suggestions?

Trying to compile the following code produces this result:

g++ -o slicer matrix.cpp slicer.cpp
/tmp/cc2iMUDS.o(.text+0x0): In function `mul(Cslice_iter<double> const&)':
: multiple definition of `mul(Cslice_iter<double> const&)'
/tmp/ccV2bc3q.o(.text+0x0): first defined here
collect2: ld returned 1 exit status


/*************
cslice_iter.h
**************/
#ifndef CSLICE_ITER_H
#define CSLICE_ITER_H

template<class T>
class Cslice_iter
{ };

double mul(const Cslice_iter<double>& v1)
{
double res = 0;
return res;
}
#endif


/*************
matrix.cpp
**************/
#include "matrix.h"
valarray<double> operator*(const Matrix& m, const valarray<double>& v)
{
valarray<double> res(m.dim1());
for (size_t i = 0; i<m.dim1(); i++)
mul(m.row(i));
return res;
}


/*************
matrix.h
**************/
#ifndef MATRIX_H
#define MATRIX_H

#include "cslice_iter.h"
#include <valarray>

using std::valarray;

class Matrix
{
size_t d1;

public:
Matrix(const int& d1_0):d1(d1_0)
{}

~Matrix(){}

Cslice_iter<double> row(size_t i) const{return Cslice_iter<double>();}
size_t dim1() const
{
return d1;
}

};
/*
valarray<double> operator*(const Matrix& m, const valarray<double>& v)
{
valarray<double> res(m.dim1());
for (size_t i = 0; i<m.dim1(); i++)
mul(m.row(i));
return res;
}
*/
#endif

/*************
main.cpp
**************/
#include "matrix.h"
#include "cslice_iter.h"
#include<iostream>

int main()
{
std::cout<<"meaningless output\n";
}
 
J

John Harrison

Steven T. Hatton said:
Is there a way to forward declare a template? I've been trying to resolve a
problem resulting from a recursive templet reference. The problem comes
about when I attempt to #include a file which itself has a #include of a
file with a function defined in it. I've simplified the code to the point
that the reason for arranging things as I have is completely lost, as is
all the functionality. I started out with this:
http://www.research.att.com/~bs/matrix.c

I decided to try to refactor it into something I could use in my own
program. I can get it to work as long as I leave everyting in headers. If
I were dealing with regular classes rather than templates, I would probably
be able to use forward references and break the loop. I can't figure out a
way to do that with templates. Any suggestions?

I'm not exactly sure what you're asking. The problem with the posted code is
easily solved by making the function inline, or by moving the function to a
source file.

OTOH you can forward declare templates in the obvious way.

// forward declare a template class
template <class T>
class X;

or

// forward declare a template function
template <class T>
void func(const T& x);

hth
john
 
S

Steven T. Hatton

John said:
I'm not exactly sure what you're asking. The problem with the posted code
is easily solved by making the function inline, or by moving the function
to a source file.

When I put the function mul() in a source file it wouldn't compile. I don't
recall the details. I now have something that works, it it might be
difficult to reproduce the problem at this point.
OTOH you can forward declare templates in the obvious way.

// forward declare a template class
template <class T>
class X;

Yes. That is what I was looking for. Thanks.
 

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

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top