GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Oct-2009, 06:31
bunsann bunsann is offline
New Member
 
Join Date: Oct 2009
Posts: 3
bunsann is on a distinguished road
Unhappy

C++ error C2784 occurred in structure and STL containers


Basically, one statement causes 9 errors.

The followings are my codes:


CPP / C++ / C Code:
#include <iostream>
#include <string>
#include <map> // for map<template>
#include <vector> 
#include <fstream> // for file io
#include <utility>
#include <cctype>
#include <algorithm> // for make_pair in map
#include <sstream> // for istringstream
using namespace std;


struct intersection {
	string road_one;
	string road_two;
};

struct neighbor {
	neighbor() { previous = -1; } // default constructor
	
	neighbor(intersection nb, double dis, double cs, int prev)
		: neighborToBe(nb), distance(dis), curSpeed(cs), previous(prev)
	{}

	intersection neighborToBe;
	double distance;
	double curSpeed;
	int previous; /* store index of vector<neighborMap>; 
				     eg: 0 refers to 1st St & 101st Ave intersection
					 eg: 12 refers to Freeway South @ 103rd Ave 
					 while -1 is undefined */
};

typedef map<intersection,neighbor> neighborMap;

class Graph {
public:

	/* vector< map<vertex, neighbor(neighborToBe, distance, curSpeed) */
	vector<neighborMap> graph_vector;
};

Within main() functions I have:

CPP / C++ / C Code:
// declaration
	Graph g;

	neighborMap nbMap; // to be inserted into Graph object  (g)

	neighbor myNeighbor; // to be inserted into Graph object (g)

	vector<intersection> myInt_s; // to be inserted into neighbor object (myNeighbor) 
                                                  // and also neighborMap object (nbMap)   ->> this is when error will occur

	intersection ints; // is used to store various local intersection object
                                // and is to be inserted into vector of intersection (myInt_s)

	istringstream iss (line, istringstream::in); // to convert numbers in string to integers/doubles

        /* in some functions, i push several intersection objects into myInt_s within a loop*/
        /* below is the code snippet */

		string a = line.substr(0, int(amp)-1);
		string b = line.substr(int(amp)+2);
		ints.road_one = a;
		ints.road_two = b;

		myInt_s.push_back(ints);

         /* the above codes work find and i have tested the vector */

         /* then i read numbers from file using iss, which istringstream object 
             to build my neighbor object, which in turn will be inserted into neighborMap object */

                iss >> start >> end >> distance >> speed; // read numbers line
		myNeighbor.neighborToBe = myInt_s.at(end);
		myNeighbor.distance = distance;
		myNeighbor.curSpeed = speed; 

          /* then the Problem comes when I tried to insert one instance of intersection from 
           * vector myInt_s and a neighbor object into neighbor map object.
           * 
           * The following statement is supposed to get the instance of an intersection object 
           * at the 'start' position of vector myInt_s
           */

	nbMap.insert( make_pair(myInt_s.at(start), myNeighbor)); // ERROR
        g.graph_vector.push_back(nbMap);
							

Anyway, before posting this, I have spent 3 hours googling but to no avail. I landed in MSDN library website, which is somewhat helpful.

Here's their suggestion:

CPP / C++ / C Code:
// C2784.cpp
template<class T> class X {};
template<class T> void f(X<T>) {}

int main() {
   X<int> x;
   f(1);   // C2784

   // try the following line instead
   f(x);
}

I think it might be related to my problem but I could not figure out what goes wrong.

I would really appreciate it if you could kindly offer suggestions/hints urgently.
This project is due in 3 days and I haven't started on the main algorithm yet. :(

The errors generated:
Code:
Error 2 error C2784: 'bool std::operator <(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const intersection' c:\program files\microsoft visual studio 9.0\vc\include\functional 143 Djikstra Algorithm Error 3 error C2784: 'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)' : could not deduce template argument for 'const std::_Tree<_Traits> &' from 'const intersection' c:\program files\microsoft visual studio 9.0\vc\include\functional 143 Djikstra Algorithm Error 4 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const intersection' c:\program files\microsoft visual studio 9.0\vc\include\functional 143 Djikstra Algorithm Error 5 error C2784: 'bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'const intersection' c:\program files\microsoft visual studio 9.0\vc\include\functional 143 Djikstra Algorithm Error 6 error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'const intersection' c:\program files\microsoft visual studio 9.0\vc\include\functional 143 Djikstra Algorithm Error 7 error C2784: 'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const intersection' c:\program files\microsoft visual studio 9.0\vc\include\functional 143 Djikstra Algorithm Error 8 error C2784: 'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)' : could not deduce template argument for 'const std::_Revranit<_RanIt,_Base> &' from 'const intersection' c:\program files\microsoft visual studio 9.0\vc\include\functional 143 Djikstra Algorithm Error 9 error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'const intersection' c:\program files\microsoft visual studio 9.0\vc\include\functional 143 Djikstra Algorithm Error 10 error C2676: binary '<' : 'const intersection' does not define this operator or a conversion to a type acceptable to the predefined operator c:\program files\microsoft visual studio 9.0\vc\include\functional 143 Djikstra Algorithm
  #2  
Old 26-Oct-2009, 10:33
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ error C2784 occurred in structure and STL containers


Quote:
Originally Posted by bunsann
...
The errors generated:
Code:
Error 10 error C2676: binary '<' : 'const intersection' does not define this operator or a conversion to a type acceptable to the predefined operator...

A map stores its elements in sorted order. To be able to insert something in a map, you have to have a predicate. This is a way to tell the map constructor how you intend to compare two key values.

If the map key values are certain types of standard things (numeric data types or std::string objects, for example), you don't need to supply the predicate, since a map can use the default predicate that is already defined for any of those data types. (It uses the template class std::less<> ::operator() for the default predicate)

Since your key values are intersection objects, you must define a predicate that tells how to compare two objects of that type. The data type of the optional third argument of the map constructor is a class with a boolean () operator that defines the comparison condition. See Footnote.
CPP / C++ / C Code:
//
// The predicate class used for the map so that objects will be
//  inserted in ascending order of key values
//
class compare_intersections
{
    public:
        bool operator() (const intersection & i1, const intersection & i2)
        {
         // 
         //  Implement something so that the function returns true if
         //  the value of i1 is to be considered less than the value
         //  of i2
         //
        }

};
.
.
.
.

typedef map < intersection, neighbor, compare_intersections > neighborMap;
.
.
.




Regards,

Dave

Footnote: The comparison could be defined as a boolean function instead of using a class with a boolean functional operator. The syntax for casting this function into the correct data type to use as the third argument in the map constructor is kind of complicated-looking, but kind of fun (if you have a weird idea of fun), and is LAAEFTWSAY (Left As An Exercise For The Weird Students Among You.)
Last edited by davekw7x : 26-Oct-2009 at 11:13.
  #3  
Old 26-Oct-2009, 15:06
bunsann bunsann is offline
New Member
 
Join Date: Oct 2009
Posts: 3
bunsann is on a distinguished road

Re: C++ error C2784 occurred in structure and STL containers


Hey dave.. Thank you very much.. After defining the predicate, it works!

Anyway, some bugs occur when I try to insert objects into my neighborMap object. Kindly see the snippet below:

CPP / C++ / C Code:
while (numberOfRoadSegments != 0 && line.find("TRIPS") == string::npos) {
	istringstream iss (line, istringstream::in); // to convert numbers in string to integers/doubles
	// skip empty lines and lines with #
	if (line.find("#") != string::npos || line.empty())
		goto readAnotherLine;

	iss >> start >> end >> distance >> speed; // read numbers from string
							
	myNeighbor.neighborToBe = myInt_s.at(end);
	myNeighbor.distance = distance;
	myNeighbor.curSpeed = speed; 

	nbMap.insert( make_pair( myInt_s.at(start), myNeighbor));
	g.graph_vector.push_back(nbMap);
	cout << "size: " << nbMap.size() << endl;

	readAnotherLine:
		getline(inFile,line);
	}

I know that map container can only have unique keys. And I am trying to insert 40 objects (15 of them are unique) (neighborMap<intersection, neighbor>) into the map, but when I display all the objects inside the nbMap object, I only get 5 entries (instead of 15).
Am I missing something? Do I have to define anything else? (since map container can only have unique keys, and giving it intersection objects, it may be unable to verify if the new key is the same as the already existing keys). And it does not produce any compile time error.

Some Notes: The loop does run 40 times. When I tried to cout the info manually, without storing into any object, it produced correct result.

And I don't think the goto function interferes the operations. (I know goto function is bad programming practice).

I also tried various map.insert functions such as map.insert(make_pair...), map.insert(pair<... , ...> (....)), and map.insert(Typedef::value_tupe (....)).

Any prompt further help or suggestion would be highly appreciated.
  #4  
Old 26-Oct-2009, 15:36
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: C++ error C2784 occurred in structure and STL containers


Quote:
Originally Posted by bunsann
...
...suggestion....
I assume that you have a function that prints out the map contents.

So, if you think there is a problem with the map and its insert function:

Each time through the loop: show what you are inserting, then do the "insert" thing, and then display the map.

Regards,

Dave
  #5  
Old 27-Oct-2009, 10:55
bunsann bunsann is offline
New Member
 
Join Date: Oct 2009
Posts: 3
bunsann is on a distinguished road

Re: C++ error C2784 occurred in structure and STL containers


It's not that I used map even though I knew it was wrong, but I just realized it it was wrong and it should have 15 objects when I tried to insert it.

Anyway, I changed my implementation to multimap, and yeah it works right on the spot.

Now I can go on with my shortest-path algorithm.

I really appreciate your help, Dave.
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 03:10.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.