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 166609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:07:40+00:00 2026-05-11T12:07:40+00:00

I have a function declared like so: template <typename T> T read(); and defined

  • 0

I have a function declared like so:

template <typename T>  T read(); 

and defined like so:

template <typename T> T packetreader::read() {     offset += sizeof(T);     return *(T*)(buf+offset-sizeof(T));  } 

However, when I try to use it in my main() function:

packetreader reader; reader.read<int>(); 

I get the following error from g++:

g++ -o main main.o packet.o main.o: In function `main': main.cpp:(.text+0xcc): undefined reference to `int packetreader::read<int>()' collect2: ld returned 1 exit status make: *** [main] Error 1 

Can anyone point me into the right direction?

  • 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. 2026-05-11T12:07:41+00:00Added an answer on May 11, 2026 at 12:07 pm

    You need to use the export keyword. However, I don’t think G++ has proper support, so you need to include the template function’s definition in the header so the translation unit can use it. This is because the <int> ‘version’ of the template hasn’t been created, only the <typename T> ‘version.’

    An easy way is to #include the .cpp file. However, this can cause problems, e.g. when other functions are in the .cpp file. It will also likely increase the compile time.

    A clean way is to move your template functions into its own .cpp file, and include that in the header or use the export keyword and compile it separately.

    More information on why you should try and put template function definitions in its header file (and ignore export altogether).

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

Sidebar

Ask A Question

Stats

  • Questions 71k
  • Answers 71k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Many thanks to Larry for confirming this behaviour is by… May 11, 2026 at 1:21 pm
  • added an answer The * should work just fine. That means 'search all… May 11, 2026 at 1:21 pm
  • added an answer A binary search tree sounds like what you're looking for.… May 11, 2026 at 1:21 pm

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.