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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:55:40+00:00 2026-05-31T23:55:40+00:00

I was trying to use a local class as a way to simulate local

  • 0

I was trying to use a local class as a way to simulate local functions. The local class defines several static helper functions, where each static functions needs to access a static array defined in the function scope. The thing is working for a normal function, but I have a linking error when using a template function. The code is:

#include <iostream>

double test_local (double x)
{
    static const double coeff[3]={ 0, 1, 2 };

    struct local_functions
    {
        static double f0 (double x)
        {
            static const double c0=coeff[0]+coeff[1];
            return c0+x;
        }

        static double f1 (double x)
        {
            static const double c1=coeff[1]+coeff[2];
            return c1+x;
        }

        static double f2 (double x)
        {
            static const double c2=coeff[2]+coeff[0];
            return c2+x;
        }      
    };

    return local_functions::f0(x)+local_functions::f1(x)+local_functions::f2(x);
}

template<class t>
t test_local_tmpl (t x)
{
    static const t coeff[3]={ 0, 1, 2 };

    struct local_functions
    {
        static t f0 (double x)
        {
            static const t c0=coeff[0]+coeff[1];
            return c0+x;
        }

        static t f1 (t x)
        {
            static const t c1=coeff[1]+coeff[2];
            return c1+x;
        }

        static t f2 (t x)
        {
            static const t c2=coeff[2]+coeff[0];
            return c2+x;
        }
    };

    return local_functions::f0(x)+local_functions::f1(x)+local_functions::f2(x);
}

int main (int argc, char** argv)
{
    double result=test_local (1e0);

    // uncommenting next line generates a linking error
    // double result_tmpl=test_local_tmpl (1e0);

    std::cout << result << std::endl;
    return 0;
}

the non template function works fine (it prints 9), while if I try to invoke the template version, it compiles fine but does not link under g++-4.6:

g++ -c -g local_class.cpp && g++ local_class.o -o local_class
Undefined symbols:
  "coeff", referenced from:
      double test_local_tmpl<double>(double)::local_functions::f2(double) in local_class.o
      double test_local_tmpl<double>(double)::local_functions::f2(double) in local_class.o
      double test_local_tmpl<double>(double)::local_functions::f1(double) in local_class.o
      double test_local_tmpl<double>(double)::local_functions::f1(double) in local_class.o
      double test_local_tmpl<double>(double)::local_functions::f0(double) in local_class.o
      double test_local_tmpl<double>(double)::local_functions::f0(double) in local_class.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Is this the expected beahaviour, i’m missing something or what?

  • 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-31T23:55:41+00:00Added an answer on May 31, 2026 at 11:55 pm

    This is a GCC bug. There is no error in your code. Please file a bug report.

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

Sidebar

Related Questions

I am trying to use a local class as a functor and get compiler
I'm trying to use a local c# app to pull some images off a
I'm trying to debug an ASP.NET webapp that's configured to Use Local IIS Web
I was trying use a set of filter functions to run the appropriate routine,
Trying to use an excpetion class which could provide location reference for XML parsing,
I am trying to use a font, that is not installed on my local
I'm trying to make a static library from a class but when trying to
So I'm trying to use the canonical path to access a sound file, but
i'm trying to connect to a database building a class connection()saved in local folder
I'm trying to use the HttpListener class in a C# application to have a

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.