Solved

compiling

Posted on 1998-08-23
13
285 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
Industry Leaders: 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to split this in C++ 4 118
Arduino EDI - Programming language 3 114
Create a path if not exists 7 108
Error C2678: binary '!=': no operator found... 4 64
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

763 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