473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

STL / C2784 Problem

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

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
4 3486
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
>
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bruce Davis | last post by:
I'm having a problem on windows (both 2000 and XP) with a multi-threaded tkinter gui application. The problem appears to be a deadlock condition when a child thread pops up a Pmw dialog window in...
117
by: Peter Olcott | last post by:
www.halting-problem.com
9
by: Mr X | last post by:
Can anyone tell how to fix the compile error for the program below? #include "iostream.h" #include <vector> #include <algorithm> #include <functional> #include <list> using namespace std;
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer says service packs marked as "critical" by...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
2
by: zelmila19 | last post by:
Hi am working on this program that will display the sum, difference, product and quotient of two numbers using the selection structures. This is what I did: #include <iostream> using...
2
by: Olumide | last post by:
Hello, I've got this nice inner class that I'm holds a set of "FrontPoint" objects as shown below. Unfortunately, the find and insert methods trigger massive C2784 errors. Would someone please...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.