Solved

compiling

Posted on 1998-08-23
13
277 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

914 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now