Nominate our 2022 Qt Champions!

[SOLVED] Link error - Undefined reference

  • Hello, I am a new QTcreator user and I can't compile a simple program,

    I have my class defined and implemented in polynomial.h and polynomial.cpp
    I have also main.cpp which (is supposed to) instantiate my class TPolynomial.

    When I build I get the error:
    undefined reference to TPolynomial<int>::TPolynomial()

    I have found similar topic so I tried to clean, run qmake and rebuild, Qt creates main.o and polynomial.o, they both are in the "build folder" with the make file but it doesn't work, it seems not to find the polynomial.o?

    And when I write the implementation of my class in the header it works, so the problem is that Qt doesn't look for in the good folder.

    Do I have to specify a folder somewhere in the options?
    I don't think because all the files are in the same folder?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    No, it's not a problem of Qt, you are implementing a template class. See the C++ Super FAQ about the template

  • @SGaist

    Thank you for the link.

    Under the implementation of the template method, I tried to add the line:
    "template void freePolynomial<int>();"

    I get the error message:
    "variable or field 'freePolynomial' declared void"

    Then I tried to add "external" before the declaration of the method in the header,
    I get the error message:
    "storage class specified for 'freePolynomial'"

    So I have no idea what to do!

  • Lifetime Qt Champion

    @Calivernon Did you check the link provided by SGaist? Templates MUST be defined in the header file. So, you cannot put the definition in a .cpp file.

  • @jsulm

    Hi, yes I tried what they say in the link but I got an error message.
    I wrote in my last message what I did and the message I got.

  • Lifetime Qt Champion

    Can you share your code ? Without it it's pretty much Crystal Ball Debugging.

  • @SGaist

    Hello sorry but I am writing my class in the header, it's a pity but it works.
    If I have time I try again.

  • It's alright, I started a new project and I don't really know why but it works now.
    Thanks guys.

  • Lifetime Qt Champion

    That's a good news but still a strange mystery…

    By the way, no need to modify the thread title, you can mark it as solved using the "Topic Tool" button :)

Log in to reply