Solved

compiling

Posted on 1998-08-23
13
274 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 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.

743 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

10 Experts available now in Live!

Get 1:1 Help Now