Help with template

hello, thanks for reading.
I'm solving an example from my book and I stuck :/
damn compiler says function does not match the argument list ^^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;

template <class polje ,typename int>
polje srednja (polje* niz, int broj) {
	polje sum;
	for(int i = 0; i < broj; i++)
		sum.set (sum.get () + niz [i]);
	polje ave;
	ave.set (sum.get () / broj);
	return ave;
}
//object of this class will be passed into function
class super {
	short* mem;
public:
	super (short m = 0) : mem (new short (m)) {}
	super (const super& ref) : mem (new short (*ref.mem)) {};
	~super () {delete mem;}
	short get () { return *mem; }
	void set (short x) { *mem = x;}
	super operator+ (const super& obj) {return super (*mem + *obj.mem);}
	super operator/ (const super& m) {return super (*mem / *m.mem);}
};

int main () {
	super* test = new super [10];
	super c;
	c = srednja (test, 10);  //here it is. won't compile this line
	return 0;
}


thanks in advice!
Last edited on
1
2
3
4
5
template <class polje ,typename int>
//                     ^^^^^^^^^^^^ what is this int for?
// try just
template <class polje>
// or    <typename polje> 
Last edited on
I've tryed out without typename int then I recive folowing errors:

Error	1	error C2784: 'std::_String_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_iterator<_Elem,_Traits,_Alloc>' from 'super'	c:\users\admin\documents\visual studio 2010\projects\demistificirani c++\testiranje\main.cpp	8	Testiranje	1
Error	2	error C2784: 'std::_String_const_iterator<_Elem,_Traits,_Alloc> std::operator +(_String_const_iterator<_Elem,_Traits,_Alloc>::difference_type,std::_String_const_iterator<_Elem,_Traits,_Alloc>)' : could not deduce template argument for 'std::_String_const_iterator<_Elem,_Traits,_Alloc>' from 'super'	c:\users\admin\documents\visual studio 2010\projects\demistificirani c++\testiranje\main.cpp	8	Testiranje	1
Error	3	error C2784: 'std::_Array_iterator<_Ty,_Size> std::operator +(_Array_iterator<_Ty,_Size>::difference_type,std::_Array_iterator<_Ty,_Size>)' : could not deduce template argument for 'std::_Array_iterator<_Ty,_Size>' from 'super'	c:\users\admin\documents\visual studio 2010\projects\demistificirani c++\testiranje\main.cpp	8	Testiranje	1
Error	4	error C2784: 'std::_Array_const_iterator<_Ty,_Size> std::operator +(_Array_const_iterator<_Ty,_Size>::difference_type,std::_Array_const_iterator<_Ty,_Size>)' : could not deduce template argument for 'std::_Array_const_iterator<_Ty,_Size>' from 'super'	c:\users\admin\documents\visual studio 2010\projects\demistificirani c++\testiranje\main.cpp	8	Testiranje	1
Error	5	error C2784: 'std::reverse_iterator<_RanIt> std::operator +(_Diff,const std::reverse_iterator<_RanIt> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'super'	c:\users\admin\documents\visual studio 2010\projects\demistificirani c++\testiranje\main.cpp	8	Testiranje	1
Error	6	error C2784: 'std::_Revranit<_RanIt,_Base> std::operator +(_Diff,const std::_Revranit<_RanIt,_Base> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'super'	c:\users\admin\documents\visual studio 2010\projects\demistificirani c++\testiranje\main.cpp	8	Testiranje	1
Error	7	error C2677: binary '+' : no global operator found which takes type 'super' (or there is no acceptable conversion)	c:\users\admin\documents\visual studio 2010\projects\demistificirani c++\testiranje\main.cpp	8	Testiranje	1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Line 26
int main () {
	super* test = new super [10];
	super c;
	// c = srednja (test, 10);  //here it is. won't compile this line
	// your passing it a pointer (test is a super array)
	//  so polje = super*, instead of just super like you excpected

	//work with an instance of super, not a pointer	
	c = srednja (test[0], 10);

	//also delete memory
	delete[] test;
	return 0;
}
1
2
3
4
5
6
7
int main () {
	super* test = new super [10];
	super c;
	c = srednja<super, int> (test[3], 10);
	delete[] test;
	return 0;
}


i've done it so and still wont work ?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
using namespace std;

// template <class polje ,typename int>
template <class polje>
polje srednja (polje* niz, int broj) {
	polje sum;
	for(int i = 0; i < broj; i++)
		sum.set (sum.get () + niz [i]);
		//       ^^^^^^^^^^^^^^^^^^^^ here you are trying to add (short + polje)
		// maybe you wanted sum.set( sum.get + niz[i].get() ), OR sum + niz[i]
	polje ave;
	ave.set (sum.get () / broj);
	return ave;
}
//object of this class will be passed into function
class super {
	short* mem;
public:
	super (short m = 0) : mem (new short (m)) {}
	super (const super& ref) : mem (new short (*ref.mem)) {};
	~super () {delete mem;}
	short get () { return *mem; }
	void set (short x) { *mem = x;}
	super operator+ (const super& obj) {return super (*mem + *obj.mem);}
	super operator/ (const super& m) {return super (*mem / *m.mem);}
};

int main () {
	super* test = new super [10];
	super c;
	c = srednja (test, 10);  //here it is. won't compile this line It should be fine now...
	return 0;
}


Ignore my last post, I see now you want to pass the whole array, my mistake.
Your error was on line 9, not in the main function.
OK NOW WHAT'S WRONG IN THIS CODE?

1
2
3
4
5
6
7
8
9
int main () {
	super* test = new super [10];
	super c;
	for(int i = 0; i < 10; i++)
		c = c + test [i];
	c.set (c.get () / 10);  // here I'm trying to put average of objects test [] ( for member mem ) into object c.
	delete [] test;
	return 0;
}


I have excluded template this time and still get error, damn bug makes me crazy.
declaration of class is one post up.
Last edited on
Topic archived. No new replies allowed.