Modular programming with Template

Hi, i'm trying to create a stack of generic variable with template.
This is the code.

BlockingStack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include "stdafx.h"
 
#pragma once



template <class T>
class BlockingStack
{
private:
 std::stack<T> stack_t;
 int stack_actual_dimension;
 int stack_max_dimension;
 
 
public:
	BlockingStack(int stack_max_dimension);
	~BlockingStack(void);
	bool get(T &t);
	void push(T t);

};


BlockingStack.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include "BlockingStack.h"
 


template<class T> 
BlockingStack<T>::BlockingStack (int stack_max_dimension):stack_max_dimension(stack_max_dimension),stack_actual_dimension(0){}


template<class T> 
BlockingStack<T>::~BlockingStack(void)
{
}

 template<class T> 
bool BlockingStack<T>::get(T &t){
*t=stack_t.top();
return false;
}

template<class T>  
void BlockingStack<T>::push(T t){
	stack_t.push(t);
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "stdafx.h"
#include "BlockingStack.h"


int _tmain(int argc, _TCHAR* argv[])
{

	BlockingStack<int> stack_wstring(10);
	 

 
  stack_wstring.push(5);
  stack_wstring.push(4);
  stack_wstring.push(3);

  int s;

  stack_wstring.get(s);

  std::cout << "mystack.top() is now " << s << '\n';
 
  
	return 0;
}



i don't understand why ,but it dont work...
It produce this errors:
1
2
3
4
5
6
1>Blocking_stack.obj : error LNK2019: riferimento al simbolo esterno "public: __thiscall BlockingStack<int>::BlockingStack<int>(int)" (??0?$BlockingStack@H@@QAE@H@Z) non risolto nella funzione _wmain
1>Blocking_stack.obj : error LNK2019: riferimento al simbolo esterno "public: __thiscall BlockingStack<int>::~BlockingStack<int>(void)" (??1?$BlockingStack@H@@QAE@XZ) non risolto nella funzione _wmain
1>Blocking_stack.obj : error LNK2019: riferimento al simbolo esterno "public: bool __thiscall BlockingStack<int>::get(int &)" (?get@?$BlockingStack@H@@QAE_NAAH@Z) non risolto nella funzione _wmain
1>Blocking_stack.obj : error LNK2019: riferimento al simbolo esterno "public: void __thiscall BlockingStack<int>::push(int)" (?push@?$BlockingStack@H@@QAEXH@Z) non risolto nella funzione _wmain
1>c:\users\francesco\documents\visual studio 2012\Projects\Blocking_stack\Debug\Blocking_stack.exe : fatal error LNK1120: 4 extern link not solved
========== Compilazione: 0 completate, 1 non riuscite, 0 aggiornate, 0 ignorate ==========


Thanks in advamce
Last edited on
The code that you've put in BlockingStack.cpp has to be in BlockingStack.h
Why i can't divide them?
Because each .cpp file is compiled separately, with no knowledge of other .cpp files.

When compiling the .cpp file with _tmain in it, the compiler doesn't see the definitions of BlockingStack<T> if they are not in the include file, so it compiles calls to those member functions, assuming they are defined somewhere else.

When compiling BlockingStack.cpp, the compiler doesn't see anything using BlockingStack<int> or any other BlockingStack<T>, so nothing is compiled.

When linking the results together, the linker finds calls to BlockingStack<int>::BlockingStack<int>(int) and others, which lead nowhere, and reports that.
Last edited on
Ok,..
you have been very clear! :) Thanks...
Topic archived. No new replies allowed.