Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Overloading operator << of nested class where enclosing class is a template

Hi guys,

I'm trying to overload the << operator for a nested class that lives inside an enclosing class which is templated.

The enclosing class is my attempt at implementing a std::vector, the nested class is an iterator.

The way I understood it, when overloading the << operator you are actually overloading the operator of the ostream object, not of your class, therefore you cannot implement it in your class. You must instead declare the operator in your class as a "friend" and implement it within the global scope.

Pseudo Code:

PHP Code

<?php 
template 
<typename T>
class 
Vec{
public:
  class 
iterator : std::iterator<...>{
    ...
    
friend std::ostream& operator<<(const std::ostream& os, const Vec<T>::iterator& it);
  }
}

/*global scope */
template <typename T>
std::ostream& operator<<(const std::ostream& os, const Vec<T>::iterator& it){
  
os << it->num;
  return 
os;
} 

Link to Full Code in Pastebin

The errors I am getting when compiling with VisualStudio

Code
use the 'typename' keyword to treat nontype "Vec<T>::iterator [with T=T]" as a type in a dependent context

'Vec<T>::iterator': dependent name is not a type

syntax error: identifier iterator

binary 'operator <<' has too few parameters

The thing I am struggling with is correctly typing the parameters for the overload function. I've tried many things but can't get it to work, or find relevant answers online, so any help would be greatly appreciated!
[Image: 0DvFxnA.gif]
Donate: 3ABKRghau93ziRaDfVwiiwtjky3i1gLrKn


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Will PattersonJan 21, 2022 02:13 AM 7 419 Jan 21, 2022 02:13 AM
Last Post: matias!
[C] by pmsSep 29, 2019 05:30 AM 1 249 Sep 29, 2019 05:30 AM
Last Post: YouthInAsia
[C] by F.A.S.TMar 8, 2019 12:38 PM 9 482 Mar 8, 2019 12:38 PM
Last Post: F.A.S.T