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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:17:42+00:00 2026-06-06T16:17:42+00:00

I have a class that wraps logic for matrix management. Meanwhile I have a

  • 0

I have a class that wraps logic for matrix management. Meanwhile I have a set of methods specialized in handling some important time spending matrix manipulation operations (like LU factorization and so on).

The class uses the functions defined in that file. That file needs to have access to that class’ elements. I need to make those specialized methods friends of the above-mentioned class. This causes me to include one header in each other header.

My problem

The situation I described before is coded here as follows. The first code refers to mat.hpp.

#ifndef MAT_HPP
#define MAT_HPP
#include "operations.hpp"
namespace nsA {
template <typename T>
// Templated class because matrices can be real or complex
class mat {
   // Members...
   friend template <typename U> 
   void exec_lu(const mat<U>& in, const mat<U>& out);
   // Members...
} /* class end */
}
#endif
#endif

The second file is operations.hpp

#ifndef OPERATIONS_HPP
#define OPERATIONS_HPP
#include "mat.hpp"
namespace nsA {
namespace nsB {
template <typename T>
void exec_lu(const mat<T>& in, const mat<T>& out);
}
}
#endif

The problem is that the compiler starts complaining.

Note

Consider please that if I comment the friend declaration in mat.hpp but leave the inclusions, the compiler tells me that in ‘operations.hpp’ type mat is not defined!

If also comment the inclusion in mat.hpp and keep friend declaration commented as well, the compiler is ok!

How to work on this?

Thankyou

  • 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-06T16:17:45+00:00Added an answer on June 6, 2026 at 4:17 pm

    You can do this by just adding a couple of forward declarations… But you can even do better with a bit more code:

    template <typename T> class mat;
    template <typename T> void exec_lu( const mat<T>&, const mat<T>& );
    template <typename T>
    class mat {
       friend void exec_lu<T>( const mat<T>&, const mat<T>& );
    };
    template <typename T>
    void exec_lu( const mat<T>& a, const mat<T>& b ) { ... }
    

    The main difference between this approach and yours (other than fixing the syntax limitations) is that in this approach a single instantiation of exec_lu is granted access to mat<T>, in particular the instantiation that needs access to it. In your code (after fixing), all specializations of exec_lu would have access to any specialization of mat (i.e. exec_lu<int> could access mat<int> private members, but also mat<double>…) and you probably don’t want that.

    For a longer description of the different options to declare friends of a template, read this answer to a related question.

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

Sidebar

Related Questions

I have a timer class set up that is basically handling all of count
I have such a case: class Person #a class that wraps to some db
I have a type that wraps an IEnumerable<T> and contains some additional logic, implementing
I have a class that wraps List<> I have GetValue by index method: public
I have a class that wraps a List and adds functionality for dealing with
I have a custom iterator template class that wraps up a std::list iterator. In
I'm working on an asp.net MVC application. I have a class that wraps a
We have a class that wraps NSURLConnection. It accepts a block that it calls
I have a repository class that wraps my LINQ to SQL Data Context. The
I have class that extend FragmentActivity in it I add fragment to layout as

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.