|
Hi, everyone!
Please look at the following codes. I think I overload the
operator clearly. But compiler still say that it is ambiguous.
Here are the source codes and related error message.
Source:
--------
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
class employee_data {
public:
employee_data() : name (""), skill(0), salary(0) {}
employee_data(string n, int s, long sa) :
name (n), skill (s), salary (sa) {}
string name;
int skill;
long salary;
friend ostream& operator<< (ostream& os, const employee_data& e);
};
ostream& operator<< (ostream& os, const employee_data& e) {
os << "employee: " << e.name << " " << e.skill << " " << e.salary;
return os;
}
class employee {
public:
employee (int i, employee_data e) :
identification_code (i), description (e) {}
int identification_code; // key expression to identify an employee
employee_data description;
bool operator< (const employee& e) const {
return identification_code < e.identification_code; }
};
int main()
{
set <employee, less<employee> > employee_set;
multiset <employee, less<employee> > employee_multiset;
map <int, employee_data, less<int> > employee_map;
multimap <int, employee_data, less<int> > employee_multimap;
employee_data ed1 ("john", 1, 5000);
employee_data ed2 ("tom", 5, 2000);
employee_data ed3 ("mary", 2, 3000);
employee e1 (1010, ed1);
employee e2 (2020, ed2);
employee e3 (3030, ed3);
pair<const int, employee_data> a[2] = { make_pair (2020, ed2),
make_pair (3030, ed3) };
employee_map.insert (&a[0], &a[2]);
employee_data d = employee_map[2020];
cout << d << endl;
return 1;
}
--------
Error message:
--------
C:\Program Files\Microsoft Visual Studio\MyProjects\testMap1\testMap1.cpp(86) : error C2593: 'operator <<' is ambiguous
--------
Thanks in advance,
George
|
|
|
|
|
Hi, everyone!
I have tried it in VC7.0 and Linux gcc. Both OK, something
wrong with VC6.0?
George
|
|
|
|
|
Templates with VC6 sort of stink. :/
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Thanks, Tim buddie!
George
|
|
|
|
|
It works OK in VC++6.0 sp5. Maybe you should consider installing that service pack.
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanks, Joaquín buddie!
I have installed VC7.0. I think I should give up
poor VC6.0.
Have a nice weekend,
George
|
|
|
|
|
I want my ATL component to expose a function that can accept string as an argument. What i am doing is:
STDMETHODIMP CProduction::SetCountry(BSTR name)
{
MesageBox(NULL, reinterpret_cast<char *>name, "Country" , MB_OK);
return S_OK;
}
i.e i accept string as BSTR in argument and display it in message box.
Similarly from the client side i am calling this function as
myobject.SetCountry("Indonesia");
The problem is that this is not working correctly. Both client and server program runs successfully. But the string is not correctly passed in the ATL exposed function. The MessageBox only show one character i.e "I"
Can any one tell me what is the problem and how can i acceept string in ATL exposed function. Currently i am using
[in] BSTR name.
Is this correct ?
|
|
|
|
|
That is correct, but to use it correctly you'll need to convert it, unless you're making a UNICODE build.
Check out the W2T macro;
{
USES_CONVERSION;
MessageBox(NULL,W2T(name),"Country",MB_OK);
}
will do what you want.
Steve S
[This signature space available for rent]
|
|
|
|
|
Hi, everyone!
Please look at the following source codes, I compile them
in VC6.0. I have tried map.insert(make_pair(...)) works OK.
But map.insert(interator, interator) is not working perperly.
How to resolve the trouble?
--------
#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
class employee_data {
public:
employee_data() : name (""), skill(0), salary(0) {}
employee_data(string n, int s, long sa) :
name (n), skill (s), salary (sa) {}
string name;
int skill;
long salary;
friend ostream& operator<< (ostream& os, const employee_data& e);
};
ostream& operator<< (ostream& os, const employee_data& e) {
os << "employee: " << e.name << " " << e.skill << " " << e.salary;
return os;
}
class employee {
public:
employee (int i, employee_data e) :
identification_code (i), description (e) {}
int identification_code; // key expression to identify an employee
employee_data description;
bool operator< (const employee& e) const {
return identification_code < e.identification_code; }
};
int main()
{
map <int, employee_data, less<int> > employee_map;
employee_data ed1 ("john", 1, 5000);
employee_data ed2 ("tom", 5, 2000);
employee_data ed3 ("mary", 2, 3000);
employee e1 (1010, ed1);
employee e2 (2020, ed2);
employee e3 (3030, ed3);
pair<int, employee_data> a[2] = { make_pair (2020, ed2),
make_pair (3030, ed3) };
employee_map.insert (&a[0], &a[2]);
return 1;
}
--------
Errors:
--------
C:\Program Files\Microsoft Visual Studio\MyProjects\testMap1\testMap1.cpp(75) : error C2664: 'class std::_Tree<int,struct
std::pair<int const ,class employee_data>,struct std::map<int,class employee_data,struct std::less<int>,class
std::allocator<cl
ass employee_data> >::_Kfn,struct std::less<int>,class std::allocator<class employee_data> >::iterator __thiscall
std::map<int,class employee_data,struct std::less<int>,class std::allocator<class employee_data> >::insert(class
std::_Tree<int,struct
std::pair<int const ,class employee_data>,struct std::map<int,class employee_data,struct std::less<int>,class
std::allocator<class employee_data> >::_Kfn,struct std::less<int>,class std::allocator<class employee_data> >::iterator,const
struct std::p
air<int const ,class employee_data> &)' : cannot convert parameter 1 from 'struct std::pair<int,class employee_data> *' to
'class std::_Tree<int,struct std::pair<int const ,class employee_data>,struct std::map<int,class employee_data,struct
std::les
s<int>,class std::allocator<class employee_data> >::_Kfn,struct std::less<int>,class std::allocator<class employee_data>
>::iterator'
No constructor could take the source type, or constructor overload resolution was ambiguous
--------
Thanks in advance,
George
|
|
|
|
|
A map stores values of type pair<const key,value) , which it is not what you're supplying. Replace the definition of a with
pair<<font color=#ff0000>const</font> int, employee_data> a[2] = ...
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanks, Joaquín buddie!
George
|
|
|
|
|
Hi, everyone!
Such are the source codes of STL,
--------
template<class RandomAccessIterator, class T>
RandomAccessIterator binary_search (RandomAccessIterator first,
RandomAccessIterator last,
const T& value,
random_access_iterator_tag) {
// more efficient, but less generic algorithm
}
--------
I found there is a type "random_access_iterator_tag" in the definition
of the function. But there is not related parameter names. I think the
reason is the that fourth parameter is not used in the function, hidden the
parameter name can avoid warning in some compilers.
Am I correct?
Thanks in advance,
Geroge
|
|
|
|
|
Yes, if you specify a variable type and do not name it, you will cause the instance to be unnamed, and therefore inaccessible, which will avoid warnings if you do not use it.
Christian
No offense, but I don't really want to encourage the creation of another VB developer.
- Larry Antram 22 Oct 2002
C# will attract all comers, where VB is for IT Journalists and managers - Michael
P Butler 05-12-2002
It'd probably be fairly easy to make a bot that'd post random stupid VB questions, and nobody would probably ever notice - benjymous - 21-Jan-2003
|
|
|
|
|
Thanks, Christian buddie!
George
|
|
|
|
|
Hi, everyone!
I meet with some trouble when debugging the following
codes dealing with overloading operator >> in STL.
I have tried my best to resolve it but still failed.
My IDE is VC6.0.
Source Codes:
--------
#include <iostream>
#include <map>
#include <set>
using namespace std;
class employee_data {
public:
employee_data() : name (""), skill(0), salary(0) {}
employee_data(string n, int s, long sa) :
name (n), skill (s), salary (sa) {}
string name;
int skill;
long salary;
friend ostream& operator<< (ostream& os, const employee_data& e);
};
ostream& operator<< (ostream& os, const employee_data& e) {
os << "employee: " << e.name << " " << e.skill << " " << e.salary;
return os;
}
class employee {
public:
employee (int i, employee_data e) :
identification_code (i), description (e) {}
int identification_code; // key expression to identify an employee
employee_data description;
bool operator< (const employee& e) const {
return identification_code < e.identification_code; }
};
int main()
{
set <employee, less<employee> > employee_set;
multiset <employee, less<employee> > employee_multiset;
employee_data ed1 ("john", 1, 5000);
employee_data ed2 ("tom", 5, 2000);
employee_data ed3 ("mary", 2, 3000);
employee e1 (1010, ed1);
employee e2 (2020, ed2);
employee e3 (3030, ed3);
pair<set <employee, less<employee> >::iterator, bool>
result = employee_set.insert (e1);
if (result.second) cout << "insert ok"; else cout << "not inserted";
cout << endl << (*result.first).description.name << endl;
result = employee_set.insert (e1);
if (result.second) cout << "insert ok"; else cout << "not inserted";
return 1;
}
--------
Error messages:
--------
C:\Program Files\Microsoft Visual Studio\MyProjects\testMap1\testMap1.cpp(22) : error C2679: binary '<<' : no operator
defined which takes a right-hand operand of type 'const class std::basic_string<char,struct std::char_traits<char>,class
std::allo
cator<char> >' (or there is no acceptable conversion)
C:\Program Files\Microsoft Visual Studio\MyProjects\testMap1\testMap1.cpp(56) : error C2679: binary '<<' : no operator
defined which takes a right-hand operand of type 'class std::basic_string<char,struct std::char_traits<char>,class
std::allocator<
char> >' (or there is no acceptable conversion)
--------
How to resolve them?
Thanks in advance,
George
|
|
|
|
|
You forgot to #include <string> .
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Thanks, Joaquín buddie!
George
|
|
|
|
|
Subject.
I just cant think of where to start from. I guess i should inherit WTL CTreeViewCtrl, and add functionality for parsing computers directory tree.
So would you be so kind to give me links to samples of doing that?
Any help would be appreciated, thanks.
Sincerely yours, Ilya Kalujny.
|
|
|
|
|
SHBrowseForFolder might work for you
|
|
|
|
|
Yeah, but isnt it modal?
When i try to use it like this:
BROWSEINFO bi = { 0 };
bi.lpszTitle = _T("Pick a Directory");
bi.hwndOwner = m_hWnd;
LPITEMIDLIST pidl = SHBrowseForFolder ( &bi );
if ( pidl != 0 )
{
// get the name of the folder
TCHAR path[MAX_PATH];
if ( SHGetPathFromIDList ( pidl, path ) )
{
}
// free memory used
IMalloc * imalloc = 0;
if ( SUCCEEDED( SHGetMalloc ( &imalloc )) )
{
imalloc->Free ( pidl );
imalloc->Release ( );
}
}
I Get a modal dialog that is definitely not what i want
Well, if only i had a possibility to modify that dialog somehow (ie. insert my onw controls and write according handlers) that would be nice, but how??
More hints pleaaaase
Sincerely yours, Ilya Kalujny.
|
|
|
|
|
This is a simple question, i hope that someone can answer. I have created an ATL projectand want to expose function from the interface. I want to accept string as an argument to this function. The problem is that i don't know how to give the input parameters. I am trying [in] char * num but it is not working. Can any one tell me how can i accept string ??
|
|
|
|
|
Conventionally, you use a BSTR type instead. This is a wide string (or UNICODE), and is dealt with in VC++ quite happily by using one of the wrapper classes _bstr_t or CComBSTR.
If you want to call your function from VB, then this is compatible.
Steve S
[This signature space available for rent]
|
|
|
|
|
How do I find the maximum value in a collection as well as the array (vector) index of that value?
e.g. a vector
[1, 5, 3, 6, 8, 2]
I want to find the largest value (8) but I also want to find its index (4).
I can use the max_element algorithm to get the value. What about the index?
Kevin
|
|
|
|
|
upper_bound should do the job
Ryan
They read good books, and quote, but never learn
a language other than the scream of rocket-burn.
Our straighter talk is drowned but ironclad;
elections, money, empire, oil and Dad.
|
|
|
|
|
|