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";
}
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";
}