Why am I having trouble compiling a templated class?

Related searches

I've been stuck on this code for a while and can't get it to compile, what exactly am I doing wrong? If there are bugs present when it compiles, please ignore them as I can fix that on my own. As of right now I'm just trying to get it to run. Thank you in advance.

#include <iostream>
#include <string.h>

//template <class t> class Matrix; //possible way of fixing the friend function.

using namespace std;
  template<class T, size_t NROWS, size_t NCOLS>
  std::ostream & operator<<(std::ostream &os, const Matrix<T,NROWS, NCOLS> &matrix);

template<class T, size_t NROWS = 1, size_t NCOLS = 1>
class Matrix{
  friend std::ostream &operator<< <>(std::ostream&os,const Matrix<T, NROWS, NCOLS> &matrix);

  T container[NROWS][NCOLS];

template<class T,size_t NROWS, size_t NCOLS>
  std::ostream &operator<<(std::ostream &os,const Matrix<T,NROWS,NCOLS>&matrix){
  for(size_t i=0;i<NROWS;++i){
    for(size_t j=0;j<NCOLS;++j){
      os  <<matrix.container[i][j]<<" ";
    os <<std::endl;
  os <<std::endl;

int main(){
  Matrix<float, 10, 5> mat;
  cout << mat;
  return 0;

The errors from the IDE I use are as follows:

main.cpp:8:51: error: no template named 'Matrix' std::ostream & operator<<(std::ostream &os, const Matrix &matrix);

main.cpp:15:24: error: no function template matches function template specialization 'operator<<' friend std::ostream &operator<< <>(std::ostream&os,const Matrix &matrix);

main.cpp:35:32: note: in instantiation of template class 'Matrix' requested here Matrix mat;

If you uncomment out line 4, and change it as follows, the code you have compiles:

template <class t, size_t, size_t> class Matrix; //possible way of fixing the friend function.

It seems your problem is that the forward-declared Matrix template parameters don't match the Matrix definition that comes later.

Also, although the code will compile after this fix, there is still a warning which you probably also want to fix:

In function 'std::ostream& operator<<(std::ostream&, const Matrix<T, NROWS, NCOLS>&)':
31:1: warning: no return statement in function returning non-void [-Wreturn-type]

How can I avoid linker errors with my template classes?, C++ FAQ, 35.20, Can the previous problem hurt me silently? Is it possible that Click here to go to the next FAQ in the "chain" of recent changes]. This answer will be updated due to C++11 extern template. Watch this Tell your C++ compiler which instantiations to make while it is compiling your template class's .cpp file. (If you've� For one thing, templates hurt the C++ compiler's ability to generate efficient code. It might surprise you to hear that, considering that C++ is "efficient" while functional languages like ML and Scheme are "inefficient," but it's true. C++ templates hide your intentions from the compiler: did you mean type abstraction?

#include <cstddef>
#include <iostream>

template<typename, std::size_t, std::size_t> class Matrix;

template<typename T, std::size_t NROWS, std::size_t NCOLS>
std::ostream& operator<<(std::ostream &os, Matrix<T, NROWS, NCOLS> const &matrix)
    for (std::size_t row{}; row < NROWS; ++row, os.put('\n'))
        for (std::size_t col{}; col < NCOLS; ++col)
            os << matrix.container[row][col] << ' ';
    return os.put('\n');

template<typename T, std::size_t NROWS = 1, std::size_t NCOLS = 1>
class Matrix {
    T container[NROWS][NCOLS] = {};
    friend std::ostream& operator<< <>(std::ostream&, Matrix<T, NROWS, NCOLS> const&);

int main()
    Matrix<float, 10, 5> mat;
    std::cout << mat;

Please get rid of the C header <string.h>.

Why am I getting errors when my template-derived-class uses a , Can the previous problem hurt me silently? Is it possible that the compiler will silently generate the wrong code? 35.21, How can I create a container-template that� Tell your C++ compiler which instantiations to make while it is compiling your template class's .cpp file. (If you've already read the previous FAQ, this answer is completely symmetric with that one, so you can probably skip this answer.) As an example, consider the header file Foo.h which contains the

You need to define Matrix before it's usage:

template<class T, size_t NROWS = 1, size_t NCOLS = 1>
class Matrix;

and add a return statement to operator<<, that returns os. You also do not need duplication of operator<< declaration, you can declare it only in a class body.

Templates, C++ FAQ, A template is not a class or a function. In order for the compiler to Reminder: Read the next two FAQs for some solutions to this problem. Same function with and without template c++,c++11 I am trying to understand a piece of code of C++11. A class contains 2 functions as shown below: class abc { public: void integerA(int x); template<typename typ> void integerA(typ x); }; I am unable to understand benefit of declaring 2 same functions. Why not declare only one template function?

C++: “undefined reference to” templated class function – Bytefreaks , In case you have a project where you use a templated class that is split not create any compilation/linking problems, as the linker will ignore� This article looks at three possible solutions with examples: You can create an object of a template class in the same source file where it is implemented, you can #include the source file that implements your template class in your client source file, and you can #include the source file that implements your template class (TestTemp.cpp) in

How to Define a Template Class in a .h File and Implement it in a , But when we implement this procedure for template classes, some compilation and linking problems will arise. Compilation Issue. Here is some sample code: Hide� An instantiated object of a templated class is called a specialization; the term specialization is useful to remember because it reminds us that the original class is a generic class, whereas a specific instantiation of a class is specialized for a single datatype (although it is possible to template multiple types).

Sample template class. Similar to template function we can also implement template class. Template classes can be made for data structures like an array, linked list, queue, stack, tree, etc. Template using one datatype: Below is the sample template class of stack with its basic functions push() and pop(). It can be used for any data type.

  • FWIW, there is no such thing as a templated class or temaplted function. There are class templates and function templates though.
  • Your code includes something labeled "possible way of fixing" -- why only possible? What's wrong with your own answer? (Keep following the error messages. You're close.)