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

  • SEARCH
  • Home
  • 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 8758079
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:27:20+00:00 2026-06-13T14:27:20+00:00

in foo.h : #ifndef FOO_H #define FOO_H enum Rat { A, B }; class

  • 0

in foo.h:

#ifndef FOO_H
#define FOO_H

enum Rat
{
    A,
    B
};

class Foo
{
public:
    template<Rat r>
    int aMemberFunc(int, int, int);
};

#endif

in foo.cpp:

#include "foo.h"

namespace {
template<Rat r>
int helper(int a, int b)
{
    return a+b*((int) r);
}
}

template<Rat r>
int Foo::aMemberFunc(int a, int b, int c)
{
    return a + helper<r>(b,c);
}

in main.cpp:

#include "foo.h"
#include <iostream>

using namespace std;

int main(void)
{
    Foo test;
    cout << test.aMemberFunc<B>(1,2,3) << endl;
}

I compile with g++ main.cpp foo.cpp and I get:

main.cpp:(.text+0x88): undefined reference to `int Foo::aMemberFunc<(Rat)1>(int, int, int)'
collect2: ld returned 1 exit status

I would prefer not to move stuff to the header because that brings along the helper and a lot of baggage, I tried to add a file fooimpl.cpp:

#include "foo.h"
#include "foo.cpp"

template int Foo::aMemberFunc<A>(int,int,int);
template int Foo::aMemberFunc<B>(int,int,int);

and then compile with g++ fooimpl.cpp main.cpp foo.cpp

This was per the suggestion of Dietmar (thanks!) but as soon as I add a function void rand(); in the header of foo.h and void rand() {} in foo.cpp the above trick yields this error:

foo.cpp:(.text+0x0): multiple definition of `Foo::rand()’
/tmp/ccoCtGMk.o:fooimpl.cpp:(.text+0x0): first defined here

how do i work around this?

  • 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-06-13T14:27:22+00:00Added an answer on June 13, 2026 at 2:27 pm

    You need to instantiate your function, not specialize it:

    #include "foo.h"
    #include "foo.cpp"
    
    template int Foo::aMemberFunc<A>(int,int,int);
    template int Foo::aMemberFunc<B>(int,int,int);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#ifndef IMAGEDATA_H #define IMAGEDATA_H #include <iostream> #include <vector> class ImageData { public: std::string foo;
FILE #1 (foo.h): #ifndef FOO_H_ #define FOO_H_ #include baseclass.h #include bar.h class Bar; class
#ifndef UNICODE #define UNICODE #endif #include <iostream> #include <Windows.h> #include <queue> using namespace std;
suppose I have a header foo.h like this: #ifndef FOO_H #define FOO_H #include <string>
#ifndef UNICODE #define UNICODE #endif #include <iostream> #include <Windows.h> #include <queue> using namespace std;
#ifndef UNICODE #define UNICODE #endif #include <iostream> #include <Windows.h> #include <queue> using namespace std;
#ifndef UNICODE #define UNICODE #endif #include <Windows.h> #include <cstdio> #include <fstream> using namespace std;
Foo.java public class Foo{ public int i = 0; } Bar.scala class Bar() extends
Given this code sample: complex.h : #ifndef COMPLEX_H #define COMPLEX_H #include <iostream> class Complex
I just created this new class : //------------------------------------------------------------------------------ #ifndef MULTITHREADEDVECTOR_H #define MULTITHREADEDVECTOR_H //------------------------------------------------------------------------------ #include

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.