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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:35:45+00:00 2026-05-24T05:35:45+00:00

This is slightly different than the question posed just below this one. Suppose I

  • 0

This is slightly different than the question posed just below this one. Suppose I have a container class which has two template parameters, the first of which is a type, the second of which is the size of the container.

Now we have multiple containers with a different container storage size. Essentially, the container functions (all the public ones, anyway) only really care about T; N is only used to allocate local storage (an allocator is used if N is not enough).

I have put together a simple example implementation that showcases the problem I am having.

#include <iostream>

template <typename T, size_t N = 10>
class TestArray
{
public:
    T Local[N];

    class Iterator
    {
    public:
        T* Array;
        int Index;      

        Iterator() : Array(NULL), Index(-1) { }
        Iterator(T* _array, int _index) : Array(_array), Index(_index) { }

        bool operator == (const Iterator& _other) const 
        { 
             return _other.Index == Index && _other.Array == Array; 
        }

        bool operator != (const Iterator& _other) const 
        { 
            return !(*this == _other); 
        }

        template <size_t _N>
        Iterator& operator = (const typename TestArray<T, _N>::Iterator &_other)
        {
            Array = _other.Array;
            Index = _other.Index;

            return *this;
        }

        void Next() { ++Index; }
        void Prev() { --Index; }

        T& Get() { return Array[Index]; }
    };

    T& operator [] (const int _index) { return Local[_index]; }

    Iterator Begin() { return Iterator(Local, 0); }
    Iterator End() { return Iterator(Local, N); }

    template <size_t _N>
    void Copy(const TestArray<T, _N> &_other, int _index, int _count)
    {   
        int i;

        for (i = 0; i < _count; i++)
            Local[_index + i] = _other.Local[i];
    }   
};

This is really a two part question. The first part of which I posted earlier: Template container with multiple template parameters interacting with other template containers with a different template parameter. For the second part, I’m trying to use it as follows:

int main() {

    TestArray<int> testArray1;
    TestArray<int, 25> testArray2;

    TestArray<int>::Iterator itr;

    itr = testArray1.Begin();

    for (itr = testArray1.Begin(); itr != testArray1.End(); itr.Next())
    {
        itr.Get() = itr1.Index;
    }

    testArray2.Copy(testArray1, 0, 10);

    for (itr = testArray2.Begin(); itr != testArray2.End(); itr.Next())
    {
        std::cout << itr.Get() << std::endl;
    }

    return 0;
}

Here is an IDEONE link: http://ideone.com/GlN54

When compiled with gcc-4.3.4, I get the following:

prog.cpp: In function ‘int main()’:
prog.cpp:67: error: no match for ‘operator=’ in ‘itr = testArray2.TestArray<T, N>::Begin [with T = int, unsigned int N = 25u]()’
prog.cpp:10: note: candidates are: TestArray<int, 10u>::Iterator& TestArray<int, 10u>::Iterator::operator=(const TestArray<int, 10u>::Iterator&)
prog.cpp:67: error: no match for ‘operator!=’ in ‘itr != testArray2.TestArray<T, N>::End [with T = int, unsigned int N = 25u]()’
prog.cpp:19: note: candidates are: bool TestArray<T, N>::Iterator::operator!=(const TestArray<T, N>::Iterator&) const [with T = int, unsigned int N = 10u]

In VS2010, I get the following:

1>------ Build started: Project: testunholytemplatemess, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(67): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'TestArray<T,N>::Iterator' (or there is no acceptable conversion)
1>          with
1>          [
1>              T=int,
1>              N=25
1>          ]
1>          c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(34): could be 'TestArray<T>::Iterator &TestArray<T>::Iterator::operator =(const TestArray<T>::Iterator &)'
1>          with
1>          [
1>              T=int
1>          ]
1>          while trying to match the argument list '(TestArray<T>::Iterator, TestArray<T,N>::Iterator)'
1>          with
1>          [
1>              T=int
1>          ]
1>c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(67): error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'TestArray<T,N>::Iterator' (or there is no acceptable conversion)
1>          with
1>          [
1>              T=int,
1>              N=25
1>          ]
1>          c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(19): could be 'bool TestArray<T>::Iterator::operator !=(const TestArray<T>::Iterator &) const'
1>          with
1>          [
1>              T=int
1>          ]
1>          while trying to match the argument list '(TestArray<T>::Iterator, TestArray<T,N>::Iterator)'
1>          with
1>          [
1>              T=int
1>          ]

I thought the Iterator& operator = would make it so that this assignment operator should work, but apparently not. Perhaps I am being thick but I am failing to determine the correct solution here. Does anyone have any suggestions?

  • 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-24T05:35:46+00:00Added an answer on May 24, 2026 at 5:35 am

    TestArray<T, 1> and TestArray<T, 2> are different types, and so are TestArray<T, 1>::Iterator and TestArray<T, 2>::Iterator. The assignment cannot work! (Your itr is a different type from the type of testArray2.Begin().)

    The entire construction seems very dubious to me — is this really necessary? What are you trying to achieve? Have you looked at std::array?


    Update: It works if you supply the template parameters explicitly:

      for (itr.operator=<int,25>(testArray2.Begin());
           itr.operator!=<int,25>(testArray2.End());
           itr.Next())
      {
        std::cout << itr.Get() << std::endl;
      }
    

    I’m not entirely sure why the parameters cannot be deduced from the arguments, and I am looking forward to a good explanation — in the meantime, I put the full code on Ideone, though it doesn’t compile there, but it does with my GCC 4.6.1.

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

Sidebar

Related Questions

This question has been asked before ( link ) but I have slightly different
I know this question has been done but I have a slightly different twist
We have a php/mysql based application which has slightly different instances for a number
I believe this question is slightly different than similar ones asked on here before
This question comes close to what I need, but my scenario is slightly different.
OK, this is a slightly weird question. We have a touch-screen application (i.e., no
I've seen second one in another's code and I suppose this length comparison have
This answer says that Linq is targeted at a slightly different group of developers
This is slightly off topic of programming but still has to do with my
This is a slightly tricky question. I am using NSDateFormatter on the iPhone but

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.