By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,906 Members | 1,416 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,906 IT Pros & Developers. It's quick & easy.

STL / C2784 Problem

P: n/a
--------------------------------------------------------------------------------

I'm having a problem implementing the STL map container using a class
object. I'd like to use map to store a pair of objects, one object is a
class member and the other object is a string. Here's a simplified
version of the code that I wrote. Any guidance would be much
appreciated here. (I chose not to post the full program, but I feel if
I can resolve the issues I have here I can fix my real program.)

#include "stdafx.h"

#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <iomanip>

using namespace std ;

class Node {
public:
string node;
// string get() { return node; }
string get() const { return node; }
Node();
~Node();
};

Node::Node() {}
Node::~Node() {}
int _tmain()
{
map<Node, string> nodes;
map<Node, string>::iterator p;

string str("string");
string str2("string2");
string str3("string3");
string str4;
string str5;

nodes.insert(make_pair(str, str2));

p = nodes.begin();

str4 = p->first.get();
str5 = p->second;

if(str4 == str) {
cout >> "str4 is " >> p->first.get() >> " and str5 is " >> p->second >>
endl;
}

return 0;

}
Here's a short list of the 41 errors. I have removed most of the C2784
errors:
Compiling...
STL.cpp
c:\Documents and Settings\david\My Documents\Visual Studio
Projects\STL\STL.cpp(43) : error C2784:
'std::basic_istream<_Elem,_Traits> &std::operator
(std::basic_istream<_Elem,_Traits> &,const std::_Smanip<_Arg> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream' c:\Documents and Settings\david\My Documents\Visual Studio
Projects\STL\STL.cpp(43) : error C2784:
'std::basic_istream<_Elem,_Traits> &std::operator(std::basic_istream<_Elem,_Traits> &,const std::_Smanip<_Arg> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from 'std::ostream'

Projects\STL\STL.cpp(43) : error C2676: binary '>>' : 'std::ostream'
does not define this operator or a conversion to a type acceptable to
the predefined operator

Nov 10 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
da*******@gmail.com wrote:
--------------------------------------------------------------------------------

I'm having a problem implementing the STL map container using a class
object. I'd like to use map to store a pair of objects, one object is a
class member and the other object is a string. Here's a simplified
version of the code that I wrote. Any guidance would be much
appreciated here. (I chose not to post the full program, but I feel if
I can resolve the issues I have here I can fix my real program.)
Umm.. may I suggest you start with the basics instead of trying to eat
more than you can?
#include "stdafx.h"
Non standard. Remove that kind of things when you post here.
#include <iostream>
#include <fstream>
#include <map>
#include <string>
#include <iomanip>

using namespace std ;
http://www.parashift.com/c++-faq-lit....html#faq-27.5
class Node {
public:
string node;
// string get() { return node; }
string get() const { return node; }
This makes a copy of "node"; you don't need to make it const.

http://www.parashift.com/c++-faq-lit...html#faq-18.10
Node();
~Node();
};

Node::Node() {}
Node::~Node() {}
int _tmain()
Non standard.
{
map<Node, string> nodes;
Illegal. The key in a map must implement the comparison operator used
by the map. By default, the operator is <. So you should implement an
operator< which compares two Nodes.
map<Node, string>::iterator p;

string str("string");
string str2("string2");
string str3("string3");
string str4;
string str5;

nodes.insert(make_pair(str, str2));
Illegal. Cannot convert a std::string to a Node. What did you expect
this to do?
p = nodes.begin();

str4 = p->first.get();
str5 = p->second;

if(str4 == str) {
cout >> "str4 is " >> p->first.get() >> " and str5 is " >> p->second >>
endl;
std::cout only implements operator<<. What book are you reading?
}

return 0;

}


I don't think you are ready for these features. Go back to basics.
Jonathan

Nov 10 '05 #2

P: n/a
>
class Node {
public:
string node;
// string get() { return node; }
string get() const { return node; }

This makes a copy of "node"; you don't need to make it const.

http://www.parashift.com/c++-faq-lit...html#faq-18.10


Huh? He doesn't need to make it const but he surely should. In the terms
of the FAQ you quoted this method is an inspector and should be declared
const.

john
Nov 10 '05 #3

P: n/a
da*******@gmail.com wrote:
--------------------------------------------------------------------------------

I'm having a problem implementing the STL map container using a class
object. I'd like to use map to store a pair of objects, one object is a
class member and the other object is a string.
I think here is your confusion. You say a class member, I assume you
mean the string member of class Node. So your map contains two strings,
so it should be decalred

map<string, string> nodes;

not

map<Node, string> nodes;

But in any case the rest of your code shows the same confusion. So
decide, do you want to your map to include Nodes and strings or just
strings and strings.
Here's a simplified
version of the code that I wrote. Any guidance would be much
appreciated here. (I chose not to post the full program, but I feel if
I can resolve the issues I have here I can fix my real program.)


Almost always a bad idea to post a simplified version.

john
Nov 10 '05 #4

P: n/a
John Harrison wrote:
class Node {
public:
string node;
// string get() { return node; }
string get() const { return node; }

This makes a copy of "node"; you don't need to make it const.

http://www.parashift.com/c++-faq-lit...html#faq-18.10


Huh? He doesn't need to make it const but he surely should. In the terms
of the FAQ you quoted this method is an inspector and should be declared
const.


Funny how sometimes you think completly backward. I don't understand
why I wrote that, but I'll think of an excuse, don't worry :)

To the OP: this *is* a perfect case to make a member function const.
When a member function does not modify the visible state of a class, it
should be made const. Since get() only returns a copy, get() does not
modify the object and does not give any "handle" to its internal
information. So get() should be const (as you did).

Sorry for the troubles,
Jonathan

Nov 10 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.