Help With STL

I am just a beginner, and have been reading about STL for a few days. Can someone tell me what I am doing wrong actually ? Thanks in advance.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <deque>
#include <algorithm>

using namespace std;

template <class P>
class Print_Elements
{
	public:
		Print_Elements(P _num)
		{
			cout << _num << ' ';
		}
};

template <class T>
class Multiply
{
	private:
		T _num;
	public:
		Multiply<T> (T& _num)
		{
			return _num * _num;  // Error in this class (maybe) here
		}
};

template <class C>
class Upper_Case
{
	private:	
		C _char;
	public:
		Upper_Case<C> (C& _char)
		{
			_char = _char + 'A' - 'a';
			return (_char); // Error in this class (maybe) here
		}
};

int main()
{
	deque<int> deque_int;
	deque<long> deque_long;
	deque<char> deque_char;
	
	for (int i = 1; i <= 9; i++)
		deque_int.push_front(i);
	for (long j = 1000000000; j <= 1000000009; j++)
		deque_long.push_back(j);
	for (char k = 'a'; k <= 'i'; k++)
	{
		deque_char.push_front(k);
		deque_char.push_back(k);
	}
	
	cout << "Elements at the beginning:";
	
	cout << endl;
	cout << "	Deque<Int>: ";
	for_each(deque_int.begin(), deque_int.end(), Print_Elements()); // Error here
	
	cout << endl;
	cout << "	Deque<Long>: ";
	for_each(deque_long.begin(), deque_long.end(), Print_Elements()); // Error here
	
	cout << endl;
	cout << "	Deque<Char>: ";
	for_each(deque_char.begin(), deque_char.end(), Print_Elements()); // Error here
	
	transform(deque_int.begin(), deque_int.end(), Multiply<int>()); // Error here
	transform(deque_long.begin(), deque_long.end(), Multiply<long>()); // Error here
	transform(deque_char.begin(), deque_char.end(), Upper_Case<char>()); // Error here
	
	cout << "Elements after transform:";
	
	cout << endl;
	cout << "	Deque<Int>: ";
	for_each(deque_int.begin(), deque_int.end(), Print_Elements()); // Error here
	
	cout << endl;
	cout << "	Deque<Long>: ";
	for_each(deque_long.begin(), deque_long.end(), Print_Elements()); // Error here
	 
	cout << endl;
	cout << "	Deque<Char>: ";
	for_each(deque_char.begin(), deque_char.end(), Print_Elements()); // Error here
	
	return 0;
}


EDIT:

Errors given:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Compiling source file(s)...
P_005.cpp
P_005.cpp: In constructor `Multiply<T>::Multiply(T&)':
P_005.cpp:25: error: returning a value from a constructor
P_005.cpp: In constructor `Upper_Case<C>::Upper_Case(C&)':
P_005.cpp:38: error: returning a value from a constructor
P_005.cpp: In function `int main()':
P_005.cpp:62: error: missing template arguments before '(' token
P_005.cpp:66: error: missing template arguments before '(' token
P_005.cpp:70: error: missing template arguments before '(' token
P_005.cpp:72: error: no matching function for call to `Multiply<int>::Multiply()'
P_005.cpp:19: note: candidates are: Multiply<int>::Multiply(const Multiply<int>&)
P_005.cpp:24: note:                 Multiply<T>::Multiply(T&) [with T = int]
P_005.cpp:73: error: no matching function for call to `Multiply<long int>::Multiply()'
P_005.cpp:19: note: candidates are: Multiply<long int>::Multiply(const Multiply<long int>&)
P_005.cpp:24: note:                 Multiply<T>::Multiply(T&) [with T = long int]
P_005.cpp:74: error: no matching function for call to `Upper_Case<char>::Upper_Case()'
P_005.cpp:31: note: candidates are: Upper_Case<char>::Upper_Case(const Upper_Case<char>&)
P_005.cpp:36: note:                 Upper_Case<C>::Upper_Case(C&) [with C = char]
P_005.cpp:80: error: missing template arguments before '(' token
P_005.cpp:84: error: missing template arguments before '(' token
P_005.cpp:88: error: missing template arguments before '(' token

P_005.exe - 17 error(s), 0 warning(s)


EDIT 2: I know that I might have done some stupid mistakes, but if anyone has the time and the knowledge, can you please explain it to me, or at least tell me how to make it work, or just guide me to a tutorial, or an example of how it should look like... Please. Thanks in advance.
Last edited on
You don't understand "constructor" and Functor.

1) you should understand what
 
P1 operator()(P1& p1){ return p1; }

do.
This is a method that you should write your "transform" code.
I read about STL (first 120 pages out of 642 from the book "The C++ Standard Library <A Tutorial and Reference> by Nicolai M. Josuttis) and it's true that I got lost when he started to explain functors/templates/using all in one...
Can you please link me to a "beginner" tutorial for these ? I mean a place where classes/templates/algorithms are explained so that anyone could understand. (Sure, if you got time, or ..)
Thanks in advance.
Or, let's take this:

1
2
3
4
5
6
7
8
9
10
11
template <class T>
class Multiply
{
	private:
		T _num;
	public:
		Multiply<T> (T& _num)
		{
			return _num * _num;  // Error in this class (maybe) here
		}
};


What is the problem in here ? The declaration of function Multiply<T> ?
Last edited on
P_005.cpp: In constructor `Multiply<T>::Multiply(T&)':
P_005.cpp:25: error: returning a value from a constructor
A constructor is used to initialize the object when it's created. It doesn't have a return type so you can't return anything from it. What you probably want to do is to assign a value to _num. The parameter has the same name so you will have to write this->_num to refer to the member variable.
this->_num = _num * _num;
You can also use the constructor initialization list.
1
2
3
4
Multiply<T> (T& _num)
:	_num(_num * _num)
{
}
EDIT: This might actually not be at all what you want.

In function `int main()':
P_005.cpp:62: error: missing template arguments before '(' token
You have forgot to specify the template argument and passing an argument to the constructor.for_each(deque_int.begin(), deque_int.end(), Print_Elements<int>(2));But that will not work because Print_Elements has no operator(). What you probably want is to change the constructor to operator().
1
2
3
4
5
6
7
8
9
10
11
12
13
template <class P>
class Print_Elements
{
	public:
		void operator()(P _num)
		{
			cout << _num << ' ';
		}
};

...

for_each(deque_int.begin(), deque_int.end(), Print_Elements<int>());


Last edited on
That's not a function - this is a constructor.

Just Try:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <class T>
class Multiply
{
	public:
	Multiply(){};
	T operator()( const T& num )
	{
		return num * num;
	}
};


int main()
{
	deque<long> deque_long;
	
	for (long j = 10; j <= 19; j++)
		deque_long.push_back(j);
	
	std::transform( deque_long.begin(), deque_long.end(), deque_long.begin(), Multiply<long>() );
	
	return 0;
}

Thanks a lot @Ivan Sidarau, @Peter87. It really helped. I saw the void operator () before, but I thought it wasn't necessary to have it in the code.
Again, thanks a lot. It really helped.
Guys I owe you a lot. With both your explanations I finally understood that :)
You saved me from hours of browsing and reading. Thanks again!
Topic archived. No new replies allowed.