I think what you're looking for is:
std::string Test = "hello world";
char* NewVar = Test.c_str();
Although I think you'd be better off using an std::stringstream.
I think what you're looking for is:
std::string Test = "hello world";
char* NewVar = Test.c_str();
Although I think you'd be better off using an std::stringstream.
You can use the STL set (#include <set>).
http://www.cplusplus.com/reference/stl/set/
For union, just add all of the elements to the set and it will take care of removing duplicates.
Grr I always forget about const_iterators - that did it, thanks!
The problem is on the "MyIter = " line:
class OrientedPoint
{
.... class variables ...
std::map<std::string, double> DoubleValues;
.....
bool OrientedPoint::getDoubleValue(const std::string &ValueName, double &Value) const
{
std::map<std::string, double>::iterator MyIter;
MyIter = DoubleValues.find(ValueName);
The error produced is:
In member function 'bool OrientedPoint::getDoubleValue(const std::string&, double&) const':
error: no match for 'operator=' in 'MyIter = ((const OrientedPoint*)this)->OrientedPoint::DoubleValues.std::map<_Key, _Tp, _Compare, _Alloc>::find [with _Key = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _Tp = double, _Compare = std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, _Alloc = std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> >](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::string*)ValueName)))'
note: candidates are: std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> >& std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> >::operator=(const std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, double> >&)
I setup an identical situation in a demo project and it seems to compile fine... does anyone see that there would be a problem with this? I just can't decypher the stl error as usual... haha
Thanks,
Dave
hm, you are correct.
The problem is then not related to this thread, so I have started a new one:
http://www.daniweb.com/forums/post917709.html#post917709
Is there a way (without overloading) to call a function like this
GetValue("one");
//AND
std::string test("one");
GetValue(test);
The function is simply:
int GetValue(const std::string &MyString)
{
return MyMap[MyString];
}
This overload does the job:
int GetValue(const char* MyString)
{
return MyMap[MyString];
}
But that seems a little annoying to have to do. Any suggestions?
Thanks
Dave
What do you mean "return only one of the values"? A function can only return one value, and it looks like yours is... maybe you can give an example input, expected output, and current output?
Please use code tags. You can find what you're looking for here:
http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/
I agree that it's generally a very bad idea - but if you DO have a good reason, compile with -Wno
It is much more reasonable to note which statements need to be converted, and then ask google how to convert them. If google doesn't know (!?) then we'll help you.
What made you think to ask this in a c++ forum?? haha
Ah that seems pretty reasonable actually - I never logout when I am done. I'll try doing that and see if it changes the behavior and report the results.
I think you need to display the plot in a separate thread. This operation will vary from OS to OS.
No - it's just some ridiculous fear of them I have haha. I understand the syntax and semantics of them, but if I started coding I would never say MyClass* A;
before MyClass A;
. I guess if you start from the ground up it may be ok, but if I already have a lot of code that depends on receiving real objects, not just pointers, then it is a bear to have to go through and change everything to pointers - know what I mean?
I see, it's a scope problem. So why isn't there something like this:
Employee emp;
if (condition1)
emp.setClass(Employee());
else if (condition2) {
emp.setClass(Manager());
I mean clearly that is not a real syntax, but then there are no scope problems and you still didn't have to use pointers.
I don't understand why can you do this:
Employee *emp;
if (condition1)
emp = new Employee();
else if (condition2)
emp = new Manager();
but not this:
Employee emp;
if (condition1)
emp = Employee();
else if (condition2) {
emp = Manager();
Maybe this is a TERRIBLE idea, but it seems to me if you don't use pointers then you never have memory leak type problems... but the language disallows you from NOT using pointers in cases like this.
Any comments?
Thanks,
Dave
You should use vectors instead of arrays.
I realize this is quite an odd/complicated problem, but can you reduce the amount of code necessary to produce it? Maybe you can play around and remove as many lines as possible and still produce the error. Often that will help you isolate the bug yourself, or if not, then it's much easier for us to dig in.
Thanks guys.
Siddhant3s - yes of course Rotate is too vague - I just made up a dummy example for the sake of argument.
StuXYZ - what was one of the original tenants? Only write code once and then reuse it?
So the answer is, in general, just use epsilon tests?
I'm trying to start writing tests for all of my functions as per Test Driven Development. My question is, say I construct a test for "rotate vector". It would be something like this:
int TestRotateVector(const Vector &V)
{
Vector Original(1.0, 2.0);
Vector Rotated = Original.Rotate(10); //rotate 10 degrees
if(Rotated == Vector(2.34, 5.67)) //...whatever the correct answer should be
return 0; //pass
else
return -1; //fail
}
So my question is, do I have to use epsilon tests to test this kind of equality - i.e. if( fabs(Rotated.x - 2.34) < epsilon && fabs(Rotated.y - 5.67) < epsilon)
? Or is there a better way to write test functions?
Thanks,
Dave
oh oh, what was I thinking hahaha, you can "return" the values by reference! What you'll need to do is put a '&' here:
void getSales(double &sales, double salesTotal)
and the same with bonus:
void calcBonus(const double RATE, double sales, double &bonus)
Well that's not gona work unless you make everything global, which is a TERRIBLE idea. displayBonus() wont work for the same reason unless you call it from calcBonus(). I duno what to tell you - the assignment is just wrong i guess :(
I don't understand, you are saying if it equals zero then "it is not a number"? Also, you can use this std::string Zero = "0";
then if(Zero.compare(buf))
actually, i think compare() returns the edit distance, so you need to see if it equals 0, so more like if(Zero.compare(buf) == 0)
The getSales() function shouldn't be void - it needs to return the sales!
Can someone comment at least if this happens to them also even if they don't know the solution?
You clearly have not read any of the forum rules...
Unfortunately I don't use windows so I can't try to help, but I have a hard time believing that error can't be produced in < 20 lines...
Please use code tags and post the smallest fully compilable example that demonstrates the problem.
Please use code tags - then the smiley faces won't happen, indentation will be correct, and syntax highlighting will happen.
First, that is a linker error - it seems you have not defined ProcessChoice(int) in an appropriate place. Also, surely 99% of those lines of code have nothing to do with the problem. Pare down the code to about 20 lines that produces the same problem and then we can take a look. You may also want to use code=cplusplus so syntax highlighting gets used.
Is there some minimum amount of time that has to pass before thread subscription will "instantly notify" me? For example (and this happens a lot) I post a question, someone answered and I received an email about this answer. Then I replied. Then someone else replied, and I didn't receive an email - am I misinterpreting the expected behavior?
Thanks,
Dave
Haha sorry, I totally flaked on that post - I've been looking at both of these feature and I posted the wrong one! I can't find an actual paper on this one, but here is what I saw:
http://www.research.att.com/~bs/C++0xFAQ.html#member-init
On an unrelated note: is there some minimum amount of time that has to pass before thread subscription will "instantly notify" me? For example (and this happens a lot) I posted a question, you answered and I received an email about this answer. Then I replied. Then you replied, and I didn't receive an email - am I misinterpreting the expected behavior?
Thanks,
Dave
It's part of the new "c++0x standard" called "delegated constructors".
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1986.pdf
i.e.
class A
{
int x = 5;
};
I saw a mailing list post from October 08 where a guy asked if this was going to be added. gcc 4.4 was released April 09, so I would assume it would have been added by then? I haven't tried the svn version, but can anyone comment about if this works for them yet?
Thanks,
Dave
Can you extract the problem into ~ 20 lines? Maybe make a hard coded name/date/account and then just call the function that is returning the wrong value?
you should use code=cplusplus rather than code=cpp because that doesn't work! also, if you don't have / know how to use a debugger, then you should do "printf debugging" which means outputting text so you can tell where the program crashes. That will probably help you find the segfault.
What is it supposed to do. Maybe you can give sample input, current (incorrect) output, and expected output.
It doesn't sound like the problem has anything to do with tic-tac-toe. Can you please make a much, much smaller demonstration of your problem along with input, expected output, and current (incorrect) output.
Shivi, it may also be easier to post the very smallest compilable example that produces an error directly on the forum using 'code' tags rather than creating an attachment. This way some users can potentially see the problem right away without having to download anything.
It looks like you have not defined the type/class 'X'.
You could use std::vector's of std::vector's - then you don't have to worry about new() and delete().
You are correct that in standard c++ you cannot overload a function by return type alone.
peter, please use descriptive thread titles in the future :)
the compare() function is just like "==" for std::strings.
I often zero pad things like this:
std::string ZeroPad(const char num, const unsigned int rep)
{
std::stringstream Filled;
Filled << std::setfill('0') << std::setw(rep) << num;
return Filled.str();
}
but maybe inserting a '0' like MosaicFuneral said is more appropriate in this case?
And, clearly, if you're having a hard time with anything in particular, ask us here!
Surely the source code is not 18MB, try deleting the files that Ancient Dragon mentioned.
Perfect - thanks!
If I do something like this:
print "%08d" % 2
It will print '00000002'. However, I want to change the "8" to a "3" at runtime (to get '002' instead). I tried this:
MyLength = 3
print "%0" + str(MyLength) + "d" % 2
but I get: "TypeError: not all arguments converted during string formatting"
Can anyone see what's wrong with that?
Thanks,
Dave