By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,121 Members | 1,093 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,121 IT Pros & Developers. It's quick & easy.

templates

P: 30
hy

i wrote a list class , i tested it for int values but now i want to make it a templet

but somting is not wright with the function declarations
i got this erorr
error LNK2019: unresolved external symbol "public: int __thiscall vector_eu<int>::erace(int,int)" (?erace@?$vector_eu@H@@QAEHHH@Z) referenced in function _wmain test vector eu.obj

here ar the declarations in define element.h



#include <iostream>

using namespace std;

template <typename tip>
class element
{public:
tip val;
int initializat; /// arata daca un element e initializat sau nu
element<tip>* point_front;
element<tip>* point_back;

};


template <typename tip>
class vector_eu
{
public:
vector_eu(); //constructor
int push_bak(tip);//adauga un element in coada vectorului push_bak(element)
int insert(tip,int);//adauga un element intr-un loc specificat insert(element,pozitie)
int erace(int);//sterge un element dintr-o pozitie specificata erace(pozitie)
int erace(int,int);//sterge un nr de caractere incepand cu o pozitie specificata erace(pozitie,nr_de_elemente)
int clear(void); //sterge tot vectorul
int get(int);

private:
element<tip>* primul_element;
int nr_de_elemente;
element<tip>* ultimul_element;

element<tip>* get_addres(int);


};






and here is the the implementations


#include "stdafx.h"
#include <iostream>
#include <string>
#include "define element.h"
using namespace std;

void error(string s)
{
cout<<s<<endl;
}


template <typename tip>
inline
vector_eu<tip>::vector_eu()
{
this->nr_de_elemente=0;
this->primul_element=new element<tip>; // creaza primul element vid
this->primul_element->initializat=0;
this->primul_element->point_back=0; // pointerul spre elemntul precedent pt primul elemnt este 0
this->primul_element->point_front=0;// daca pointerul spre urmatorul element este 0 --> nu exita
this->ultimul_element=this->primul_element;
}



template <typename tip>
inline
int vector_eu<tip>::push_bak(tip val)
{
if((this->nr_de_elemente)==0)
{
(this->primul_element)->initializat=1;
(this->primul_element)->val=val;
++(this->nr_de_elemente);
return 1;
}
else
{
element * temp=new element;
temp->initializat=1;
temp->val=val;
temp->point_front=0;
temp->point_back=(this->ultimul_element);
(this->ultimul_element)->point_front=temp;
(this->ultimul_element)=temp;
++(this->nr_de_elemente);
return 1;
}
}



template <typename tip>
inline
element<tip>* vector_eu<tip>::get_addres(int n) /// iterator incepe de la 0
{
if (this->nr_de_elemente==0)
{
error("nu exista elemente");
return 0;
}
else
{ if(n<this->nr_de_elemente)
{
if(n==0) return this->primul_element;
else
{
if(n<0){ error("iterator<0");return 0;}
else
{
element*temp=this->primul_element;
while(n>0)
{
temp=temp->point_front;
--n;
}
return temp;
}
}
}
else
{
error("iterator>nr_de_elemente");
}
}


}









template <typename tip>
inline
int vector_eu<tip>::get(int n)
{
if(this->nr_de_elemente>n)
{
if(n>-1)
{
element * temp =get_addres(n);
return temp->val;
}
else {
error("iterator<0");
return 0;
}
}
else {
error("iterator>nr_de_elemente");
return 0;
}
}




template <typename tip>
inline
int vector_eu<tip>::clear(void)
{
if(this->nr_de_elemente!=0)
{
if(this->nr_de_elemente>0)
{
element* temp=this->ultimul_element;
while(this->nr_de_elemente>1)
{
temp=temp->point_back;
delete (temp->point_front);
--(this->nr_de_elemente);
}
temp->point_front=0;
temp->initializat=0;
temp->val=0;
(this->ultimul_element)=(this->primul_element);
(this->nr_de_elemente)=0;
return 1;
}
else {error("nr_de_elemente<0");return 0;}
}
else
return 1;
}



template <typename tip>
inline
int vector_eu<tip>::erace(int n)
{
if(n<this->nr_de_elemente)
{
if(n>-1)
{
if(n==0)
{
if(this->nr_de_elemente==1)
{
this->clear();
}
else
{
element *temp=this->primul_element;
this->primul_element=(this->primul_element)->point_front;
(this->primul_element)->point_back=0;
delete temp;
--(this->nr_de_elemente);
return 1;
}
}
else{
if((n+1)==this->nr_de_elemente)
{
element * temp=this->ultimul_element;
((this->ultimul_element)->point_back)->point_front=0;
(this->ultimul_element)=(this->ultimul_element)->point_back;
delete temp;
--(this->nr_de_elemente);
return 1;
}
else
{
element * temp=get_addres(n);
(temp->point_back)->point_front=temp->point_front;
(temp->point_front)->point_back=temp->point_back;
delete temp;
--(this->nr_de_elemente);
return 1;
}
}
}
else error("iterator<0");

}
else error("iterator>nr_de_elemente");

return 0;
}



template <typename tip>
inline
int vector_eu<tip>::erace(int n, int r) ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
if(n<0)
{
error("iterator poz<0");
return 0;
}
else
if(r<0)
{
error("iterator lungime<0");
return 0;
}
else
if(r==0)
{
error("iterator lungime = 0");
}
else
if((n+r)>(this->nr_de_elemente))
{
error("depaseste dimensiunea");
return 0;
}
else
{
if(r==1)
{
erace(n);
return 1;
}
else
if(n==0)
{
if(r==(this->nr_de_elemente))
{
this->clear();
return 1;
}
else
{
element * temp=(this->primul_element);
for(int i=0;i<r;++i)
{
temp=(temp->point_front);
delete (temp->point_back);

}
(this->primul_element)=temp;
(this->primul_element)->point_back=0;
this->nr_de_elemente-=r;
return 1;
}
}
else
{
if((n+r)==(this->nr_de_elemente))
{
element *temp=get_addres(n);
(this->ultimul_element)=temp->point_back;
(this->ultimul_element)->point_front=0;
for(int i=1;i<r;++i)
{
temp=temp->point_front;
delete temp->point_back;
}
delete temp;
this->nr_de_elemente-=r;
return 1;
}
else
{
element *temp=get_addres(n);
element *temp_2=(get_addres(n-1));
element *temp_3=get_addres(n+r);
temp_2->point_front=temp_3;
temp_3->point_back=temp_2;
for(int i=1;i<r;++i)
{
temp=temp->point_front;
delete temp->point_back;
}
delete temp;
this->nr_de_elemente-=r;
return 1;
}

}
}
}


template <typename tip>
inline
int vector_eu<tip>::insert(tip val, int n)
{
if((n==0)&&(this->nr_de_elemente==0))
this->push_bak(val);
else
if(n>this->nr_de_elemente)
{
error("iterator>nr_de_elemente");
return 0;
}
else
if(n<0)
{
error("iterator<0");
return 0;
}
else
if(n==this->nr_de_elemente)
{
this->push_bak(val);

return 1;
}
else
if(n==0)
{
element * temp=new element;
temp->initializat=1;
temp->point_back=0;
temp->point_front=this->primul_element;
(temp->val)=val;
(this->primul_element)->point_back=temp;
this->primul_element=temp;
++(this->nr_de_elemente);
return 1;
}
else
{
element * temp=get_addres(n-1);
element * new_element= new element;

new_element->initializat=1;
new_element->val=val;

new_element->point_front=temp->point_front;
(temp->point_front)->point_back=new_element;
temp->point_front=new_element;
new_element->point_back=temp;
++(this->nr_de_elemente);
return 1;

}

}




please help
Dec 26 '06 #1
Share this Question
Share on Google+
10 Replies


Expert 100+
P: 1,510
a few errors (mainly missing <tip>) - now compiles
Expand|Select|Wrap|Line Numbers
  1. //#include "stdafx.h"  // ** remove
  2. #include <iostream>
  3. #include <string>
  4. #include "element.h"  // ** removed define??
  5. using namespace std;
  6.  
  7. void error(string s)
  8. {
  9. cout<<s<<endl;
  10. }
  11.  
  12.  
  13. template <typename tip>
  14. inline
  15. vector_eu<tip>::vector_eu()
  16. {
  17. this->nr_de_elemente=0;
  18. this->primul_element=new element<tip>; // creaza primul element vid
  19. this->primul_element->initializat=0;
  20. this->primul_element->point_back=0; // pointerul spre elemntul precedent pt primul elemnt este 0
  21. this->primul_element->point_front=0;// daca pointerul spre urmatorul element este 0 --> nu exita
  22. this->ultimul_element=this->primul_element;
  23. }
  24.  
  25.  
  26.  
  27. template <typename tip>
  28. inline
  29. int vector_eu<tip>::push_bak(tip val)   // ** added :p
  30. {
  31. if((this->nr_de_elemente)==0)
  32. {
  33. (this->primul_element)->initializat=1;
  34. (this->primul_element)->val=val;
  35. ++(this->nr_de_elemente);
  36. return 1;
  37. }
  38. else
  39. {
  40. element<tip> * temp=new element<tip>();  // ** added <tip>
  41. temp->initializat=1;
  42. temp->val=val;
  43. temp->point_front=0;
  44. temp->point_back=(this->ultimul_element);
  45. (this->ultimul_element)->point_front=temp;
  46. (this->ultimul_element)=temp;
  47. ++(this->nr_de_elemente);
  48. return 1;
  49. }
  50. }
  51.  
  52.  
  53.  
  54. template <typename tip>
  55. inline
  56. element<tip>* vector_eu<tip>::get_addres(int n) /// iterator incepe de la 0
  57. {
  58. if (this->nr_de_elemente==0)
  59. {
  60. error("nu exista elemente");
  61. return 0;
  62. }
  63. else
  64. { if(n<this->nr_de_elemente)
  65. {
  66. if(n==0) return this->primul_element;
  67. else
  68. {
  69. if(n<0){ error("iterator<0");return 0;}
  70. else
  71. {
  72. element<tip> *temp=this->primul_element;  // ** added <tip>
  73. while(n>0)
  74. {
  75. temp=temp->point_front;
  76. --n;
  77. }
  78. return temp;
  79. }
  80. }
  81. }
  82. else
  83. {
  84. error("iterator>nr_de_elemente");
  85. }
  86. }
  87.  
  88.  
  89. }
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99. template <typename tip>
  100. inline
  101. int vector_eu<tip>::get(int n)
  102. {
  103. if(this->nr_de_elemente>n)
  104. {
  105. if(n>-1)
  106. {
  107. element<tip> * temp =get_addres(n);  // ** addedd <tip>
  108. return temp->val;
  109. }
  110. else {
  111. error("iterator<0");
  112. return 0;
  113. }
  114. }
  115. else {
  116. error("iterator>nr_de_elemente");
  117. return 0;
  118. }
  119. }
  120.  
  121.  
  122.  
  123.  
  124. template <typename tip>
  125. inline
  126. int vector_eu<tip>::clear(void)
  127. {
  128. if(this->nr_de_elemente!=0)
  129. {
  130. if(this->nr_de_elemente>0)
  131. {
  132. element<tip> * temp=this->ultimul_element;  // ** added <tip>
  133. while(this->nr_de_elemente>1)
  134. {
  135. temp=temp->point_back;
  136. delete (temp->point_front);
  137. --(this->nr_de_elemente);
  138. }
  139. temp->point_front=0;
  140. temp->initializat=0;
  141. temp->val=0;
  142. (this->ultimul_element)=(this->primul_element);
  143. (this->nr_de_elemente)=0;
  144. return 1;
  145. }
  146. else {error("nr_de_elemente<0");return 0;}
  147. }
  148. else
  149. return 1;
  150. }
  151.  
  152.  
  153.  
  154. template <typename tip>
  155. inline
  156. int vector_eu<tip>::erace(int n)
  157. {
  158. if(n<this->nr_de_elemente)
  159. {
  160. if(n>-1)
  161. {
  162. if(n==0)
  163. {
  164. if(this->nr_de_elemente==1)
  165. {
  166. this->clear();
  167. }
  168. else
  169. {
  170. element<tip> *temp=this->primul_element;  // ** addedd <tip>
  171. this->primul_element=(this->primul_element)->point_front;
  172. (this->primul_element)->point_back=0;
  173. delete temp;
  174. --(this->nr_de_elemente);
  175. return 1;
  176. }
  177. }
  178. else{
  179. if((n+1)==this->nr_de_elemente)
  180. {
  181. element<tip> * temp=this->ultimul_element;  // ** addedd <tip>
  182. ((this->ultimul_element)->point_back)->point_front=0;
  183. (this->ultimul_element)=(this->ultimul_element)->point_back;
  184. delete temp;
  185. --(this->nr_de_elemente);
  186. return 1;
  187. }
  188. else
  189. {
  190. element<tip> * temp=get_addres(n);  // ** addedd <tip>
  191. (temp->point_back)->point_front=temp->point_front;
  192. (temp->point_front)->point_back=temp->point_back;
  193. delete temp;
  194. --(this->nr_de_elemente);
  195. return 1;
  196. }
  197. }
  198. }
  199. else error("iterator<0");
  200.  
  201. }
  202. else error("iterator>nr_de_elemente");
  203.  
  204. return 0;
  205. }
  206.  
  207.  
  208.  
  209. template <typename tip>
  210. inline
  211. int vector_eu<tip>::erace(int n, int r) ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  212. {
  213. if(n<0)
  214. {
  215. error("iterator poz<0");
  216. return 0;
  217. }
  218. else
  219. if(r<0)
  220. {
  221. error("iterator lungime<0");
  222. return 0;
  223. }
  224. else
  225. if(r==0)
  226. {
  227. error("iterator lungime = 0");
  228. }
  229. else
  230. if((n+r)>(this->nr_de_elemente))
  231. {
  232. error("depaseste dimensiunea");
  233. return 0;
  234. }
  235. else
  236. {
  237. if(r==1)
  238. {
  239. erace(n);
  240. return 1;
  241. }
  242. else
  243. if(n==0)
  244. {
  245. if(r==(this->nr_de_elemente))
  246. {
  247. this->clear();
  248. return 1;
  249. }
  250. else
  251. {
  252. element <tip> * temp=(this->primul_element);  // ** addedd <tip>
  253. for(int i=0;i<r;++i)
  254. {
  255. temp=(temp->point_front);
  256. delete (temp->point_back);
  257.  
  258. }
  259. (this->primul_element)=temp;
  260. (this->primul_element)->point_back=0;
  261. this->nr_de_elemente-=r;
  262. return 1;
  263. }
  264. }
  265. else
  266. {
  267. if((n+r)==(this->nr_de_elemente))
  268. {
  269. element <tip> *temp=get_addres(n);  // ** addedd <tip>
  270. (this->ultimul_element)=temp->point_back;
  271. (this->ultimul_element)->point_front=0;
  272. for(int i=1;i<r;++i)
  273. {
  274. temp=temp->point_front;
  275. delete temp->point_back;
  276. }
  277. delete temp;
  278. this->nr_de_elemente-=r;
  279. return 1;
  280. }
  281. else
  282. {
  283. element<tip>*temp=get_addres(n);  // ** addedd <tip>
  284. element <tip> *temp_2=(get_addres(n-1));  // ** addedd <tip>
  285. element<tip>  *temp_3=get_addres(n+r);  // ** addedd <tip>
  286. temp_2->point_front=temp_3;
  287. temp_3->point_back=temp_2;
  288. for(int i=1;i<r;++i)
  289. {
  290. temp=temp->point_front;
  291. delete temp->point_back;
  292. }
  293. delete temp;
  294. this->nr_de_elemente-=r;
  295. return 1;
  296. }
  297.  
  298. }
  299. }
  300. }
  301.  
  302.  
  303. template <typename tip>
  304. inline
  305. int vector_eu<tip>::insert(tip val, int n)
  306. {
  307. if((n==0)&&(this->nr_de_elemente==0))
  308. this->push_bak(val);
  309. else
  310. if(n>this->nr_de_elemente)
  311. {
  312. error("iterator>nr_de_elemente");
  313. return 0;
  314. }
  315. else
  316. if(n<0)
  317. {
  318. error("iterator<0");
  319. return 0;
  320. }
  321. else
  322. if(n==this->nr_de_elemente)
  323. {
  324. this->push_bak(val);
  325.  
  326. return 1;
  327. }
  328. else
  329. if(n==0)
  330. {
  331. element<tip> * temp=new element<tip>();  // ** addedd <tip>
  332. temp->initializat=1;
  333. temp->point_back=0;
  334. temp->point_front=this->primul_element;
  335. (temp->val)=val;
  336. (this->primul_element)->point_back=temp;
  337. this->primul_element=temp;
  338. ++(this->nr_de_elemente);
  339. return 1;
  340. }
  341. else
  342. {
  343. element<tip> * temp=get_addres(n-1);  // ** addedd <tip>
  344. element<tip> * new_element= new element<tip>();  // ** addedd <tip>
  345.  
  346. new_element->initializat=1;
  347. new_element->val=val;
  348.  
  349. new_element->point_front=temp->point_front;
  350. (temp->point_front)->point_back=new_element;
  351. temp->point_front=new_element;
  352. new_element->point_back=temp;
  353. ++(this->nr_de_elemente);
  354. return 1;
  355.  
  356. }
  357.  
  358. }
  359.  
Dec 26 '06 #2

P: 30
thanks!!! I forgot to add the parameter list when i convert it to a template :D

that was one error ( more errors but basicaly the same one) , and my compiler din't find it .
I' m using MS visual studio 2005 ( on xp pro sp 2)
i tried to compile it after i added the parameter lits but it didn't work.
I steel get:

Error 1 error LNK2019: unresolved external symbol "public: int __thiscall vector_eu<int>::erace(int,int)" (?erace@?$vector_eu@H@@QAEHHH@Z) referenced in function _wmain test vector eu.obj

Error 2 error LNK2019: unresolved external symbol "public: int __thiscall vector_eu<int>::erace(int)" (?erace@?$vector_eu@H@@QAEHH@Z) referenced in function _wmain test vector eu.obj

............. for all the functions


tis happends when i declear a function but don't write somewhere its boddy
and i use it for ex :

int max(int,int);
///// i don't write its boddy
but then i use it
c=max(a,b);

and now i surly get an error LNK2019: unresolved external symbol


so can anybody tel me wats rong in my program? din't i wrote the function's name corectly when i wrote there boddy?
Dec 26 '06 #3

P: 30
i realy need this so PLS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Dec 27 '06 #4

Expert 100+
P: 1,510
i realy need this so PLS!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
could you post the complete code which is giving you errors? don't forget the code tags!
Dec 27 '06 #5

P: 30
here is all the info

i'm using microsoft visual c++ 2005 on xp sp 2
i tested it with int tipe ( without templates ) and it worked fine




here are the header files

" stdafx.h"

#pragma once


#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <stdio.h>
#include <tchar.h>



" define element.h"

#include <iostream>

using namespace std;

template <typename tip>
class element
{public:
tip val;
int initializat; /// arata daca un element e initializat sau nu
element<tip>* point_front;
element<tip>* point_back;

};


template <typename tip>
class vector_eu
{
public:
vector_eu(); //constructor
int push_bak(tip);//adauga un element in coada vectorului push_bak(element)
int insert(tip,int);//adauga un element intr-un loc specificat insert(element,pozitie)
int erace(int);//sterge un element dintr-o pozitie specificata erace(pozitie)
int erace(int,int);//sterge un nr de caractere incepand cu o pozitie specificata erace(pozitie,nr_de_elemente)
int clear(void); //sterge tot vectorul
int get(int);

private:
element<tip>* primul_element;
int nr_de_elemente;
element<tip>* ultimul_element;

element<tip>* get_addres(int);


};





" implement.cpp"

#include "stdafx.h"
#include <iostream>
#include <string>
#include "define element.h"
using namespace std;

void error(string s)
{
cout<<s<<endl;
}


template <typename tip>
inline
vector_eu<tip>::vector_eu()
{
this->nr_de_elemente=0;
this->primul_element=new element<tip>; // creaza primul element vid
this->primul_element->initializat=0;
this->primul_element->point_back=0; // pointerul spre elemntul precedent pt primul elemnt este 0
this->primul_element->point_front=0;// daca pointerul spre urmatorul element este 0 --> nu exita
this->ultimul_element=this->primul_element;
}



template <typename tip>
inline
int vector_eu<tip>::push_bak(tip val)
{
if((this->nr_de_elemente)==0)
{
(this->primul_element)->initializat=1;
(this->primul_element)->val=val;
++(this->nr_de_elemente);
return 1;
}
else
{
element<tip> * temp=new element;
temp->initializat=1;
temp->val=val;
temp->point_front=0;
temp->point_back=(this->ultimul_element);
(this->ultimul_element)->point_front=temp;
(this->ultimul_element)=temp;
++(this->nr_de_elemente);
return 1;
}
}



template <typename tip>
inline
element<tip>* vector_eu<tip>::get_addres(int n) /// iterator incepe de la 0
{
if (this->nr_de_elemente==0)
{
error("nu exista elemente");
return 0;
}
else
{ if(n<this->nr_de_elemente)
{
if(n==0) return this->primul_element;
else
{
if(n<0){ error("iterator<0");return 0;}
else
{
element<tip>*temp=this->primul_element;
while(n>0)
{
temp=temp->point_front;
--n;
}
return temp;
}
}
}
else
{
error("iterator>nr_de_elemente");
}
}


}









template <typename tip>
inline
int vector_eu<tip>::get(int n)
{
if(this->nr_de_elemente>n)
{
if(n>-1)
{
element<tip> * temp =get_addres(n);
return temp->val;
}
else {
error("iterator<0");
return 0;
}
}
else {
error("iterator>nr_de_elemente");
return 0;
}
}




template <typename tip>
inline
int vector_eu<tip>::clear(void)
{
if(this->nr_de_elemente!=0)
{
if(this->nr_de_elemente>0)
{
element<tip>* temp=this->ultimul_element;
while(this->nr_de_elemente>1)
{
temp=temp->point_back;
delete (temp->point_front);
--(this->nr_de_elemente);
}
temp->point_front=0;
temp->initializat=0;
temp->val=0;
(this->ultimul_element)=(this->primul_element);
(this->nr_de_elemente)=0;
return 1;
}
else {error("nr_de_elemente<0");return 0;}
}
else
return 1;
}



template <typename tip>
inline
int vector_eu<tip>::erace(int n)
{
if(n<this->nr_de_elemente)
{
if(n>-1)
{
if(n==0)
{
if(this->nr_de_elemente==1)
{
this->clear();
}
else
{
element<tip> *temp=this->primul_element;
this->primul_element=(this->primul_element)->point_front;
(this->primul_element)->point_back=0;
delete temp;
--(this->nr_de_elemente);
return 1;
}
}
else{
if((n+1)==this->nr_de_elemente)
{
element<tip> * temp=this->ultimul_element;
((this->ultimul_element)->point_back)->point_front=0;
(this->ultimul_element)=(this->ultimul_element)->point_back;
delete temp;
--(this->nr_de_elemente);
return 1;
}
else
{
element<tip> * temp=get_addres(n);
(temp->point_back)->point_front=temp->point_front;
(temp->point_front)->point_back=temp->point_back;
delete temp;
--(this->nr_de_elemente);
return 1;
}
}
}
else error("iterator<0");

}
else error("iterator>nr_de_elemente");

return 0;
}



template <typename tip>
inline
int vector_eu<tip>::erace(int n, int r) ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
{
if(n<0)
{
error("iterator poz<0");
return 0;
}
else
if(r<0)
{
error("iterator lungime<0");
return 0;
}
else
if(r==0)
{
error("iterator lungime = 0");
}
else
if((n+r)>(this->nr_de_elemente))
{
error("depaseste dimensiunea");
return 0;
}
else
{
if(r==1)
{
erace(n);
return 1;
}
else
if(n==0)
{
if(r==(this->nr_de_elemente))
{
this->clear();
return 1;
}
else
{
element<tip> * temp=(this->primul_element);
for(int i=0;i<r;++i)
{
temp=(temp->point_front);
delete (temp->point_back);

}
(this->primul_element)=temp;
(this->primul_element)->point_back=0;
this->nr_de_elemente-=r;
return 1;
}
}
else
{
if((n+r)==(this->nr_de_elemente))
{
element<tip> *temp=get_addres(n);
(this->ultimul_element)=temp->point_back;
(this->ultimul_element)->point_front=0;
for(int i=1;i<r;++i)
{
temp=temp->point_front;
delete temp->point_back;
}
delete temp;
this->nr_de_elemente-=r;
return 1;
}
else
{
element<tip> *temp=get_addres(n);
element<tip> *temp_2=(get_addres(n-1));
element<tip> *temp_3=get_addres(n+r);
temp_2->point_front=temp_3;
temp_3->point_back=temp_2;
for(int i=1;i<r;++i)
{
temp=temp->point_front;
delete temp->point_back;
}
delete temp;
this->nr_de_elemente-=r;
return 1;
}

}
}
}


template <typename tip>
inline
int vector_eu<tip>::insert(tip val, int n)
{
if((n==0)&&(this->nr_de_elemente==0))
this->push_bak(val);
else
if(n>this->nr_de_elemente)
{
error("iterator>nr_de_elemente");
return 0;
}
else
if(n<0)
{
error("iterator<0");
return 0;
}
else
if(n==this->nr_de_elemente)
{
this->push_bak(val);

return 1;
}
else
if(n==0)
{
element<tip> * temp=new element;
temp->initializat=1;
temp->point_back=0;
temp->point_front=this->primul_element;
(temp->val)=val;
(this->primul_element)->point_back=temp;
this->primul_element=temp;
++(this->nr_de_elemente);
return 1;
}
else
{
element<tip> * temp=get_addres(n-1);
element<tip> * new_element= new element;

new_element->initializat=1;
new_element->val=val;

new_element->point_front=temp->point_front;
(temp->point_front)->point_back=new_element;
temp->point_front=new_element;
new_element->point_back=temp;
++(this->nr_de_elemente);
return 1;

}

}




" test vector eu.cpp "

#include "stdafx.h"
#include "define element.h"
#include <iostream>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int a,b,c;

vector_eu<int> e;
while(1)
{

cout<<endl<<endl;
cout<<"1. push_bak"<<endl;
cout<<"2. insert"<<endl;
cout<<"3.clear"<<endl;
cout<<"4.get"<<endl;
cout<<"5.erace(int)"<<endl;
cout<<"6.erace(int,int)"<<endl;
cin>>a;
if(a==0) break;
switch (a)
{
case 1:
cout<<" val= ";
cin>>b;

e.push_bak(b);
break;
case 2:
cout<<"val= ";
cin>>b;
cout<<" poz= ";
cin>>c;
e.insert(b,c);
break;
case 3:
e.clear();
break;
case 4:
cout<<"poz= ";
cin>>b;
cout<<"e["<<b<<"]= "<<e.get(b);
break;
case 5:
cout<<"poz= ";
cin>>b;
e.erace(b);
break;
case 6:
cout<<"poz= ";
cin>>b;
cout<<"lungme= ";
cin>>c;
e.erace(b,c);
break;

}

}

std::system("pause");
return 0;
}
Dec 28 '06 #6

Expert 100+
P: 1,510
you missed a few things (indicated buy // ** comments)

I have all the code in one file so
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename tip>
  6. class element
  7. {public:
  8. tip val;
  9. int initializat; /// arata daca un element e initializat sau nu
  10. element<tip>* point_front;
  11. element<tip>* point_back;
  12.  
  13. };
  14.  
  15.  
  16. template <typename tip>
  17. class vector_eu
  18. {
  19. public:
  20. vector_eu(); //constructor
  21. int push_bak(tip);//adauga un element in coada vectorului push_bak(element)
  22. int insert(tip,int);//adauga un element intr-un loc specificat insert(element,pozitie)
  23. int erace(int);//sterge un element dintr-o pozitie specificata erace(pozitie)
  24. int erace(int,int);//sterge un nr de caractere incepand cu o pozitie specificata erace(pozitie,nr_de_elemente)
  25. int clear(void); //sterge tot vectorul
  26. int get(int);
  27.  
  28. private:
  29. element<tip>* primul_element;
  30. int nr_de_elemente;
  31. element<tip>* ultimul_element;
  32.  
  33. element<tip>* get_addres(int);
  34.  
  35.  
  36. };
  37.  
  38.  
  39.  
  40.  
  41.  
  42. //" implement.cpp"
  43.  
  44. //#include "stdafx.h"
  45. #include <iostream>
  46. #include <string>
  47. //#include "define element.h"
  48. using namespace std;
  49.  
  50. void error(string s)
  51. {
  52. cout<<s<<endl;
  53. }
  54.  
  55.  
  56. template <typename tip>
  57. inline
  58. vector_eu<tip>::vector_eu()
  59. {
  60. this->nr_de_elemente=0;
  61. this->primul_element=new element<tip>; // creaza primul element vid
  62. this->primul_element->initializat=0;
  63. this->primul_element->point_back=0; // pointerul spre elemntul precedent pt primul elemnt este 0
  64. this->primul_element->point_front=0;// daca pointerul spre urmatorul element este 0 --> nu exita
  65. this->ultimul_element=this->primul_element;
  66. }
  67.  
  68.  
  69.  
  70. template <typename tip>
  71. inline
  72. int vector_eu<tip>::push_bak(tip val)  // ** added :p
  73. {
  74. if((this->nr_de_elemente)==0)
  75. {
  76. (this->primul_element)->initializat=1;
  77. (this->primul_element)->val=val;
  78. ++(this->nr_de_elemente);
  79. return 1;
  80. }
  81. else
  82. {
  83. element<tip> * temp=new element<tip>();
  84. temp->initializat=1;
  85. temp->val=val;
  86. temp->point_front=0;
  87. temp->point_back=(this->ultimul_element);
  88. (this->ultimul_element)->point_front=temp;
  89. (this->ultimul_element)=temp;
  90. ++(this->nr_de_elemente);
  91. return 1;
  92. }
  93. }
  94.  
  95.  
  96.  
  97. template <typename tip>
  98. inline
  99. element<tip>* vector_eu<tip>::get_addres(int n) /// iterator incepe de la 0
  100. {
  101. if (this->nr_de_elemente==0)
  102. {
  103. error("nu exista elemente");
  104. return 0;
  105. }
  106. else
  107. { if(n<this->nr_de_elemente)
  108. {
  109. if(n==0) return this->primul_element;
  110. else
  111. {
  112. if(n<0){ error("iterator<0");return 0;}
  113. else
  114. {
  115. element<tip>*temp=this->primul_element;
  116. while(n>0)
  117. {
  118. temp=temp->point_front;
  119. --n;
  120. }
  121. return temp;
  122. }
  123. }
  124. }
  125. else
  126. {
  127. error("iterator>nr_de_elemente");
  128. }
  129. }
  130.  
  131.  
  132. }
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142. template <typename tip>
  143. inline
  144. int vector_eu<tip>::get(int n)
  145. {
  146. if(this->nr_de_elemente>n)
  147. {
  148. if(n>-1)
  149. {
  150. element<tip> * temp =get_addres(n);
  151. return temp->val;
  152. }
  153. else {
  154. error("iterator<0");
  155. return 0;
  156. }
  157. }
  158. else {
  159. error("iterator>nr_de_elemente");
  160. return 0;
  161. }
  162. }
  163.  
  164.  
  165.  
  166.  
  167. template <typename tip>
  168. inline
  169. int vector_eu<tip>::clear(void)
  170. {
  171. if(this->nr_de_elemente!=0)
  172. {
  173. if(this->nr_de_elemente>0)
  174. {
  175. element<tip>* temp=this->ultimul_element;
  176. while(this->nr_de_elemente>1)
  177. {
  178. temp=temp->point_back;
  179. delete (temp->point_front);
  180. --(this->nr_de_elemente);
  181. }
  182. temp->point_front=0;
  183. temp->initializat=0;
  184. temp->val=0;
  185. (this->ultimul_element)=(this->primul_element);
  186. (this->nr_de_elemente)=0;
  187. return 1;
  188. }
  189. else {error("nr_de_elemente<0");return 0;}
  190. }
  191. else
  192. return 1;
  193. }
  194.  
  195.  
  196.  
  197. template <typename tip>
  198. inline
  199. int vector_eu<tip>::erace(int n)
  200. {
  201. if(n<this->nr_de_elemente)
  202. {
  203. if(n>-1)
  204. {
  205. if(n==0)
  206. {
  207. if(this->nr_de_elemente==1)
  208. {
  209. this->clear();
  210. }
  211. else
  212. {
  213. element<tip> *temp=this->primul_element;
  214. this->primul_element=(this->primul_element)->point_front;
  215. (this->primul_element)->point_back=0;
  216. delete temp;
  217. --(this->nr_de_elemente);
  218. return 1;
  219. }
  220. }
  221. else{
  222. if((n+1)==this->nr_de_elemente)
  223. {
  224. element<tip> * temp=this->ultimul_element;
  225. ((this->ultimul_element)->point_back)->point_front=0;
  226. (this->ultimul_element)=(this->ultimul_element)->point_back;
  227. delete temp;
  228. --(this->nr_de_elemente);
  229. return 1;
  230. }
  231. else
  232. {
  233. element<tip> * temp=get_addres(n);
  234. (temp->point_back)->point_front=temp->point_front;
  235. (temp->point_front)->point_back=temp->point_back;
  236. delete temp;
  237. --(this->nr_de_elemente);
  238. return 1;
  239. }
  240. }
  241. }
  242. else error("iterator<0");
  243.  
  244. }
  245. else error("iterator>nr_de_elemente");
  246.  
  247. return 0;
  248. }
  249.  
  250.  
  251.  
  252. template <typename tip>
  253. inline
  254. int vector_eu<tip>::erace(int n, int r) ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  255. {
  256. if(n<0)
  257. {
  258. error("iterator poz<0");
  259. return 0;
  260. }
  261. else
  262. if(r<0)
  263. {
  264. error("iterator lungime<0");
  265. return 0;
  266. }
  267. else
  268. if(r==0)
  269. {
  270. error("iterator lungime = 0");
  271. }
  272. else
  273. if((n+r)>(this->nr_de_elemente))
  274. {
  275. error("depaseste dimensiunea");
  276. return 0;
  277. }
  278. else
  279. {
  280. if(r==1)
  281. {
  282. erace(n);
  283. return 1;
  284. }
  285. else
  286. if(n==0)
  287. {
  288. if(r==(this->nr_de_elemente))
  289. {
  290. this->clear();
  291. return 1;
  292. }
  293. else
  294. {
  295. element<tip> * temp=(this->primul_element);
  296. for(int i=0;i<r;++i)
  297. {
  298. temp=(temp->point_front);
  299. delete (temp->point_back);
  300.  
  301. }
  302. (this->primul_element)=temp;
  303. (this->primul_element)->point_back=0;
  304. this->nr_de_elemente-=r;
  305. return 1;
  306. }
  307. }
  308. else
  309. {
  310. if((n+r)==(this->nr_de_elemente))
  311. {
  312. element<tip> *temp=get_addres(n);
  313. (this->ultimul_element)=temp->point_back;
  314. (this->ultimul_element)->point_front=0;
  315. for(int i=1;i<r;++i)
  316. {
  317. temp=temp->point_front;
  318. delete temp->point_back;
  319. }
  320. delete temp;
  321. this->nr_de_elemente-=r;
  322. return 1;
  323. }
  324. else
  325. {
  326. element<tip> *temp=get_addres(n);
  327. element<tip> *temp_2=(get_addres(n-1));
  328. element<tip> *temp_3=get_addres(n+r);
  329. temp_2->point_front=temp_3;
  330. temp_3->point_back=temp_2;
  331. for(int i=1;i<r;++i)
  332. {
  333. temp=temp->point_front;
  334. delete temp->point_back;
  335. }
  336. delete temp;
  337. this->nr_de_elemente-=r;
  338. return 1;
  339. }
  340.  
  341. }
  342. }
  343. }
  344.  
  345.  
  346. template <typename tip>
  347. inline
  348. int vector_eu<tip>::insert(tip val, int n)
  349. {
  350. if((n==0)&&(this->nr_de_elemente==0))
  351. this->push_bak(val);
  352. else
  353. if(n>this->nr_de_elemente)
  354. {
  355. error("iterator>nr_de_elemente");
  356. return 0;
  357. }
  358. else
  359. if(n<0)
  360. {
  361. error("iterator<0");
  362. return 0;
  363. }
  364. else
  365. if(n==this->nr_de_elemente)
  366. {
  367. this->push_bak(val);
  368.  
  369. return 1;
  370. }
  371. else
  372. if(n==0)
  373. {
  374. element<tip> * temp=new element<tip>();  // ** added <tip>()
  375. temp->initializat=1;
  376. temp->point_back=0;
  377. temp->point_front=this->primul_element;
  378. (temp->val)=val;
  379. (this->primul_element)->point_back=temp;
  380. this->primul_element=temp;
  381. ++(this->nr_de_elemente);
  382. return 1;
  383. }
  384. else
  385. {
  386. element<tip> * temp=get_addres(n-1);
  387. element<tip> * new_element= new element<tip>();  // ** added <tip>()
  388.  
  389. new_element->initializat=1;
  390. new_element->val=val;
  391.  
  392. new_element->point_front=temp->point_front;
  393. (temp->point_front)->point_back=new_element;
  394. temp->point_front=new_element;
  395. new_element->point_back=temp;
  396. ++(this->nr_de_elemente);
  397. return 1;
  398.  
  399. }
  400.  
  401. }
  402.  
  403.  
  404.  
  405.  
  406. //" test vector eu.cpp "
  407.  
  408. //#include "stdafx.h"
  409. //#include "define element.h"
  410. //#include <iostream>
  411.  
  412. //using namespace std;
  413.  
  414. int main(int argc, char* argv[])  // ** change to normal main()
  415. {
  416. int a,b,c;
  417.  
  418. vector_eu<int> e;
  419. while(1)
  420. {
  421.  
  422. cout<<endl<<endl;
  423. cout<<"1. push_bak"<<endl;
  424. cout<<"2. insert"<<endl;
  425. cout<<"3.clear"<<endl;
  426. cout<<"4.get"<<endl;
  427. cout<<"5.erace(int)"<<endl;
  428. cout<<"6.erace(int,int)"<<endl;
  429. cin>>a;
  430. if(a==0) break;
  431. switch (a)
  432. {
  433. case 1:
  434. cout<<" val= ";
  435. cin>>b;
  436.  
  437. e.push_bak(b);
  438. break;
  439. case 2:
  440. cout<<"val= ";
  441. cin>>b;
  442. cout<<" poz= ";
  443. cin>>c;
  444. e.insert(b,c);
  445. break;
  446. case 3:
  447. e.clear();
  448. break;
  449. case 4:
  450. cout<<"poz= ";
  451. cin>>b;
  452. cout<<"e["<<b<<"]= "<<e.get(b);
  453. break;
  454. case 5:
  455. cout<<"poz= ";
  456. cin>>b;
  457. e.erace(b);
  458. break;
  459. case 6:
  460. cout<<"poz= ";
  461. cin>>b;
  462. cout<<"lungme= ";
  463. cin>>c;
  464. e.erace(b,c);
  465. break;
  466.  
  467. }
  468.  
  469. }
  470.  
  471. std::system("pause");
  472. return 0;
  473. }
  474.  
I compile it with gcc and when run it gives


1. push_bak
2. insert
3.clear
4.get
5.erace(int)
6.erace(int,int)
1
val= 45

1. push_bak
2. insert
3.clear
4.get
5.erace(int)
6.erace(int,int)
1
val= 23

1. push_bak
2. insert
3.clear
4.get
5.erace(int)
6.erace(int,int)
4
poz= 1
e[1]= 23

1. push_bak
2. insert
3.clear
4.get
5.erace(int)
6.erace(int,int)
Dec 28 '06 #7

P: 30
i modifaide it but stil doesn't work

i stil get :

Error 1 error LNK2019: unresolved external symbol "public: int __thiscall vector_eu<int>::erace(int,int)" (?erace@?$vector_eu@H@@QAEHHH@Z) referenced in function _wmain test vector eu.obj


one for each function

does any on know why?
Dec 28 '06 #8

P: 30
Pls Help!!!!!!!
Dec 30 '06 #9

Expert 100+
P: 1,510
the code in my last post works OK with DEV-C++ & Borland V5.6 under windows and gnu C++ under Linux

tried the code with VC V5.0 had to make a cople of modifiactions (marked in code below) and it ran OK

copy this and save it into a single file - it should compile and run
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename tip>
  6. class element
  7. {public:
  8. tip val;
  9. int initializat; /// arata daca un element e initializat sau nu
  10. element<tip>* point_front;
  11. element<tip>* point_back;
  12.  
  13. };
  14.  
  15.  
  16. template <typename tip>
  17. class vector_eu
  18. {
  19. public:
  20. vector_eu(); //constructor
  21. int push_bak(tip);//adauga un element in coada vectorului push_bak(element)
  22. int insert(tip,int);//adauga un element intr-un loc specificat insert(element,pozitie)
  23. int erace(int);//sterge un element dintr-o pozitie specificata erace(pozitie)
  24. int erace(int,int);//sterge un nr de caractere incepand cu o pozitie specificata erace(pozitie,nr_de_elemente)
  25. int clear(void); //sterge tot vectorul
  26. int get(int);
  27.  
  28. private:
  29. element<tip>* primul_element;
  30. int nr_de_elemente;
  31. element<tip>* ultimul_element;
  32.  
  33. element<tip>* get_addres(int);
  34.  
  35.  
  36. };
  37.  
  38.  
  39. //" implement.cpp"
  40.  
  41. //#include "stdafx.h"
  42. #include <iostream>
  43. #include <string>
  44. //#include "define element.h"
  45. using namespace std;
  46.  
  47. void error(string s)
  48. {
  49. cout<<s<<endl;
  50. }
  51.  
  52.  
  53. template <typename tip>
  54. inline
  55. vector_eu<tip>::vector_eu()
  56. {
  57. this->nr_de_elemente=0;
  58. this->primul_element=new element<tip>; // creaza primul element vid
  59. this->primul_element->initializat=0;
  60. this->primul_element->point_back=0; // pointerul spre elemntul precedent pt primul elemnt este 0
  61. this->primul_element->point_front=0;// daca pointerul spre urmatorul element este 0 --> nu exita
  62. this->ultimul_element=this->primul_element;
  63. }
  64.  
  65.  
  66.  
  67. template <typename tip>
  68. inline
  69. int vector_eu<tip>::push_bak(tip val)  // ** added :p
  70. {
  71. if((this->nr_de_elemente)==0)
  72. {
  73. (this->primul_element)->initializat=1;
  74. (this->primul_element)->val=val;
  75. ++(this->nr_de_elemente);
  76. return 1;
  77. }
  78. else
  79. {
  80. element<tip> * temp=new element<tip>();
  81. temp->initializat=1;
  82. temp->val=val;
  83. temp->point_front=0;
  84. temp->point_back=(this->ultimul_element);
  85. (this->ultimul_element)->point_front=temp;
  86. (this->ultimul_element)=temp;
  87. ++(this->nr_de_elemente);
  88. return 1;
  89. }
  90. }
  91.  
  92.  
  93.  
  94. template <typename tip>
  95. inline
  96. element<tip>* vector_eu<tip>::get_addres(int n) /// iterator incepe de la 0
  97. {
  98. if (this->nr_de_elemente==0)
  99. {
  100. error("nu exista elemente");
  101. return 0;
  102. }
  103. else
  104. { if(n<this->nr_de_elemente)
  105. {
  106. if(n==0) return this->primul_element;
  107. else
  108. {
  109. if(n<0){ error("iterator<0");return 0;}
  110. else
  111. {
  112. element<tip>*temp=this->primul_element;
  113. while(n>0)
  114. {
  115. temp=temp->point_front;
  116. --n;
  117. }
  118. return temp;
  119. }
  120. }
  121. }
  122. else
  123. {
  124. error("iterator>nr_de_elemente");
  125. }
  126. }
  127. return 0;  // ** added for VC
  128.  
  129. }
  130.  
  131.  
  132.  
  133. template <typename tip>
  134. inline
  135. int vector_eu<tip>::get(int n)
  136. {
  137. if(this->nr_de_elemente>n)
  138. {
  139. if(n>-1)
  140. {
  141. element<tip> * temp =get_addres(n);
  142. return temp->val;
  143. }
  144. else {
  145. error("iterator<0");
  146. return 0;
  147. }
  148. }
  149. else {
  150. error("iterator>nr_de_elemente");
  151. return 0;
  152. }
  153. }
  154.  
  155.  
  156. template <typename tip>
  157. inline
  158. int vector_eu<tip>::clear(void)
  159. {
  160. if(this->nr_de_elemente!=0)
  161. {
  162. if(this->nr_de_elemente>0)
  163. {
  164. element<tip>* temp=this->ultimul_element;
  165. while(this->nr_de_elemente>1)
  166. {
  167. temp=temp->point_back;
  168. delete (temp->point_front);
  169. --(this->nr_de_elemente);
  170. }
  171. temp->point_front=0;
  172. temp->initializat=0;
  173. temp->val=0;
  174. (this->ultimul_element)=(this->primul_element);
  175. (this->nr_de_elemente)=0;
  176. return 1;
  177. }
  178. else {error("nr_de_elemente<0");return 0;}
  179. }
  180. else
  181. return 1;
  182. }
  183.  
  184.  
  185.  
  186. template <typename tip>
  187. inline
  188. int vector_eu<tip>::erace(int n)
  189. {
  190. if(n<this->nr_de_elemente)
  191. {
  192. if(n>-1)
  193. {
  194. if(n==0)
  195. {
  196. if(this->nr_de_elemente==1)
  197. {
  198. this->clear();
  199. }
  200. else
  201. {
  202. element<tip> *temp=this->primul_element;
  203. this->primul_element=(this->primul_element)->point_front;
  204. (this->primul_element)->point_back=0;
  205. delete temp;
  206. --(this->nr_de_elemente);
  207. return 1;
  208. }
  209. }
  210. else{
  211. if((n+1)==this->nr_de_elemente)
  212. {
  213. element<tip> * temp=this->ultimul_element;
  214. ((this->ultimul_element)->point_back)->point_front=0;
  215. (this->ultimul_element)=(this->ultimul_element)->point_back;
  216. delete temp;
  217. --(this->nr_de_elemente);
  218. return 1;
  219. }
  220. else
  221. {
  222. element<tip> * temp=get_addres(n);
  223. (temp->point_back)->point_front=temp->point_front;
  224. (temp->point_front)->point_back=temp->point_back;
  225. delete temp;
  226. --(this->nr_de_elemente);
  227. return 1;
  228. }
  229. }
  230. }
  231. else error("iterator<0");
  232.  
  233. }
  234. else error("iterator>nr_de_elemente");
  235.  
  236. return 0;
  237. }
  238.  
  239.  
  240.  
  241. template <typename tip>
  242. inline
  243. int vector_eu<tip>::erace(int n, int r) ///!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  244. {
  245. if(n<0)
  246. {
  247. error("iterator poz<0");
  248. return 0;
  249. }
  250. else
  251. if(r<0)
  252. {
  253. error("iterator lungime<0");
  254. return 0;
  255. }
  256. else
  257. if(r==0)
  258. {
  259. error("iterator lungime = 0");
  260. }
  261. else
  262. if((n+r)>(this->nr_de_elemente))
  263. {
  264. error("depaseste dimensiunea");
  265. return 0;
  266. }
  267. else
  268. {
  269. if(r==1)
  270. {
  271. erace(n);
  272. return 1;
  273. }
  274. else
  275. if(n==0)
  276. {
  277. if(r==(this->nr_de_elemente))
  278. {
  279. this->clear();
  280. return 1;
  281. }
  282. else
  283. {
  284. element<tip> * temp=(this->primul_element);
  285. for(int i=0;i<r;++i)
  286. {
  287. temp=(temp->point_front);
  288. delete (temp->point_back);
  289.  
  290. }
  291. (this->primul_element)=temp;
  292. (this->primul_element)->point_back=0;
  293. this->nr_de_elemente-=r;
  294. return 1;
  295. }
  296. }
  297. else
  298. {
  299. if((n+r)==(this->nr_de_elemente))
  300. {
  301. element<tip> *temp=get_addres(n);
  302. (this->ultimul_element)=temp->point_back;
  303. (this->ultimul_element)->point_front=0;
  304. for(int i=1;i<r;++i)
  305. {
  306. temp=temp->point_front;
  307. delete temp->point_back;
  308. }
  309. delete temp;
  310. this->nr_de_elemente-=r;
  311. return 1;
  312. }
  313. else
  314. {
  315. element<tip> *temp=get_addres(n);
  316. element<tip> *temp_2=(get_addres(n-1));
  317. element<tip> *temp_3=get_addres(n+r);
  318. temp_2->point_front=temp_3;
  319. temp_3->point_back=temp_2;
  320. for(int i=1;i<r;++i)
  321. {
  322. temp=temp->point_front;
  323. delete temp->point_back;
  324. }
  325. delete temp;
  326. this->nr_de_elemente-=r;
  327. return 1;
  328. }
  329.  
  330. }
  331. }
  332. return 0;  // ** added for VC
  333. }
  334.  
  335.  
  336. template <typename tip>
  337. inline
  338. int vector_eu<tip>::insert(tip val, int n)
  339. {
  340. if((n==0)&&(this->nr_de_elemente==0))
  341. this->push_bak(val);
  342. else
  343. if(n>this->nr_de_elemente)
  344. {
  345. error("iterator>nr_de_elemente");
  346. return 0;
  347. }
  348. else
  349. if(n<0)
  350. {
  351. error("iterator<0");
  352. return 0;
  353. }
  354. else
  355. if(n==this->nr_de_elemente)
  356. {
  357. this->push_bak(val);
  358.  
  359. return 1;
  360. }
  361. else
  362. if(n==0)
  363. {
  364. element<tip> * temp=new element<tip>();  // ** added <tip>()
  365. temp->initializat=1;
  366. temp->point_back=0;
  367. temp->point_front=this->primul_element;
  368. (temp->val)=val;
  369. (this->primul_element)->point_back=temp;
  370. this->primul_element=temp;
  371. ++(this->nr_de_elemente);
  372. return 1;
  373. }
  374. else
  375. {
  376. element<tip> * temp=get_addres(n-1);
  377. element<tip> * new_element= new element<tip>();  // ** added <tip>()
  378.  
  379. new_element->initializat=1;
  380. new_element->val=val;
  381.  
  382. new_element->point_front=temp->point_front;
  383. (temp->point_front)->point_back=new_element;
  384. temp->point_front=new_element;
  385. new_element->point_back=temp;
  386. ++(this->nr_de_elemente);
  387. return 1;
  388.  
  389. }
  390.  
  391. return 0;  // ** added for VC
  392. }
  393.  
  394.  
  395.  
  396.  
  397. //" test vector eu.cpp "
  398.  
  399. //#include "stdafx.h"
  400. //#include "define element.h"
  401. //#include <iostream>
  402.  
  403. //using namespace std;
  404.  
  405. int main(int argc, char* argv[])  // ** change to normal main()
  406. {
  407. int a,b,c;
  408.  
  409. vector_eu<int> e;
  410. while(1)
  411. {
  412.  
  413. cout<<endl<<endl;
  414. cout<<"1. push_bak"<<endl;
  415. cout<<"2. insert"<<endl;
  416. cout<<"3.clear"<<endl;
  417. cout<<"4.get"<<endl;
  418. cout<<"5.erace(int)"<<endl;
  419. cout<<"6.erace(int,int)"<<endl;
  420. cin>>a;
  421. if(a==0) break;
  422. switch (a)
  423. {
  424. case 1:
  425. cout<<" val= ";
  426. cin>>b;
  427.  
  428. e.push_bak(b);
  429. break;
  430. case 2:
  431. cout<<"val= ";
  432. cin>>b;
  433. cout<<" poz= ";
  434. cin>>c;
  435. e.insert(b,c);
  436. break;
  437. case 3:
  438. e.clear();
  439. break;
  440. case 4:
  441. cout<<"poz= ";
  442. cin>>b;
  443. cout<<"e["<<b<<"]= "<<e.get(b);
  444. break;
  445. case 5:
  446. cout<<"poz= ";
  447. cin>>b;
  448. e.erace(b);
  449. break;
  450. case 6:
  451. cout<<"poz= ";
  452. cin>>b;
  453. cout<<"lungme= ";
  454. cin>>c;
  455. e.erace(b,c);
  456. break;
  457.  
  458. }
  459.  
  460. }
  461.  
  462. system("pause");  // ** remove std:: for VC
  463. return 0;
  464. }
  465.  
Dec 31 '06 #10

P: 30
Thanks I Tried It . And It Works !!!
By The Way Happy New Year!!!!!!!!!!

But I Wat To Have The Code In Different Files Like A Header File And A Code File So I Can Include The Header In Different Other Files Without Copying The Code.
Dec 31 '06 #11

Post your reply

Sign in to post your reply or Sign up for a free account.