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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:37:04+00:00 2026-05-15T22:37:04+00:00

I am trying to print a value of an array element as cout <<

  • 0

I am trying to print a value of an array element as cout << array[0], (where the array is some glorified class using operator[]), but the C++ typing system seems incredibly confusing. The GCC error is this:

example.cpp:44:20: error: no match for ‘operator<<’ in ‘std::cout << a_0.fixedarr<T, N>::operator[] [with T = int, long unsigned int N = 5ul, size_t = long unsigned int](0ul)’

(The entire source comes from something more complicated, but I think I’ve pared it down to a minimal example).

#include <assert.h>
#include <cassert>
#include <climits>
#include <cstdio>
#include <iostream>

using namespace std;

template<typename T>
class fixedarrRef{
    T* ref;
    int sz;
    public:
        fixedarrRef(T* t, int psz){ ref = t; sz = psz;}

        T val(){ return ref[0]; }
};

template<typename T, size_t N>
class fixedarr{
    public:
        T arr[N];
        fixedarr(){
            for(int i=0; i<N; ++i){
                arr[i] = 0;
            }
        }
        inline fixedarrRef<T> operator[] (const size_t i) const{
            assert ( i < N);
            return fixedarrRef<T>((T*)&arr[i], N-i);
        }
};

template <typename T>
ostream & operator << (ostream &out, fixedarrRef<T> &v)
{
    return (out << v.val());
}   

int main() {
    fixedarr<int, 5> a_0;
    fixedarrRef<int> r = a_0[0];
    cout << (a_0[0]) << endl;
    // cout << r << endl;
    return 0;
}

Note that the commented code at the end works. Thanks in advance.

  • 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-15T22:37:04+00:00Added an answer on May 15, 2026 at 10:37 pm

    You should declare both T fixedarrRef::val() and fixedarrRef<T> &v in operator << const.

    T val() const { return ref[0]; }
    

    and

    template <typename T>
    ostream & operator << (ostream &out, const fixedarrRef<T> &v)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an element in my array that when I print out the value
I'm trying to detect when an array element is null, but is_null isn't doing
I'm trying to iterate through an array of objects and recursively print out each
I am trying to find the location of an element in the array. I
I am trying to read JSON objects using the QScriptValue class in Qt and
I am trying to create a Hash that has as its value an array.
I am trying to implement a dynamic array: template <typename Item> class Array {
I'm trying to get specific value by array name: <?php $json = json_decode($_POST['json'], true);
I am trying to edit an array element as to convert a format of
import xlrd book = xlrd.open_workbook(File_1.xls) sheet = book.sheet_by_index(0) print sheet.row_values(0)[0] I am trying to

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.