Solved

compiling

Posted on 1998-08-23
13
289 Views
Last Modified: 2012-05-05
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 ?
0
Comment
Question by:simi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 3

Expert Comment

by:q2guo
ID: 1171019
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());
    }
0
 

Author Comment

by:simi
ID: 1171020
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.
0
 

Author Comment

by:simi
ID: 1171021
Changing print() to print(void) does not make any change
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:psdavis
ID: 1171022
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
0
 

Author Comment

by:simi
ID: 1171023
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
0
 
LVL 23

Expert Comment

by:chensu
ID: 1171024
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.
0
 

Author Comment

by:simi
ID: 1171025
Adjusted points to 100
0
 

Author Comment

by:simi
ID: 1171026
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
0
 
LVL 3

Expert Comment

by:LucHoltkamp
ID: 1171027
chensu is right, a template implementation must go in a header
0
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1171028
Thank you.
0
 

Author Comment

by:simi
ID: 1171029
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 ?
0
 
LVL 23

Expert Comment

by:chensu
ID: 1171030
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.
0
 

Author Comment

by:simi
ID: 1171031
Thanks
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question