Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Why only some of the methods of a template are allowed to be defined outside of class?
Why only some of the methods of a template are allowed to be defined outside of class? [message #1821255] Sat, 08 February 2020 13:10 Go to next message
User One is currently offline User OneFriend
Messages: 8
Registered: October 2017
Junior Member
Here is the class for 7-segment LED display (see attachments).
Here is how it is instantiated:
const byte pinSeg[7] = {2, 3, 4, 6, 7, 8, 9};
const byte pinPos[4] = {10, 11, 12, 13};
const byte pinCathode = A0;
const byte pinRed = 11;
const byte pinGreen = 13;
UVCounterDisplay<7, 4> disp(pinSeg, pinPos, pinRed, pinGreen, pinCathode);


When I move the definition of the method "display()" into a .cpp file the compiler starts to complain: "..undefined reference to `UVCounterDisplay<(unsigned char)7, (unsigned char)4>::display()'".
What makes this method special comparing to "setGreen()" and "setRed()" methods (which are defined in the .cpp file)?

[Updated on: Sat, 08 February 2020 13:14]

Report message to a moderator

Re: Why only some of the methods of a template are allowed to be defined outside of class? [message #1821256 is a reply to message #1821255] Sat, 08 February 2020 14:23 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
This forum is for dealing with issues using Eclipse/CDT.
It really isn't a forum on "how-to-program".


The short answer to your question is:
Template instantiation requires the complete definition of the template.
This can be tricky in a .cpp file.
Google "c++ template instantiation" for more.
Here's one response:
http://www.cplusplus.com/forum/articles/14272/

Basically, the following definition example must be available at the time of instantiation otherwise any attempt to use it will result in "undefined reference" link errors.
template <typename T>
void myTemplate<T>::displayData() const
{
    std::cout << data <<std::endl;
}


One way to solve this is to include UVCounterDisplay.cpp whenever you want to use one of the templates.
Usually, this file would have a .tpp extent to emphasize how it should be used.


[Updated on: Sat, 08 February 2020 14:41]

Report message to a moderator

Re: Why only some of the methods of a template are allowed to be defined outside of class? [message #1821258 is a reply to message #1821256] Sat, 08 February 2020 19:13 Go to previous messageGo to next message
User One is currently offline User OneFriend
Messages: 8
Registered: October 2017
Junior Member
Quote:
This forum is for dealing with issues using Eclipse/CDT.
It really isn't a forum on "how-to-program".

The question was not about "How to program" but "why this certain IDE treats similar methods differently". As I expect it must detect either all methods or none of them.

After compiling of similar code on GNU/Linux I have found that the reason was: I didn't had a call of methods "setGreen()" and "setRed()" in main loop. If I had one, the same error would appear for them.
Anyway, it looks weird in 2020 to have difficulties in finding of method's definition.

[Updated on: Sat, 08 February 2020 19:18]

Report message to a moderator

Re: Why only some of the methods of a template are allowed to be defined outside of class? [message #1821260 is a reply to message #1821258] Sun, 09 February 2020 05:33 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
You do understand that Eclipse is neither compiling nor linking any of your code, yes?
That job is relegated to the compiler and linker.
the ..undefined reference to `UVCounterDisplay<(unsigned char)7, (unsigned char)4>::display() from your first post is a linker error.
Has nothing to do with Eclipse.
Try compiling and linking outside of Eclipse and you will get the same results.

It's caused by the compiler not having the complete template definitions for the class methods.
So it assumes they were instantiated elsewhere (i.e., external) and generates only the call.

You need to understand that a template is just that: a template.
In some cases, such as with assembly code, they would be called macros.
They are patterns used to generate the actual code.
They have to be available to be expanded to the resulting code.

Don't blame the editing program for your coding errors.

What I described to you to fix it is required by C++
Your post was really about how to program in C++



[Updated on: Sun, 09 February 2020 10:29]

Report message to a moderator

Re: Why only some of the methods of a template are allowed to be defined outside of class? [message #1821262 is a reply to message #1821260] Sun, 09 February 2020 10:25 Go to previous messageGo to next message
User One is currently offline User OneFriend
Messages: 8
Registered: October 2017
Junior Member
David Vavra wrote on Sun, 09 February 2020 05:33
You do understand that Eclipse is neither compiling nor linking any of your code, yes?

As a java guy I know that IDE DOES generate parameters for compiler. One of the critical parameter is called "classpath", which sometimes actually causes problem with dependencies. Another critical parameter is version of language standard (different versions support different features). So my natural suggestion was that there is a similar to "classpath" or "language version" problem in C++.
I couldn't imagine than in 2019 the compiler can be so stupid. In fact, in Java we have generics since 2004.

[Updated on: Sun, 09 February 2020 10:28]

Report message to a moderator

Re: Why only some of the methods of a template are allowed to be defined outside of class? [message #1821263 is a reply to message #1821262] Sun, 09 February 2020 10:35 Go to previous messageGo to next message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
Quote:
IDE DOES generate parameters for compiler


More like CAN generate.
Eclipse only does this if you ask if to generate a makefile or its equivalent.
You can always write your own makefile and Eclipse will pass it to make or whatever.

Quote:
I couldn't imagine than in 2019 the compiler can be so stupid


A newbie outlook.
C++ solved the problem a different way which works so there's no need to change it.
As a programmer, you need to understand the tools you have chosen to use.
If you don't like C++ and like Java instead, well, then don't use C++.

[Updated on: Sun, 09 February 2020 10:46]

Report message to a moderator

Re: Why only some of the methods of a template are allowed to be defined outside of class? [message #1821265 is a reply to message #1821263] Sun, 09 February 2020 15:40 Go to previous messageGo to next message
User One is currently offline User OneFriend
Messages: 8
Registered: October 2017
Junior Member
[quote title=David Vavra wrote on Sun, 09 February 2020 10:35]Quote:
A newbie outlook.
C++ solved the problem a different way which works so there's no need to change it.

Solved the problem? Really? There is no problem in java to expose an interface and hide the implementation. The article you pointed above says I have to eat what I was given (and the explicit instantiation mentioned there doesn't work for me).
Quote:
If you don't like C++ and like Java instead, well, then don't use C++.

Wish i do. Would you point me to a family of low cost low power MCUs that run a human-friendly programming language?

[Updated on: Sun, 09 February 2020 15:43]

Report message to a moderator

Re: Why only some of the methods of a template are allowed to be defined outside of class? [message #1821267 is a reply to message #1821265] Sun, 09 February 2020 18:45 Go to previous message
David VavraFriend
Messages: 1426
Registered: October 2012
Senior Member
Yes, solved the problem of handling template (macro) expansions.
Too bad they didn't consult with you first.
There was an early attempt to automatically locate the templates.
It used the 'export' keyword.
Few compiler writers implemented it so it was removed from the standard in 2011 because it was unnecessary.

Quote:
point me to a family of low cost low power MCUs that run a human-friendly programming language


"human friendly"?
No CPU runs on any programming language.
Instead all CPUs use combinations of bits to specify what is to be done.
Programming languages are for humans to make programming "human friendly".

In any case, this is way off topic for this forum.
Take it up on reddit.



[Updated on: Sun, 09 February 2020 21:31]

Report message to a moderator

Previous Topic:Debugging ncurses application in Eclipse CDT (Linux)
Next Topic:Adding a new number format
Goto Forum:
  


Current Time: Thu Mar 28 10:49:02 GMT 2024

Powered by FUDForum. Page generated in 0.04524 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top