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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:16:42+00:00 2026-05-17T03:16:42+00:00

These are my files: ——–[ c.hpp ]——– #ifndef _C #define _C #include<iostream> class C

  • 0

These are my files:

——–[ c.hpp ]——–

#ifndef _C
#define _C

#include<iostream>
class C
{
public:
    template<class CARTYPE> void call(CARTYPE& c);
};

#endif

——–[ c.cpp ]——–

#include "c.hpp"

template<class CARTYPE> void C::call(CARTYPE& c)
{
    //make use of c somewhere here
    std::cout<<"Car"<<std::endl;
}

——–[ v.cpp ]——–

class Merc
{};

——–[ main.cpp ]——–

#include "c.hpp"
#include "v.cpp"
//#include "c.cpp"

int main()
{
    Merc m; 
    C someCar;
    someCar.call(m);

}//main

I’m able to generate “.o” files for all the above files, with the command g++ -c main.cpp and g++ -c c.cpp and so on.
But when I try linking the “.o” files with g++ -o car c.o main.o v.o
I get this error:

main.o: In function `main':
main.cpp:(.text+0x17): undefined reference to `void C::call<Merc>(Merc&)'
collect2: ld returned 1 exit status

The error goes away when I uncomment the line #include “c.cpp” in main.cpp, but I feel it may be bad practice to include the cpp file this way. Am I doing this wrong? Is there a better way to cater to templated declarations while creating separate object files and linking them?
p.s: I’m using the templated function in a much more complex class structure. What is shown here is just a small example for the sake of showing you the kind of error I’m facing.

  • 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-17T03:16:43+00:00Added an answer on May 17, 2026 at 3:16 am

    A way to solve this problem is to

    a. remove ‘#include “c.hpp”‘ from c.cpp AND

    b. include ‘c.cpp’ at the end of ‘c.hpp’ (strange sounding ‘#include “c.pp”‘)

    This way the template definitions are availabe to each translation unit that includes ‘c.hpp’ without explicitly doing so in each .cpp file. This is called the ‘inclusion model’

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

Sidebar

Related Questions

please consider these files: p.h: #ifndef _p_h_ #define _p_h_ class p{ public: static void
I have these files test1.h extern int value; void inc_value(); int print_value(); test1.c #include
So I have these files: / | + include/ | | | + database.php
I have site with these files and foldes: /index.php ( that include php files
master branch has these files and folders (simplified): C:\Local\TickZoom\Project>ls file.txt name.txt public public branch
I have two php files: main.class.php form.class.php in these files there are two class
When obfuscating .class files annotations are removed. Is there a way to obfuscate these
I have these files board.class and x4.class (x4.class has main() method). To jar these
I have a folder with these files: alongfilename1.txt <--- created first alongfilename3.txt <--- created
On install I copy some files in 'System32' folder. When uninstalling these files are

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.