I cannot compile the code which defines a std::map type consisting of
built in types and operator<< overload for std::map::value _type.
See the code below - I attach a full example.
Note: if I define map type with my new type (structure) everything is
OK. All compileres I've cheked report an error so I think it is a
problem with my code.
#include <algorithm>
#include <cstddef>
#include <fstream>
#include <iterator>
#include <map>
class A
{
struct S // behavior similar to built-in int
{
int i;
S( int _i ) : i( _i ) {}
};
typedef std::map< int, int Map1;
typedef std::map< int, S Map2;
// the function below cannot be found by
std::ostream_it erator< Map1::value_typ e >
friend std::ostream &operator<<( std::ostream &_ostr, const
Map1::value_typ e &_value )
{
return _ostr << _value.first << ' ' << _value.second;
}
friend std::ostream &operator<<( std::ostream &_ostr, const
Map2::value_typ e &_value )
{
return _ostr << _value.first << ' ' << _value.second.i ;
}
Map1 m_map1;
Map2 m_map2;
public:
A(
const int *_keyStart,
const int *_valueStart,
const std::size_t _size
)
{
for ( std::size_t i = 0; i < _size; i++ )
{
m_map1.insert( Map1::value_typ e( _keyStart[i], _valueStart[i] ) );
m_map2.insert( Map2::value_typ e( _keyStart[i],
S( _valueStart[i] ) ) );
}
}
~A()
{
std::ofstream out( "test" );
out << "#### Map1 ####" << std::endl;
// this call does not compile
std::copy(
m_map1.begin(),
m_map1.end(),
std::ostream_it erator< Map1::value_typ e >( out, "\n" )
);
out << "\n#### Map2 ####" << std::endl;
std::copy(
m_map2.begin(),
m_map2.end(),
std::ostream_it erator< Map2::value_typ e >( out, "\n" )
);
}
};
int main()
{
int tab1[] = { 1, 3, 5, 2, 0 };
int tab2[ sizeof tab1 / sizeof *tab1 ] = { 1, 2, 3, 4, 5 };
A a( tab1, tab2, sizeof tab1 / sizeof *tab1 );
} 5 3648
On 30 Maj, 09:03, krzysztof.kono. ..@gmail.com wrote:
I cannot compile the code which defines a std::map type consisting of
built in types and operator<< overload for std::map::value _type.
See the code below - I attach a full example.
Note: if I define map type with my new type (structure) everything is
OK. All compileres I've cheked report an error so I think it is a
problem with my code.
#include <algorithm>
#include <cstddef>
#include <fstream>
#include <iterator>
#include <map>
class A
{
struct S // behavior similar to built-in int
{
int i;
S( int _i ) : i( _i ) {}
};
typedef std::map< int, int Map1;
typedef std::map< int, S Map2;
// the function below cannot be found by
// std::ostream_it erator< Map1::value_typ e >
friend std::ostream &operator<<(
std::ostream &_ostr,
const Map1::value_typ e &_value
)
{
return _ostr << _value.first << ' ' << _value.second;
}
The type of Map1::value_typ e is int which means that you are trying to
overload the operator
std::ostream &operator<<(std ::ostream&, int)
To overload an operator at least one of the operands needs to be of
user-defined type (which is why it works with the second one).
--
Erik Wikström
Erik Wikström napisa³(a):
>
The type of Map1::value_typ e is int which means that you are trying to
overload the operator
std::ostream &operator<<(std ::ostream&, int)
To overload an operator at least one of the operands needs to be of
user-defined type (which is why it works with the second one).
--
Erik Wikström
No, 'Map1::value_ty pe' is of type 'std::pair< const int, int >', not
'int', so I am trying to define function:
std::ostream &operator<<( std::ostream&, const std::pair< const int,
int & )
If your statement would be true I got compilation error that
std::ostream &operator<<(std ::ostream&, int) is already defined but I
didn't. My problem is that std::ostream_it erator< Map1::value_typ e >
cannot find my definition of operator<<().
If I write the code below everything is OK (it can find my definition
of operator<<() )
for ( Map1::const_ite rator it = m_map1.begin(); it != m_map1.end(); it+
+ )
logfile << *it << std::endl; kr************* **@gmail.com wrote :
I cannot compile the code which defines a std::map type consisting of
built in types and operator<< overload for std::map::value _type.
See the code below - I attach a full example.
Note: if I define map type with my new type (structure) everything is
OK. All compileres I've cheked report an error so I think it is a
problem with my code.
[.. snip ..]
class A
{
struct S // behavior similar to built-in int
{
int i;
S( int _i ) : i( _i ) {}
};
typedef std::map< int, int Map1;
typedef std::map< int, S Map2;
friend std::ostream &operator<<( std::ostream &_ostr, const
Map1::value_typ e &_value )
{
return _ostr << _value.first << ' ' << _value.second;
}
Try not to define the operator<<() inside A. While the definition of
operator<<() injects that function in the enclosing namespace, it is
still in the lexical scope of the class itself, and it will therefore
not be found during overload resolution. The reason that it works when
you use 'A' or 'S' as template arguments for the map, your class *is*
searched because of ADL, and the correct operator<<() is found.
- Sylvester
Sylvester Hesp napisa (a):
Try not to define the operator<<() inside A. While the definition of
operator<<() injects that function in the enclosing namespace, it is
still in the lexical scope of the class itself, and it will therefore
not be found during overload resolution. The reason that it works when
you use 'A' or 'S' as template arguments for the map, your class *is*
searched because of ADL, and the correct operator<<() is found.
- Sylvester
Yes, I thought that ADL is reason in this case but I want to keep Map1
typedef private in class A so it implies operator<<() to be a friend
of class A.
Moreover, I need to declare operator<<() in std namespace.
Of course, there are work-arounds:
- use loop instead of std::copy and std::ostream_it erator
- make Map1 typedef public and define operator<<() in std namespace
I prefer first solution but it fits only the simple example I
provided. The main problem is still unresolved so I think I need to
dig in and understand ADL deeper and find a solution.
Thanks kr************* **@gmail.com wrote :
Sylvester Hesp napisa (a):
>Try not to define the operator<<() inside A. While the definition of operator<<() injects that function in the enclosing namespace, it is still in the lexical scope of the class itself, and it will therefore not be found during overload resolution. The reason that it works when you use 'A' or 'S' as template arguments for the map, your class *is* searched because of ADL, and the correct operator<<() is found.
- Sylvester
Yes, I thought that ADL is reason in this case but I want to keep Map1
typedef private in class A so it implies operator<<() to be a friend
of class A.
Moreover, I need to declare operator<<() in std namespace.
Yes indeed, I didn't even think of that.
>
Of course, there are work-arounds:
- use loop instead of std::copy and std::ostream_it erator
- make Map1 typedef public and define operator<<() in std namespace
And obviously:
- Use a user defined type that behaves like another (built-in) type,
like your 'A::S'.
I think I personally prefer that solution - you don't have to put
anything into the std namespace and you don't impose certain standard
unspecified behaviour with the standard types. What if another
programmer in your project tries to do something similar to what you're
doing? That is impossible because you have already put a definition for
operator<<(std: :map<int,int>) in the std namespace.
By using a custom type you keep things private to your class.
- Sylvester This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ivan Paganini |
last post by:
Hello all.
I'm trying to use a vector<double> to alocate de data that i need, and
I am passing by reference to the function to receive the values. The
code goes like this:
//code
void GenerateRandomPositions(vector<double>& posiX, vector<double>&
posiY, int nbneurons, int* sizespace)
{posiX.clear();
|
by: gg |
last post by:
If I have the following class -
template <class T>
class X {
public:
void dump(ostream & os) { // dump obj_ to os }
private:
T obj_;
};
|
by: Kenneth |
last post by:
<list> seems to be a powerful structure to store the related nodes in
memory for fast operations, but the examples I found are all related to
primitive type storage.
I'm doing a project on C++ with my defined classes to be added to
linked list structure so as to facilitate the operation of all
instances of defined classes. Is that possible to apply such classes to
<list> or <Vector> structure?
Thanks!
|
by: aarthi28 |
last post by:
Hi,
I have written this code, and at the end, I am trying to write a
vector of strings into a text file. However, my program is nor
compiling, and it gives me the following error when I try to write to
the file:
error C2679: binary '<<' : no operator found which takes a right-hand
operand of type 'std::string' (or there is no acceptable conversion)
I don't know what I am doing wrong. I have posted my entire program
|
by: ozizus |
last post by:
I overloaded operator << for STL map successfully:
template <typename T1, typename T2ostream & operator << (ostream &
o, map <T1,T2& m)
{
//code
}
the code works like a charm. Now, I want the same functionality for
multimap. Since their interface is same for the problem at hand, I
| |
by: arnuld |
last post by:
This works fine, I welcome any views/advices/coding-practices :)
/* C++ Primer - 4/e
*
* Exercise 8.9
* STATEMENT:
* write a program to store each line from a file into a
* vector<string>. Now, use istringstream to read read each line
* from the vector a word at a time.
|
by: subramanian100in |
last post by:
Consider the following piece of code:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <utility>
#include <iterator>
#include <algorithm>
int main()
|
by: barcaroller |
last post by:
In the boost::program_options tutorial, the author included the following
code:
cout << "Input files are: "
<< vm.as< vector<string()
<< "\n";
Basically, he is trying to print a vector of string, in one line. I could
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |