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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:43:20+00:00 2026-05-28T03:43:20+00:00

I am learning C++ and it hasn’t been an enjoyable experience (compared to Java

  • 0

I am learning C++ and it hasn’t been an enjoyable experience (compared to Java or VBA at least). I have the following code:

//This is in a number.h file
  #pragma once
  template <class T>
  class number{
        public: 
               T value1, value2, result; 
        public: 
               T add();
               number(T value1_in, T value2_in);
  };

 //This is in a number.cpp file
   template <class T>
   number<T>::number(T value1_in, T value2_in){
              value1 = value1_in;
              value2 = value2_in;
   }

   template <class T>
   T number<T>::add(){
   result = value1 + value2; 
   return result; 
   }

 //This is in main.cpp
   #include "number.h"
   #include <iostream>
   using namespace std;

   int main(){
       int a = 2, b =3;

       number<int> n1(a,b);
       cout << n1.add();
       system("pause");
       return EXIT_SUCCESS;
  }

Which of course gives me an error. Even though I am pretty sure it should work. More specifically I get a linker error. After 3 hours of looking at this I decided to include number.cpp in main.cpp and that magically made it work. What the hell is going on? I thought I only need to include the header file (I wrote a matrix class with a bunch of linear solvers for different algorithms before this and only included header files in the whole project). Is this C++ specific or compiler specific? (I am using Dev-C++ 4.9.9.2 which has Mingw I guess)

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

    You do not have to put the entire definition into the header file; don’t listen to what others tell you 🙂

    That is the conventional solution, and you and I will do it quite often, but it’s not the only solution.

    The other option is to simply place this line at the end of your number.cpp file, in order to force that particular template class to be instantiated and fully compiled there.

    template class number<int>;
    

    In short, there are two valid solutions. You can use this line, or you can copy the definition into the header file. Depending on context, one approach might be better than the other, but both are valid. See this answer of mine for a more comprehensive discussion of both approaches:

    https://stackoverflow.com/a/8752879/146041

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

Sidebar

Related Questions

It hasn't been long since I started learning python, but I really want to
I have an interesting situation that my usually clever mind hasn't been able to
Im learning lisp and im pretty new at this so i was wondering... if
Learning jquery, so please be kind :) Using PHP, I have a table with
Learning Ruby. Needed to create a hash of arrays. This works... but I don't
Learning CasperJS Trying to understand why the following is not displaying my results in
I'm learning Python from a book, and I came across this example: M =
Lately as part of my day job I've been learning IBM Rhapsody and using
I have a small application developed in ASP.NET 2.0 WebForms. For learning purpose, I
learning Jquery and integrating with PHP - getting there, but have one last challenge

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.