473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to fix linker error with function template?

bajajv
152 100+
Hi, I am trying to write a function template which takes starting and end addresses for an array and the value to search. But when I call this function, I get linker error 1120 - 1 unresolved external.
Can you please check what am I doing wrong?

Expand|Select|Wrap|Line Numbers
  1. template <typename T>
  2. bool find(T* first, T* last, T& value)
  3. {
  4.     bool result = false;
  5.  
  6.     if ((first) && (last))
  7.     {
  8.         while (first != last)
  9.         {
  10.             if (*first == value)
  11.             {
  12.                 result = true;
  13.                 break;
  14.             }
  15.             first++;
  16.         }
  17.     }
  18.     return result;
  19. }
  20.  
  21. int main()
  22. {
  23.     int arr[] = {1,2,3,4,5};
  24.  
  25.     vector<int> vect;
  26.     vect.push_back(arr[1]);
  27.     vect.push_back(arr[2]);
  28.     vect.push_back(arr[3]);
  29.     vect.push_back(arr[4]);
  30.     vect.push_back(arr[5]);
  31.  
  32.     int val_to_check = 3;
  33.  
  34.     bool result1 = find(arr, arr+4, val_to_check);
  35.  
  36.     bool result2 = find(&vect[0], &vect[4], val_to_check);
  37.  
  38.     cout << result1 << " " << result2 << endl;
  39.  
  40.      return 0;
  41. }
  42.  
Jan 23 '11 #1
4 2085
horace1
1,510 Expert 1GB
gcc compiles it OK when I add the headers
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
a run gives
Expand|Select|Wrap|Line Numbers
  1. 1 1 
  2.  
what compiler are you using?
Jan 23 '11 #2
bajajv
152 100+
It is working here also now.. it turned out that the implementations were in different file, so the definition was not available.
After including the definition file, it is working.
Sorry for the trouble..
But do I need to include both the .h as well as .cpp files everytime?
Jan 23 '11 #3
horace1
1,510 Expert 1GB
header files usually hold declarations and you include them in any file that uses the constants/classes/functions/externs etc.
You include the .cpp file that contains the definitions once in the project otherwise you would get identifiers defined multiple times.
Jan 23 '11 #4
bajajv
152 100+
thanks horace1.. this solved my error in another function also... :)
Jan 23 '11 #5

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

Similar topics

5
by: tuko | last post by:
The following snipet gives a linker error. I don't get it... template<class T> class tran { public: public: private: }; template<class T> class matrix {
2
by: B_Love | last post by:
Hey! When trying to compile the code for a ordered vector class I get the following error: undefined reference to `WinMain@16' Anyone have any idea what I might be doing wrong? I've been...
2
by: haplotype | last post by:
I have designed a package with several files, which is listed bellow base.cpp & base.hpp - define the template class base tree.cpp & tree.hpp - define the class tree derived from base<int>...
3
by: Bob Altman | last post by:
Hi all, This is the first time I've tried to create a templatized function, and I can't get past a linker error. I create a new, unmanaged C++ application and added a class named MyClass. In...
4
by: PyongHopscotch | last post by:
Hi All, So I'm getting the generic linker error (unresolved external error) in VC 7.0 when I define a templatized function. Here's some code examples: template <class T> class...
0
by: Robbie Hatley | last post by:
I'd always thougth that a C++ compiler/linker should be able to instantiate a template in mulitple places (say, in two different translation units), even using the same template parameters so that...
5
by: Mark | last post by:
Sorry for creating such a newbish topic, but I just can't seem to figure out what the problem is here. // main.cpp #include <cstdlib> #include <iostream> #include "Vector.h" using namespace...
1
by: giganut | last post by:
Hi, I'm using MS VS.Net2003, and I'm getting a linker error in a project for a templated friend function Specifically: template <class Tclass Vec3 {...
1
by: antani | last post by:
When I add to my code this line vcg::tri::io::Importer<CMesh>::Open(mesh,"mesh.ply"); I get a linker error. Please help me. I tested a qt empty project with only main function. I added the...
3
by: Rahul | last post by:
Hi Everyone, I have the following polymorphic classes, class Shape { public : virtual void draw() { } virtual void sample();
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.