Link to home
Start Free TrialLog in
Avatar of simi
simi

asked on

compiling

Hi

I have problems compiling the following:
file CData.h
*****************************************
#ifndef _CData
#define _CData

class CData{
      int i;
public:
      CData();
      ~CData();
      int give();
};


file CData.cpp
*****************************************

#include<stdio.h>
#include"CData.h"

CData::CData()
{
}

CData::~CData()
{
}

int CData::give()
{
      return i;
}



#endif


file CAction.h
*****************************************

#ifndef _CAction
#define _CAction

template<class SomeData> class CAction{
public:
      print();
};

#endif

file CAction.cpp
***************************************

#include "CAction.h"


template<class SomeData> CAction<SomeData>::CAction()
{
      
}
      
template<class SomeData> CAction<SomeData>::~CAction()
{
}

template<class SomeData> CAction<SomeData>::print()
{
      printf("This is it : %d\n", SomeData.give());
}


file test.cpp
******************************************

#include"CData.h"
#include"CAction.h"


int main()
{
      CAction<CData> action;
      action.print();
      
      return 0;
}


I can compile all the .cpp files.
When I want to link I get the following message:
unresolved external symbol "public: int __thiscall CAction<class CData>::print(void)"(?print@?$CAction@VCData@@@@QAEHXZ)
on MSVisual C++. I tried it on a Turbo C++(Borland) compiler
and I get a similar error message.

Can you please help ?
Avatar of q2guo
q2guo

change the CAction.cpp file to the following
   

    file CAction.cpp
    ***************************************

    #include "CAction.h"


    template<class SomeData> CAction<SomeData>::CAction()
    {

    }

    template<class SomeData> CAction<SomeData>::~CAction()
    {
    }

    template<class SomeData> CAction<SomeData>::print(void)
    {
    printf("This is it : %d\n", SomeData.give());
    }
Avatar of simi

ASKER

The only change I notice in your answer is the void being added to the print function. I did it and still get the same error when linking.
Avatar of simi

ASKER

Changing print() to print(void) does not make any change
I think I got it!

public: int __thiscall CAction<class CData>::print(void)"(?

Ahhh.. the 'int'.  You didn't give it a return type in the declaration.  Add 'int' or 'void' to the beginning of the line.

void template<class SomeData> CAction<SomeData>::print()

Phillip
Avatar of simi

ASKER

No.
It is right that I forgot to give the return type, and that's why it was presumed to be int. After I have added void before print()(by the way it should be template<class SomeData> void print() and not void template<class SomeData> print()), so I added void and I still get the error message, the only thing that has changed is void has replaced int as a return type:

unresolved external symbol "public: void __thiscall CAction<class CData>::print(void)"(?print@?$CAction@VCData@@@@QAEXXZ)
This was the last error message I got
Combine the contents of CAction.h and CAction.cpp into CAction.h and delete CAction.cpp. The body of template needs to be present at compiling time. It is similar to macro.
Avatar of simi

ASKER

Adjusted points to 100
Avatar of simi

ASKER

Sorry chensu
I did not see your answer when I rejected it.
I can not check the behaviour according to your recomended change untill later in the evening, but it sounds to me that you are right.
Please let me know how can I do in order to assign you the points, can you answer me again so I could evaluate your answer and give you the points or is there another way of doing it.
Thanks.
Simi
chensu is right, a template implementation must go in a header
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simi

ASKER

Yes, I tried and it works.
But I am asking myself what about the "hiding".
I understood that one of the meanings of object oriented is to
hide the implementation and provide the user just with an interface that is placed in the .h file. Placing the implementation of a template in the .h file reveales it to the user isn't it ?
Is there any way where you can use a .h , .cpp file in order to define and implement a template ?
Yes, you have to reveal it. You may assemble some code which is not relevant to the template into some functions in the .cpp and call them from the template in the .h.
Avatar of simi

ASKER

Thanks