I have a base class which has a protected member:
map<mstring,mstring>
mstring is a derived class from std::string, which includes some
functionality to cope with MFC.
It works fine in the base class, but when I try to use it in a derived
class, it gives me an error message.
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\functional(139): error C2678: binary '<' : no operator
found which takes a left-hand operand of type 'const mstring' (or there
is no acceptable conversion)
In my class there is an operator < overload. I took that out to see it
that could be the problem, but I get the same message. I then tried a
test map<string,string> and I get the same message. I tried messing
with map < mstring, mstring > (adding the spaces, because sometimes
with a template of containers VC++ needs certain spaces). Nothing.
The funny thing is that if I use the mfc CString class instead, it
works, but I don't want to redo the extra functionality deriving from
CString.
Any help?
Thanks,
Jay 11 4298
Donkeylung wrote: C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const mstring' (or there is no acceptable conversion)
In my class there is an operator < overload.
Can you please show two things:
1) How you derive from std::string?
2) How do you implement operator < in mstring?
3) How do you use map in case of std::string possession?
1)
typedef class mfc_string :
public string
{
.....
}mstring;
2)
Here are the 2 operator < overloads (they originally did not take
const arguments either way, I still get the error)
mfc_string mfc_string::operator<(const mfc_string& p)
{
double left = atof(this->c_str());
double right = atof(p.c_str());
char enter_string[50];
sprintf(enter_string, "%f", left-right);
return mstring(enter_string);
}
mfc_string mfc_string::operator<(const double& right)
{
double left = atof(this->c_str());
char enter_string[50];
sprintf(enter_string, "%f", left-right);
return mstring(enter_string);
}
3) When I used map<string, string>, it was only for a test.
Another note, the error only occurs when I use the [] notation. It
doesn't matter if it is accessing a previously defined key(in the base
class, where it is fine) or if is creating a new key in the derived
class.
thanks,
jay
Donkeylung a écrit : mstring is a derived class from std::string
std::string isn't meant to be derived, it doesn't have any virtual function.
Thus you will probably have issues if you try doing that.
Donkeylung wrote: Another note, the error only occurs when I use the [] notation. It doesn't matter if it is accessing a previously defined key(in the base class, where it is fine) or if is creating a new key in the derived class.
Hmmm, what about inserting new pair? Does it work?
Can you please try the following code that's trying to model your
problem on your compiler. I have a slight guess that it is a bug/feature
of VC7.0.
#include <map>
#include <string>
typedef class mfc_string : public std::string
{
public:
int size;
} mstring;
int main()
{
std::map<mstring,mstring> a;
mstring b;
a[b].size = 1;
}//main
This code compiled correctly on compilers I currently have access to:
Comeau, GCC 3.4 (MinGW), VC++8.
If problem is in compiler we'll try to think of a workaround that could
be applied.
Donkeylung wrote: I have a base class which has a protected member: map<mstring,mstring> mstring is a derived class from std::string, which includes some functionality to cope with MFC.
It works fine in the base class, but when I try to use it in a derived class, it gives me an error message.
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\functional(139): error C2678: binary '<' : no operator found which takes a left-hand operand of type 'const mstring' (or there is no acceptable conversion)
Do you have:
bool operator<( const mstring&, const mString& );
or
bool mstring::operator<( const mString& )const;
In my class there is an operator < overload. I took that out to see it that could be the problem, but I get the same message. I then tried a test map<string,string> and I get the same message. I tried
Did you properly #include <string>, notice the lack of .h? You must have a
'using namespace std;' in there as well somewhere, just an assumption as
you've neglected to post a single line of actual code.
Jeff Flinn
loufoque wrote: Donkeylung a écrit : mstring is a derived class from std::string
std::string isn't meant to be derived, it doesn't have any virtual function. Thus you will probably have issues if you try doing that.
Interface derives even if parent has no virtual functions. Not having a
virtual functions, IMHO, is not a reason to rewrite all the work that's
been accomplished by STL creators for a sake of adding one-two-three
tiny functions.
On Thu, 02 Mar 2006 15:20:07 -0800, Aleksander Beluga
<ch*****@tigris.org> wrote: loufoque wrote: Donkeylung a écrit : mstring is a derived class from std::string
std::string isn't meant to be derived, it doesn't have any virtual function. Thus you will probably have issues if you try doing that.
Interface derives even if parent has no virtual functions. Not having a virtual functions, IMHO, is not a reason to rewrite all the work that's been accomplished by STL creators for a sake of adding one-two-three tiny functions.
Just be sure never to allow anyone to delete a mfc_string through a
pointer to std::string. Since std::string has no virtual destructor,
this would invoke undefined behavior.
--
Bob Hairgrove No**********@Home.com
Aleksander Beluga wrote: loufoque wrote: Donkeylung a écrit : mstring is a derived class from std::string
std::string isn't meant to be derived, it doesn't have any virtual function. Thus you will probably have issues if you try doing that.
Interface derives even if parent has no virtual functions. Not having a virtual functions, IMHO, is not a reason to rewrite all the work that's been accomplished by STL creators for a sake of adding one-two-three tiny functions.
It's unsafe if somebody tries to delete one through the base pointer.
IMO, it's better to have std::string as a member (which is the preferred
form of code reuse) and then provide a constructor from std::string, a
conversion operator to std::string, and wrap any other functions you
feel necessary. Yes, it's hassle, but it does prevent users from
inadvertently invoking undefined behaviour.
Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Bob Hairgrove wrote: Just be sure never to allow anyone to delete a mfc_string through a pointer to std::string. Since std::string has no virtual destructor, this would invoke undefined behavior.
Not even an UB, just a little memory leak :)
Aleksander Beluga wrote: Bob Hairgrove wrote:
Just be sure never to allow anyone to delete a mfc_string through a pointer to std::string. Since std::string has no virtual destructor, this would invoke undefined behavior.
Not even an UB, just a little memory leak :)
No - it is UB, but could very well result in a small memory leak. It
could also result in having your heap corrupted, in terminating your
program or in other less likely behaviours.
/Peter
On 2006-03-02, Donkeylung <jk******@yahoo.com> wrote: 1) typedef class mfc_string : public string { .... }mstring;
2) Here are the 2 operator < overloads (they originally did not take const arguments either way, I still get the error) mfc_string mfc_string::operator<(const mfc_string& p)
mfc_string mfc_string::operator<(const double& right)
You should return a bool from operator<, not a copy of the lesser
mfc_string. If (*this) is less than right, return true, otherwise return
false.
--
Neil Cerutti
I don't know what to expect right now, but we as players have to
do what we've got to do to make sure that the pot is spread
equally. --Jim Jackson This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: jmoy |
last post by:
I am a C programmer graduating to C++. As an exercise I wrote a
program to count the number of times that different words occur in a
text file. Though a hash table might have been a better choice,...
|
by: Saeed Amrollahi |
last post by:
Dear All C++ Programmers
Hello
I am Saeed Amrollahi. I am a software engineer in
Tehran Sewerage Company. I try to use std::map and map::find member
function. I
use Visual Studio .NET. my...
|
by: Peter Jansson |
last post by:
Hello,
I have the following code:
std::map<int,std::set<std::string> > k;
k="1234567890";
k="2345678901";
//...
std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)...
|
by: Erik Wikström |
last post by:
First of all, forgive me if this is the wrong place to ask this question,
if it's a stupid question (it's my second week with C++), or if this is
answered some place else (I've searched but not...
|
by: Dan Trowbridge |
last post by:
Hi everyone,
In my attempt to port code from VS 6.0 to VS.NET I had some code break along
the way, mostly due to not adhereing closely to the C++ standard. This may
be another instance but I...
|
by: marco_segurini |
last post by:
Hi,
At the moment I am using Visual Studio 2005 beta 1.
The following program does not compile using the debug configuration
setting the "Disable Language Extensions" flag to "Yes(/Za)" while...
|
by: kamaraj80 |
last post by:
Hi
I am using the std:: map as following.
typedef struct _SeatRowCols
{
long nSeatRow;
unsigned char ucSeatLetter;
}SeatRowCols;
typedef struct _NetData
|
by: F. Meyer |
last post by:
Hi, everybody
I'm working with STL container map to store an element. Everything
happens well when I've an simple class (whit no inheritance). But if
using inherited classes I got some estrangs...
|
by: DevNull |
last post by:
Hi there everyone,
I'm creating a very simple immediate mode command interpreter.
The final purpose is to provide a pluggable control and command
console for a MUD server I have written.
The...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |