daviddoria 334 Posting Virtuoso Featured Poster

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.

daviddoria 334 Posting Virtuoso Featured Poster

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.

daviddoria 334 Posting Virtuoso Featured Poster

Grr I always forget about const_iterators - that did it, thanks!

daviddoria 334 Posting Virtuoso Featured Poster

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

daviddoria 334 Posting Virtuoso Featured Poster

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

daviddoria 334 Posting Virtuoso Featured Poster

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

daviddoria 334 Posting Virtuoso Featured Poster

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?

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags. You can find what you're looking for here:
http://www.learncpp.com/cpp-tutorial/93-overloading-the-io-operators/

daviddoria 334 Posting Virtuoso Featured Poster

I agree that it's generally a very bad idea - but if you DO have a good reason, compile with -Wno

daviddoria 334 Posting Virtuoso Featured Poster

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.

daviddoria 334 Posting Virtuoso Featured Poster

What made you think to ask this in a c++ forum?? haha

daviddoria 334 Posting Virtuoso Featured Poster

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.

daviddoria 334 Posting Virtuoso Featured Poster

I think you need to display the plot in a separate thread. This operation will vary from OS to OS.

daviddoria 334 Posting Virtuoso Featured Poster

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?

daviddoria 334 Posting Virtuoso Featured Poster

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.

daviddoria 334 Posting Virtuoso Featured Poster

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

daviddoria 334 Posting Virtuoso Featured Poster

You should use vectors instead of arrays.

daviddoria 334 Posting Virtuoso Featured Poster

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.

daviddoria 334 Posting Virtuoso Featured Poster

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?

daviddoria 334 Posting Virtuoso Featured Poster

So the answer is, in general, just use epsilon tests?

daviddoria 334 Posting Virtuoso Featured Poster

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

daviddoria 334 Posting Virtuoso Featured Poster

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)
daviddoria 334 Posting Virtuoso Featured Poster

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 :(

daviddoria 334 Posting Virtuoso Featured Poster

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)

daviddoria 334 Posting Virtuoso Featured Poster

The getSales() function shouldn't be void - it needs to return the sales!

daviddoria 334 Posting Virtuoso Featured Poster

Can someone comment at least if this happens to them also even if they don't know the solution?

daviddoria 334 Posting Virtuoso Featured Poster

You clearly have not read any of the forum rules...

daviddoria 334 Posting Virtuoso Featured Poster

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...

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags and post the smallest fully compilable example that demonstrates the problem.

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags - then the smiley faces won't happen, indentation will be correct, and syntax highlighting will happen.

daviddoria 334 Posting Virtuoso Featured Poster

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.

daviddoria 334 Posting Virtuoso Featured Poster

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

daviddoria 334 Posting Virtuoso Featured Poster

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

daviddoria 334 Posting Virtuoso Featured Poster

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

daviddoria 334 Posting Virtuoso Featured Poster

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

daviddoria 334 Posting Virtuoso Featured Poster

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?

daviddoria 334 Posting Virtuoso Featured Poster

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.

daviddoria 334 Posting Virtuoso Featured Poster

What is it supposed to do. Maybe you can give sample input, current (incorrect) output, and expected output.

daviddoria 334 Posting Virtuoso Featured Poster

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.

daviddoria 334 Posting Virtuoso Featured Poster

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.

daviddoria 334 Posting Virtuoso Featured Poster

It looks like you have not defined the type/class 'X'.

daviddoria 334 Posting Virtuoso Featured Poster

You could use std::vector's of std::vector's - then you don't have to worry about new() and delete().

daviddoria 334 Posting Virtuoso Featured Poster

You are correct that in standard c++ you cannot overload a function by return type alone.

daviddoria 334 Posting Virtuoso Featured Poster

peter, please use descriptive thread titles in the future :)

tux4life commented: Yes, same came to my mind :) +11
daviddoria 334 Posting Virtuoso Featured Poster

the compare() function is just like "==" for std::strings.

daviddoria 334 Posting Virtuoso Featured Poster

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?

daviddoria 334 Posting Virtuoso Featured Poster

And, clearly, if you're having a hard time with anything in particular, ask us here!

daviddoria 334 Posting Virtuoso Featured Poster

Surely the source code is not 18MB, try deleting the files that Ancient Dragon mentioned.

daviddoria 334 Posting Virtuoso Featured Poster

Perfect - thanks!

daviddoria 334 Posting Virtuoso Featured Poster

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