C++: problem with constructor template

Status
Not open for further replies.

nathematics

Ars Scholae Palatinae
984
Edit: The problem has been found; there was an asterisk where there shouldn't have been.

here's a trimmed down version of the class:

Code:
#ifndef FILTER_HPP
#define FILTER_HPP
#include <algorithm>

class filter
{
private:
	double *h, *h_end;
public:
	template<typename It, typename Size>
	filter(It impulse_response, Size filter_size)
		: h(new double[filter_size]), h_end(h + filter_size)
	{
		for(double* hi=h; hi < h + filter_size; ++hi)
		{
			*hi = *impulse_response;
			++impulse_response;
		}
	}

	filter& operator=(const filter& rhs)
	{
		if(rhs.size() != this->size())
		{
			delete[] h;
			h = new double[rhs.size()];
		}
		std::copy(rhs.h, rhs.h_end, h);
		return *this;
	}

	template<typename It, typename size>
	void load_filter(It * impulse_response, size filter_size)
	{
		//allocate if necessary
		if(filter_size != this->size())
		{
			delete[] h;
			h = new double[filter_size];
		}
		//copy data into filter impulse response h
		for(double* hi=h; hi < h + filter_size; ++hi)
		{
			*hi = *impulse_response;
			++impulse_response;
		}
	}

	//returns pointer to begining of impulse reponse
	const double * begin() const
	{
		return h;
	}

	//returns pointer to one past last element of h
	const double * end() const
	{
		return h_end;
	}

	int size() const
	{
		return h_end - h;
	}

	~filter()
	{
		delete[] h;
	}
	
};

#endif


It compiles fine like this:
Code:
double h[] = {5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0};
filter* f = new filter(h, 8);

however when I had code to construct with an iterator class I wrote:

Code:
template <typename T, typename RowIt, typename ColSize>
class vv_row_iterator
{
	RowIt row;
	ColSize col;
public:
	vv_row_iterator(RowIt _row, ColSize _col)
		:row(_row), col(_col)
	{
	}
	vv_row_iterator(const vv_row_iterator& arg)
		: row(arg.row), col(arg.col)
	{
	}

	vv_row_iterator & operator=(const vv_row_iterator& rhs)
	{
		row = rhs.row;
		col = rhs.col;
	}

	T& operator*()
	{
		return (*row)[col];
	}

	vv_row_iterator & operator++()
	{
		++row;
		return *this;
	}
	vv_row_iterator & operator--()
	{
		--row;
		return *this;
	}

	vv_row_iterator operator++(int)
	{
		vv_row_iterator temp = *this;
		++row;
		return temp;
	}
	vv_row_iterator operator--(int)
	{
		vv_row_iterator temp = *this;
		--row;
		return temp;
	}
};

specifically, when I attempted to construct with the first parameter of the filter class constuctor as vv_row_iterator<double, vector<vector<double> >::iterator, long long> as the first parameter of the filter class constuctor, I get an error like

Code:
 error C2661: 'filter::filter: no overloaded function takes 2 arguments
in Visual Studio (2005 Express and 2010 Express)


I should note, that the actual class attempting to be constructed is not filter but a similiar class, however the constuctor is the same except the name change. I have not attempted to compile construction of the simpler filter class using vv_row_iterator yet.

Why won't this compile?
 
You didn't include how you're trying to construct the filter with vv_row_iterator.

When I do the following I get an error that seems similar to what you describe as your problem.
Code:
    double h[] = {5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0};
    vv_row_iterator<double,double*,size_t> v(h,8);
    filter* f = new filter(v, 1);
If that's what you're trying, clang gives a pretty clear explanation of your problem:
Code:
tmp.cpp:130:21: error: no matching constructor for initialization of 'filter'
    filter* f = new filter(v, 1);
                    ^      ~~~~
tmp.cpp:11:5: note: candidate template ignored: failed template argument deduction
    filter(It * impulse_response, Size filter_size)
    ^
tmp.cpp:5:7: note: candidate constructor (the implicit copy constructor) not viable: requires 1
      argument, but 2 were provided
class filter
      ^
1 error generated.
The note that says 'candidate template ignored' makes it clear that it's not even trying your two argument constructor because the parameter that was passed isn't a pointer.

If I fix that by passing a vv_row_iterator pointer to the constructor then I get the error
Code:
tmp.cpp:16:17: error: assigning to 'double' from incompatible type 'vv_row_iterator<double, double
      *, unsigned long>'
            *hi = *impulse_response;
                ^ ~~~~~~~~~~~~~~~~~
tmp.cpp:130:21: note: in instantiation of function template specialization
      'filter::filter<vv_row_iterator<double, double *, unsigned long>, int>' requested here
    filter* f = new filter(&v, 1);
                    ^
1 error generated.
which shows that the code is making some problematic assumptions about the iterator it's given.
 

nathematics

Ars Scholae Palatinae
984
Edit: The problem has been found; there was an improper asterisk before one of the constuctor arguments.


Here is code example of how the classes should work:

Code:
using namespace std;
double h[] = {5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0};
vector<vector<double> > vv;
vv.push_back(vector<double>(h, h + 8));
vv_row_iterator<double, vector<vector<double> >::iterator, long long> vv_it(vv.begin(), 0);
filter* f1 = new filter(h, 8), 
*f2 = new filter(vv_it, 8);
 
Status
Not open for further replies.