CoderSource.net
Search
Re: templates_hate_me
Started by transistor at 04-20-2006 3:50 PM. Topic has 1 replies.

Print Search « Previous Thread Next Thread »
   04-20-2006, 3:50 PM
transistor is not online. Last active: 4/23/2006 6:25:04 PM transistor

Top 75 Posts
Joined on 04-20-2006
Posts 1
Zip it! [:#] templates_hate_me

Hello everybody

I am developing a simple program and it has the next errors::

unresolved external symbol "public: class cComplejo<int> __thiscall cComplejo<int>::operator+(class cComplejo<int>)" (??H?$cComplejo@H@@QAE?AV0@V0@@Z)

unresolved external symbol "class ostream & __cdecl operator<<(class ostream &,class cComplejo<int> &)" (??6@YAAAVostream@@AAV0@AAV?$cComplejo@H@@@Z)

error LNK2001: unresolved external symbol "public: __thiscall cComplejo<int>::cComplejo<int>(int,int)" (??0?$cComplejo@H@@QAE@HH@Z)

The source code is very simple and I don´t know why is the reason when appears linkers error:

*****************************clase_complejo.h*****************************
#ifndef __CLASE_COMPLEJO_H__
#define __CLASE_COMPLEJO_H__
#include <iostream.h>

template <class Tipo>
class cComplejo
{
     private:
            Tipo real;        
            Tipo imaginaria;     
     public:
          cComplejo(Tipo, Tipo);
          cComplejo operator+(cComplejo);
          friend ostream& operator <<(ostream &salida,cComplejo &complejo);
};
#endif
***********************clase_complejo.cpp*********************************
#include <iostream.h>
#include "clase_complejo.h"

template <class Tipo>
cComplejo<Tipo>::cComplejo<Tipo>(Tipo real, Tipo imaginaria){
 this->real = real;
 this->imaginaria = imaginaria;
};

template <class Tipo>
cComplejo<Tipo> cComplejo<Tipo>::operator+(cComplejo<Tipo> operando2){
 this->real = this->real + operando2.real;
 this->imaginaria = this->imaginaria + operando2.imaginaria;
};

template <class Tipo>
ostream& operator <<(ostream& salida,cComplejo<Tipo> &complejo){
 salida << "Real: " << complejo.real << endl;
 salida << "Imaginaria: " << complejo.imaginaria << endl;
 return salida;
};

*********************************************************ppal.cpp*********************

#include <iostream.h>
#include "clase_complejo.h"

void main(){
 //Se le indica el tipo de dato que va a manipular la clase
 cComplejo <int> complejo1(1, -3);
 cComplejo <int> complejo2(2, 1);
 cComplejo <int> complejo3(0, 0);

 cout << "Complejo 1: " << endl << complejo1;
 cout << "Complejo 2: " << endl << complejo2;

 complejo3 = complejo1 + complejo2;
 cout << "Complejo 3: " << endl << complejo1;
 }

Thank you very much,

Daniel

 


   Report 
   04-21-2006, 7:59 AM
Yasir is not online. Last active: 11/16/2006 10:02:40 AM Yasir

Top 10 Posts
Joined on 03-15-2006
Posts 44
Re: templates_hate_me
Hi Daniel

Actually the problem is that templates classes should have defination of class and implementation in the same file. you are putting it in two different files that is the problem.

you can solve this by putting the whole code in header file or include the *.cpp in the header file at the end of defination. Like this

*****************************clase_complejo.h*****************************
#ifndef __CLASE_COMPLEJO_H__
#define __CLASE_COMPLEJO_H__
#include


template
class cComplejo
{
private:
Tipo real;
Tipo imaginaria;
public:
cComplejo(Tipo, Tipo);
cComplejo operator+(cComplejo);
friend ostream& operator <<(ostream &salida,cComplejo &complejo);
};
#include "clase_complejo.cpp"
#endif

I have just added one line before the #endif. It will solve your problem.
If you need any further assistance you are welcome.

Regards
-Yasir
   Report 
Codersource.Net » Programming » C++ Programming » Re: templates_hate_me

MENU
Home
MFC 
C++
.Net
WIN32
Programming
Forum
My Articles
Welcome to Codersource.Net Login | Register | Faq  

Google
 

NOTES:


Thanks for visiting our CoderSource.net. This site will be improved with more articles. Interested visitors can also submit their articles through the Submit Article link.Your article will also be published after due consideration by the editor. 

© Copyright 2003. All rights on content reserved by CoderSource.net. Contact    About Us