i am making a list template for a tutorial, heres the code:

template<typename t> class list {
	int sz;
	t elem;
public:
	list() : sz(0), elem(new t[0]){ }
	list(int i) : sz(i), elem(new t[i]){ }
	list(int* i, double val) : sz(i), elem(new t[i])
	{
		for(int i=0;i>sz;i++) elem[i] = val;
	}

	list operator[](int i){ return elem[i]; }

	int size(void){ return sz; }
};

and when i call it like this:

list<double> temps(5, 5.0);

it gives me this error:
c:\documents and settings\tom\my documents\visual studio 2008\projects\c++ tutorial\c++ tutorial\main.cpp(9) : error C2664: 'list<t>::list(int *,double)' : cannot convert parameter 1 from 'int' to 'int *'
with
[
t=double
]
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

and when i use the C-style cast, it gives me 6 other errors.

any help would be appreciated:)

maybe try:

int a = 5;
list<double> temps(&a, 5.0);

BTW how are you inserting the elements/nodes, your not doing it by the constructor are you?

Your list is incorrect, because you assume that t is a pointer of some type, when it might not be.

Consider your own example when you declare a list with the
double type, then this happens--

class list<double> {
	int sz;
	double elem;
public:
        // default constructor attempts to pre-initialize elem with
        // a pointer to a continguous amount of doubles of size 0
        // immediate type-mismatch
	list() : sz(0), elem(new double[0]){ }

        // same problem here
	list(int i) : sz(i), elem(new double[i]){ }

        // same problem here
	list(int* i, double val) : sz(i), elem(new double[i])
	{
                // if sz is 5, and i is 0, when is 0 > 5? never? Will this execute? Maybe you meant i < sz ?
		for(int i=0;i>sz;i++) elem[i] = val;
	}

        // how are you returning a copy of a list-type from a double?
	list operator[](int i){ return elem[i]; }

	int size(void){ return sz; }
};

-- I also commented in some logic-errors I noticed when attempting to do what the compiler does the moment you declare a list<double>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.