Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] simple template class is not compiled
Forum Updated to NodeBB v4.3 + New Features

[Solved] simple template class is not compiled

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Galiego710
    wrote on last edited by
    #1

    Good day!
    I written simple template class, but it is not compiled in the current view:
    MyClass.h
    @#ifndef MYCLASS_H
    #define MYCLASS_H

    template <class T> class MyClass
    {
    public:
    MyClass();
    T x;
    };

    #endif // MYCLASS_H@
    MyClass.cpp
    @#include "MyClass.h"

    template < class T > MyClass<T>::MyClass()
    {
    x = 0;
    }
    @
    main.cpp
    @#include <QtCore/QCoreApplication>
    #include <QDebug>
    #include "MyClass.h"

    int main(int argc, char *argv[])
    {
    MyClass<int> a;
    a.x = 10;
    qDebug() << a.x;
    return 0;
    }@
    I get the next error:
    @D:..\template_class1\main.cpp:7: error: undefined reference to `MyClass<int>::MyClass()'@

    But if I write all in the one file then all of it is successfully compiled:
    @#include <QtCore/QCoreApplication>
    #include <QDebug>

    template <class T> class MyClass
    {
    public:
    MyClass();
    T x;
    };

    template < class T > MyClass<T>::MyClass()
    {
    x = 0;
    }

    int main(int argc, char *argv[])
    {
    MyClass<int> a;
    a.x = 10;
    qDebug() << a.x;
    return 0;
    }@

    Why is not compiled the first variant?
    Say me please, where is my error?

    ——————
    Best regards,
    Galiego710

    1 Reply Last reply
    0
    • ZlatomirZ Offline
      ZlatomirZ Offline
      Zlatomir
      wrote on last edited by
      #2

      Since a template is only a pattern to generate code in the first case the compiler can't "see" the implementation of your template class - you include the header and the compiler needs the implementation to generate the code for MyClass with int instead of T
      Read more "here":http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

      https://forum.qt.io/category/41/romanian

      1 Reply Last reply
      0
      • G Offline
        G Offline
        Galiego710
        wrote on last edited by
        #3

        I will read it.
        Thank you very much for fast answer and very useful link!

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved