Problem in template class!!

Hi, I have problems on my template class.
I am trying to make bag class that store 10 items by using template.
I have read my data structure text book and followed the instruction..!
But don't know what's wrong with this program!

//header file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

template <class T>
class bag{
public:
	//constructor
	bag(){
		data = new T[10];
		used = 0;
	}

	//member functions
	void insert(T entry);
	void disp();
private:
	T* data;
	int used;
};
#include "temp_imp.template" 


//implementation file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
//#include "temp_header.h"
using namespace std;

template <class T>
void bag<T>::insert(T entry){
	if(used > 10){
		cout<<"Bag is full!"<<endl;
		return;
	}
	else{
		data[used] = entry;
		used++;
	}
}

template <class T>
void bag<T>::disp(){
	for(int i = 0; i <used; i+){
		cout<<data[i]<<endl;
	}
}


//errors I got..
error C2143: syntax error : missing ';' before '<'
error C2182: 'bag' : illegal use of type 'void'
error C2988: unrecognizable template declaration/definition
error C2059: syntax error : '<'
error C2039: 'insert' : is not a member of '`global namespace''
error C2039: 'disp' : is not a member of '`global namespace''
error C2143: syntax error : missing ';' before '{'
error C2447: '{' : missing function header (old-style formal list?)


T.,T;;;;;;;
This template is so confusing me!!
I've compiled this with g++. In line 19 of your .template file, you use i+ instead of i++. That's a problem.

Otherwise, the code is fine. the #include " .template" is a little strange. I've seen .inl (for inline) before, but not .template. I wonder if that's a problem for Visual Studio.

Also you have it commented, out but delete that line where you include the header in the template file. That'll cause some major problems if you don't guard your files (with #pragma once).
Still not working..;;
I named the implementation file as .template because that is what my text book has...
Did not work so I changed the code little...I changed the implementation file as .cpp file and included the header file..!!
1
2
#include <iostream>
#include "temp_header.h" 

The errors I got is these!!
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall bag<int>::insert(int)" (?insert@?$bag@H@@QAEXH@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall bag<int>::disp(void)" (?disp@?$bag@H@@QAEXXZ) referenced in function _main

closed account (zb0S216C)
Template definitions cannot be separated from the declarations. So what you have to do is "#include" your implementation file after the class definition, like so:

.inl file:

1
2
3
4
5
6
7
8
9
10
11
template< typename T >
void Bag< T >::insert( T entry )
{
  // ...
}

template< typename T >
void Bag< T >::disp( )
{
  // ...
}

[Note: .inl files don't have header guards because they are meant to only hold definitions, not interfaces]

Header file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#if !defined( __HEADER__ )
#define __HEADER__

template< typename T >
class Bag
{
  public:
    // ...
    void instert( T entry );
    void disp( );
};

#include <ImplementationFile.inl>
#endif 

The compiler will expand the "ImplementationFile.inl" after the class definition which will bring the member-function definitions into same file scope as the class -- this will allow the linker to see the template member-function definitions.

Wazzak
Last edited on
Did you instruct your compiler/IDE to compile "temp_imp.template"?

Besides this your code would fail at line 7 of the implementation file:
if(used > 10){ should be if(used >= 10){
> Did you instruct your compiler/IDE to compile "temp_imp.template"?
To clarify, you shouldn't.
The program works after I combined the header file and implementation file as Framework suggested. I don't know why my textbook wrote about .template file.. The .inl file works perfectly..Thanks a lot guys!!
Last edited on
The name is irrelevant.

> Note: .inl files don't have header guards because they are meant to only hold definitions, not interfaces
The purpose of headers guards is to prevent multiple definitions

class foo{}; is a class definition.
Last edited on
The usual way to handle template implementations is like you've done. Often the implementation file is suffixed with ".tcc" or ".tpp". If your file splitting didn't compile, there may be some other error which you currently haven't detected. My idea is you've erroneously instructed your IDE to compile the implementation file.
closed account (zb0S216C)
@ne555: I know what header guards are for. All I was saying was that in-line files don't have them because they don't need them. And I know know the difference between a forward class declaration and a class definition. So please, refrain from posting useless replies that don't help the OP. Thanks.

Wazzak
> All I was saying was that in-line files don't have them because they don't need them.
But your reason was wrong.
They don't need header guards as long as there is no other file (apart from the correspondent .h) that include them.
closed account (zb0S216C)
Well, obviously. In-line files are "#included" by headers anyway -- that's why they're called in-line files.

Wazzak
Topic archived. No new replies allowed.