473,626 Members | 3,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>::iterat or p;

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

nodes.insert(ma ke_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\Visua l Studio
Projects\STL\ST L.cpp(43) : error C2784:
'std::basic_ist ream<_Elem,_Tra its> &std::operat or
(std::basic_i stream<_Elem,_T raits> &,const std::_Smanip<_A rg> &)' : could not deduce template argument for 'std::basic_ist ream<_Elem,_Tra its> &' from 'std::ostream' c:\Documents and Settings\david\ My Documents\Visua l Studio
Projects\STL\ST L.cpp(43) : error C2784:
'std::basic_ist ream<_Elem,_Tra its> &std::operat or(std::basic_i stream<_Elem,_T raits> &,const std::_Smanip<_A rg> &)' : could not deduce template argument for 'std::basic_ist ream<_Elem,_Tra its> &' from 'std::ostream'

Projects\STL\ST L.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 3503
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>::iterat or p;

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

nodes.insert(ma ke_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
3060
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 the context of a main window. The problem does not occur on HPUX or Linux. The following simple example code illustrates the problem and the work around I've come up with; However, I'd like, very much, to get rid of the kludgy work around....
117
7172
by: Peter Olcott | last post by:
www.halting-problem.com
9
2808
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
5199
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(); .... and then call the virtual method, why is it that the base class's method is called instead of the overridden method? How do I fix this if I don't know at runtime what the child class is? I'm using Activator.CreateInstance() to load the...
6
3799
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 is more that 1249 bytes, then the progress bar of the browser will move too slow and then displaying that the page not found!!!! If the message is less than or equal to 1249 then no problem.
16
4901
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 Microsoft must be installed on their servers. Now german Umlaute (ä, ü, ö) and quotes are returned incorrectly in SOAP fault responses. This can be easily verified: Implement the following in a web service method (just raises a SOAPException with a...
2
4544
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 set to...it is set to false. Can someone show me what I am doing wrong and tell me the correct way? Thank you. In the page load event, I am doing the following:
2
2227
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 std::cout; using std::cin; using std::endl; int num1 = 0;
2
3869
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 point out what I'm doing wrong? Many thanks, - Olumide
0
8266
marktang
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...
0
8638
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8365
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8505
tracyyun
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...
0
7196
agi2029
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...
1
6125
isladogs
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...
0
4092
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...
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.