STL / C2784 Problem

D

david.dfx

--------------------------------------------------------------------------------

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:
c:\Documents and Settings\david\My Documents\Visual Studio
Projects\STL\STL.cpp(43) : error C2784:
Projects\STL\STL.cpp(43) : error C2676: binary '>>' : 'std::eek:stream'
does not define this operator or a conversion to a type acceptable to
the predefined operator
 
J

Jonathan Mcdougall

--------------------------------------------------------------------------------

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-lite/coding-standards.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-lite/const-correctness.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
 
J

John Harrison

--------------------------------------------------------------------------------

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
 
J

Jonathan Mcdougall

John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,166
Messages
2,570,903
Members
47,444
Latest member
Michaeltoyler01

Latest Threads

Top