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

  • Home
  • SEARCH
  • 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 8850585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:56:16+00:00 2026-06-14T12:56:16+00:00

I’m learning C++, and I’m running into a linking error when building my project.

  • 0

I’m learning C++, and I’m running into a linking error when building my project.

The error is associated with my code that is overloading the output << operator.

This is the error that I am getting:

1>lab4.obj : error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Matrix<2,2> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Matrix@$01$01@@@Z) referenced in function _main
1>c:\users\matt\documents\visual studio 2010\Projects\lab4\Debug\lab4.exe : fatal error LNK1120: 1 unresolved externals

How do I fix this error? I have attached my code below

#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

template <int m, int n> 
class Matrix{
public:
    int data[m][n];
public:
    Matrix(){};

    void setup(){
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                data[i][j] = 0;
            }
        }
    }

    void setAll(int integer){
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                data[i][j] = integer;
            }
        }
    }

    void output(){
        for(int i=0;i<m;i++){
            cout << "[ ";
            for(int j=0;j<n;j++){
                cout << data[i][j] << " ";
            }
            cout<<"]"<<endl;
        }
    }

    Matrix<m,n> operator+ (const Matrix<m,n> &rhs)
    {
        Matrix<m,n> result;

        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                result.data[i][j] = this->data[i][j] + rhs.data[i][j];
            }
        }

        return result;
    }

    Matrix<m,n> operator- (const Matrix<m,n> &rhs)
    {
        Matrix<m,n> result;

        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                result.data[i][j] = this->data[i][j] - rhs.data[i][j];
            }
        }

        return result;
    }

    template <int l>
    Matrix <m,l> operator*(const Matrix<n,l> &rhs) //const
    {
        //only if number of columns in the first matrix equals the number of rows in the second matrix. 
        Matrix<m,l> result;
        for(int i = 0; i < m; i++){
            for(int j = 0 ; j < l ; j++){
                result.data[i][j] = 0;
                for(int k = 0; k < n; k++){
                    result.data[i][j] += this->data[i][k] * rhs.data[k][j];
                }
            }
        }
        return result;
    };

    Matrix <m,n> operator*(int scalar){
        Matrix <m,n> result;
        for(int i= 0; i < m; i++){
            for(int j = 0; j < n; j++){
                result.data[i][j] = this->data[i][j] * scalar;
            }
        }
        return result;
    };

    friend ostream& operator<<(ostream &os, const Matrix<m,n> &rhs);
    friend Matrix <m,n> operator++(const Matrix<m,n> &rhs, int);
    friend Matrix <m,n> operator--(const Matrix<m,n> &rhs, int);
};

template <int m, int n> 
ostream& operator<< (ostream &os, const Matrix<m,n> &rhs){
    for(int i=0;i<m;i++){
        os << "[ ";
        for(int j=0;j<n;j++){
            os << rhs.data[i][j] << " ";
        }
        os<<"]"<<endl;
    }
    return os;
}

template <int m, int n> 
 Matrix <m,n> operator++(const Matrix<m,n> &rhs, int){
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            rhs.data[i][j]++;
        }
    }
}

template <int m, int n> 
 Matrix <m,n> operator--(const Matrix<m,n> &rhs, int){
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            rhs.data[i][j]--;
        }
    }
}

int main(){
    Matrix<2,2> m = Matrix<2,2>();
    m.setup();
    //m.output();

    Matrix<2,2> p = Matrix<2,2>();
    p.setup();

    Matrix<2,2> z = Matrix<2,2>();
    z = m + p;
    //z.output();

    z = z * z;
    cout << z;
    string stopper;
    getline(cin, stopper);
}
  • 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-06-14T12:56:17+00:00Added an answer on June 14, 2026 at 12:56 pm

    Is your whole source located really in a single file, as posted here?
    Are you aware, that you have to define function-members of a template class in its header? (actually, it’s just the simplest solution, there are some workarounds)

    Ref.: http://www.parashift.com/c++-faq/separate-template-fn-defn-from-decl.html

    If not, it can be the reason of an error.

    BTW, this source is erroneous:

    template <int m, int n> 
    ostream& operator<< (ostream &os, const Matrix<m,n> &rhs){
        for(int i=0;i<m;i++){
            os << "[ ";
            for(int j=0;j<n;j++){
                os << data[i][j] << " ";
            }
            os<<"]"<<endl;
        }
        return os;
    }
    

    data here is not declared, it must be referenced as rhs.data, since this operator<< is not a member of the class matrix.

    Upd.

    You have to declare all your friend functions inside the class as

    template <int f_m, int f_n> 
    friend ostream& operator<<(ostream &os, const Matrix<f_m,f_n> &rhs);
    

    They’re separate templates, their template args must be separate. It’s strange, that MSVC++ does not issue that error. gcc says:

    matrix.cc:91:67: warning: friend declaration «std::ostream& operator<<(std::ostream&, const Matrix<m, n>&)» declares a non-template function [-Wnon-template-friend]
    matrix.cc:91:67: note: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am currently running into a problem where an element is coming back from
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I know there's a lot of other questions out there that deal with this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.