error message: "ambiguous overload for 'operator=' (operand types are 'std::string {aka std::basic_string<char>}' and 'Variant')"
can anyone advice me how to fix the string operator?
You forgot to tell the program how to handle the '=' operator when it is given a string and an instance of your class. Line 71 isn't your problem, it's Line 90 that's causing this error.
Get rid of this. It's a very bad idea to pass off ownership of dynamically allocated memory in an implicit cast operator. This will cause memory leaks pretty much every time this cast occurs.
The actual error you're getting is because string can be assigned by either another string, or from a char*. Your variant class can be implicitly cast to either of those, so the compiler doesn't know which one you want.
Removing the char* cast operator will not only make it clear that you are casting to a string... but also will avoid the memory leak
I think you have to get rid of both casts (to string and to char *)
You have 2 cast operators that can start with a Variant and automatically result in a string, but don't forget you're trying to assign a Variant in the first place!
So, in line 90, the question the compiler can't answer is this "Is b a Variant, a const char * or a string?"
I suggest adding functions that return string or const char *, instead of overloading the cast.
P.S. You need to #include <cstdlib> to use atof (suggest using strtod though).
sorry if i'm bored you with these, but you have true: i didn't read some information from the books or something :(
but how can i resulve these problem?
I see what you're trying to do now, also please ignore my first comment. That was just nonsense. Let's go back to @Computergeek01 -
The problem is that the string operator= accepts both const string& and const char * Variant can be cast to either of them, so how will the compiler decide which one you intend?
Your code for char * works, bacause there's no ambiguity. You can't automatically convert a string to a char *
It doesn't work the other way, because you can automatically convert a char * to a string - this leads to ambiguity.
Even operator double () sill cause a problem here.
Unfortunately, you can't overload operator= globally, which is what I think you would like to do (in terms of functionality).
Bottom line, I'm not sure this can be done this way.
please tell me how can i output string or char* without compilers enter in conflits... please?
yes you have right, now i have tested about the double, and i must use int for fix it ;)