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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:41:34+00:00 2026-05-25T13:41:34+00:00

I have a problem with generics in c++.I have two Matrix.h and Matrix.cpp files.Here

  • 0

I have a problem with generics in c++.I have two Matrix.h and Matrix.cpp files.Here is files:

#pragma once
template<class T>
class Matrix
{
    public:
        static T** addSub(int size,T** firstMatrix,T** secondMatrix,int operation);
}

and Matrix.cpp

#include "Martix.h"
template<class T>
static T** Matrix<T>::addSub( int n,T **firstMatrix,T **secondMatrix,int operation)
{
    //variable for saving result operation
    T **result = new T*[n];

    //create result matrix
    for( int i=0;i<n;i++)
        result[i] = new T[n];

    //calculate result
    for( int i=0;i<n;i++)
        for(int j=0;j<n;j++)
            result[i][j] = 
            (operation == 1) ? firstMatrix[i][j] + secondMatrix[i][j]:
                                firstMatrix[i][j] - secondMatrix[i][j];

    return result;
}

when I run these I get the below error:

Error   1   error LNK2019: unresolved external symbol "public: static int * * __cdecl Matrix<int>::addSub(int,int * *,int * *,int)" (?addSub@?$Matrix@H@@SAPAPAHHPAPAH0H@Z) referenced in function "public: static int * * __cdecl Matrix<int>::strassenMultiply(int,int * *,int * *)" (?strassenMultiply@?$Matrix@H@@SAPAPAHHPAPAH0@Z) C:\Users\ba.mehrabi\Desktop\Matrix\matrixMultiplication\main.obj    matrixMultiplication

what is the problem?

  • 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-25T13:41:35+00:00Added an answer on May 25, 2026 at 1:41 pm

    Unfortunately, you can’t have the declaration of a template class in the *.cpp files. The full definition has to stay in the header file. This is a rule of many C++ compilers.

    See this: http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12

    1. A template is not a class or a function. A template is a “pattern”
      that the compiler uses to generate a family of classes or functions.
    2. In order for the compiler to generate the code, it must see both
      the template definition (not just declaration) and the specific
      types/whatever used to “fill in” the template. For example, if you’re
      trying to use a Foo, the compiler must see both the Foo template
      and the fact that you’re trying to make a specific Foo.
    3. Your compiler probably doesn’t remember the details of one .cpp
      file while it is compiling another .cpp file. It could, but most do
      not and if you are reading this FAQ, it almost definitely does not.
      BTW this is called the “separate compilation model.”

    The link has a workaround, but keep in mind that a template is a glorified macro so the header file is probably the best place for it.

    The poster of this SO post shows a demonstration of the workaround.

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

Sidebar

Related Questions

So, I have a little problem here. Suppose I have: public class Repository<TEntity> where
I have an abstract class BaseItem declared like this: public abstract class BaseItem {
I have some generics in the following code: public <T extends Comparable<T>> int insertionSort(T[]
I'll try to clarify by using the following example. In here I have two
I have two questions: Is there a need to use Interlocked class for accessing
Assume I have a template (called ExampleTemplate) that takes two arguments: a container type
i have a little big problem. My problem is the following. I´ve got two
I have a generic java class that stores comparables: public class MyGenericStorage<T extends Comparable<T>>
This time I have problem with virtual fields. I have core class for my
I have an interesting problem. I'd like to create a generic class that can

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.