Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6250879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:30:38+00:00 2026-05-24T13:30:38+00:00

File main2.cpp: #include poly2.h int main(){ Poly<int> cze; return 0; } File poly2.h: template

  • 0

File main2.cpp:

#include "poly2.h"

int main(){
 Poly<int> cze;
 return 0;
}

File poly2.h:

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

File poly2.cpp:

#include "poly2.h"

template<class T>
Poly<T>::Poly() {
}

Error during compilation:

src$ g++ poly2.cpp main2.cpp -o poly
/tmp/ccXvKH3H.o: In function `main':
main2.cpp:(.text+0x11): undefined reference to `Poly<int>::Poly()'
collect2: ld returned 1 exit status

I’m trying to compile above code, but there are errors during compilation. What should be fixed to compile constructor with template parameter?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:30:39+00:00Added an answer on May 24, 2026 at 1:30 pm

    The full template code must appear in one file. You cannot separate interface and implementation between multiple files like you would with a normal class. To get around this, many people write the class definition in a header and the implementation in another file, and include the implementation at the bottom of the header file. For example:

    Blah.h:

    #ifndef BLAH_H
    #define BLAH_H
    
    template<typename T>
    class Blah {
        void something();
    };
    
    #include "Blah.template"
    
    #endif
    

    Blah.template:

    template<typename T>
    Blah<T>::something() { }
    

    And the preprocessor will do the equivalent of a copy-paste of the contents of Blah.template into the Blah.h header, and everything will be in one file by the time the compiler sees it, and everyone’s happy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this C++ file (let's call it main.cpp ): #include <string> #include main.y.c
g++ -std=gnu++0x main.cpp In file included from main.cpp:6:0: CustArray.h: In constructor 'CustArray::CustArray()': CustArray.h:26:32: error:
I have the following source code : // main.cpp #include a.h int main() {
I have the following source code in main.cpp: #include <iostream> #include <iomanip> int main()
I am in linux. My Makefile file is this main2: main.cpp g++ -c $(LIBS)
In file main.cpp... #include pqueue.h struct nodeT; struct coordT { double x, y; };
Say you've got the following simple main.cpp file: #include <cstdlib> #include <iostream> #include <fstream>
I have a code that looks like this: int main () { fstream file;
In code: //file main.cpp LINT a = 12; LINT b = 3; a =
I currently have a program where my main code is in a file main.cpp.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.