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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:30:07+00:00 2026-05-28T00:30:07+00:00

I am writing a templatized Matrix class, and I include the declarations in Matrix.h

  • 0

I am writing a templatized Matrix class, and I include the declarations in Matrix.h, the implementation in Matrix.cpp. I have a test file testMatrix.cpp.

The beginning of the files look like this:

Matrix.h

#ifndef _MATRIX
#define _MATRIX
#include <string.h>
// Definition of Matrix class
#endif

Matrix.cpp

#include "Matrix.h"
#include <iostream>
using namespace std;
// ...Implementation

testMatrix.cpp

#include "Matrix.h"
#include <iostream>
using namespace std;
int main () {
  cout << "Test constructors...\n";
  cout << "Unitialized matrix (4, 4):\n";
  Matrix<int> mi1 (4, 4);
  mi1.print ();
  cout << "4*4 matrix initialized to -1:\n";
  Matrix<int> mi2 (4, 4, -1);
  mi2.print();                                                                                                                                    
  cout << "Constructing mi3 as a copy of mi2:\n";
  Matrix<int> mi3 (mi2);
mi3.print();                                                                                                                                    
  cout << "Assigning mi3 to mi1:\n";
  mi1 = mi3; 
mi1.print();                                                                                                                                    

  return 0;
}

The command line I used to compile:

g++ -Wall -lrt -g Matrix.cpp testMatrix.cpp -o testMatrix

The compiler keeps giving me the error:

/tmp/ccofoNiO.o: In function `main':
/afs/ir/users/t/i/tianxind/practice/Essential_C++/testMatrix.cpp:9: undefined reference to `Matrix<int>::print() const'
collect2: ld returned 1 exit status

Does anyone have an idea what went wrong here? Thanks so much!

  • 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-28T00:30:08+00:00Added an answer on May 28, 2026 at 12:30 am

    The implementation of template functions/classes has to be visible to the compiler at the point of instantiation. This means that you should put the whole templates in the header file, not in a .cpp file.

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

Sidebar

Related Questions

Writing a file utility to strip out all non-ASCII characters from files. I have
Writing a test app to emulate PIO lines, I have a very simple Python/Tk
I'm writing a logging class that uses a templatized operator<< function. I'm specializing the
I'm writing a generic wrapper class for a bunch of classes we have defined
I have been writing several class templates that contain nested iterator classes, for which
Writing a lexer of .java source files in Java. I have a stream of
So in writing a C++ template class, I have defined a method that returns
Writing some test scripts in IronPython, I want to verify whether a window is
Writing some classes for a Framework extension, and I have the following code: public
I'm writing a templated C++ generic container class that can optionally maintain its contents

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.