daviddoria 334 Posting Virtuoso Featured Poster

Hm, this might be the answer?
http://www.cplusplus.com/reference/std/iterator/

It looks like the assignment operator requirement is only for random access iterators?

daviddoria 334 Posting Virtuoso Featured Poster

I know there is an "assignable and copy constructable" requirement for objects in an STL container. For example, you cannot place an object with a const member into a vector.

#include <vector>
    
    // /home/doriad/Test/Test.cxx:3:8: error: non-static const member ‘const int MyClass::x’, can’t use default assignment operator
    
    // struct MyClass
    // {
    //   int const x;
    //   MyClass(int x): x(x) {}
    // };
    // 
    // int main()
    // {
    //   std::vector<MyClass> vec;
    //   vec.push_back(MyClass(3));
    //   return 0;
    // }

However, I noticed that it works with std::set:

#include <set>
    
    // Attempt 1
    struct MyClass
    {
      int const x;
      MyClass(int x): x(x) {}
      bool operator< (const MyClass &other) const;
    };
    
    bool MyClass::operator<(const MyClass &other) const
    {
      if(this->x < other.x)
      {
        return true;
      }
      else if (other.x < this->x)
      {
        return false;
      }
    
    }
        
    int main()
    {
      std::set<MyClass> container;
      container.insert(MyClass(3));
      return 0;
    }

Will this only work on some compilers? Or is this ok to do?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

I have a class called PatchCollection. It is in charge of creating a bunch of Patch objects. It stores them in a member variable of type std::set. Now I want to loop over all of the patches from outside the class. That is:

MyClass
{
 PatchCollection Patches;
 void DoSomething()
 {
   // Loop over all patches in Patches
 }
};

What is the "best" way to go about this? I've though of:

1) Of course I could make a std::set<Patch>& GetPatches() function, but then I need to know that the patches are stored in a std::set, which seems to violate some principles of OO (i.e. the calling function should not care about the internal details).

2) I could make a typedef of std::set<Patch> ContainerType. Then I could just use PatchCollection::ContainerType::iterator in the calling function. This is better than (1), but it still indicates an STL container is being used.

3) I could "reimplement" the std::set function - making a PatchCollection::GetIterator(), PatchCollection::NextItem(), etc - but this seems like a lot of duplication.

Any thoughts? What would you do :) ?

David

daviddoria 334 Posting Virtuoso Featured Poster

This is probably a better question for this forum:
http://www.opengl.org/discussion_boards/

daviddoria 334 Posting Virtuoso Featured Poster

You should look into fstream to write data to a file, and iostream to read input from the command line.

daviddoria 334 Posting Virtuoso Featured Poster

I would suggest making a very small (<20 lines) example of one piece of your program that is not working. In many cases, by breaking it down like this you will solve your problem on your own :)

daviddoria 334 Posting Virtuoso Featured Poster

I typically make a Types.h to define all of my types in and then include that in all of my project files. I haven't ever seen this anywhere though, so I assume it is "bad/wrong". The situation is often like this:

Consider a class Gardener that needs to know type of grass he will be cutting. Then consider a class Lawnmower that also needs to know what type of grass will be cut. What I would have done in the past is put typedef float GrassType; in Types.h and then included Types.h from Gardener.h and Lawnmower.h.

If, instead, I put the typedef in Gardener.h, then to access it from Lawnmower.h I would have to include Gardener.h, but since Gardener.h includes Lawnmower.h (so he can know what kind of lawnmower he has), that makes a circular dependency.

Is there a better way to do this than to introduce a template parameter to Lawnmower that can be set by the Gardener that owns it?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

I just watched this webcast: http://videos.webpronews.com/2011/05/31/daniweb-speaks-out-on-recovering-from-google-panda/

I'd like to follow up on the idea of no-indexing threads with zero replies. During the webcast it was stated that the idea was to not have people find a thread that has their exact question but no answer. In some cases, they will find a thread that has the answer elsewhere. In other cases, won't this just lead to the person posting the same question here (or elsewhere) because they don't think it has been asked before? It seems to me like we'd want them to find the post without an answer and then "vote it" to the top, or reply with "any thoughts?" so it gets bumped.

Can anyone explain further?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

Sure thing. It would be great if you could post your working code and then mark the thread as solved.

daviddoria 334 Posting Virtuoso Featured Poster

This seems like a totally separate question. You should start a new thread.

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags. I still don't understand why you are incrementing the lowerbound?

daviddoria 334 Posting Virtuoso Featured Poster

Take a read through this:
http://www.cplusplus.com/doc/tutorial/control/

Then give it another shot.

Please keep in mind my suggestion of:

I suggest replacing user input with hard coded values for testing. You should also tell us the expected output and the current output.

Then post your new code if it still doesn't work.

David

daviddoria 334 Posting Virtuoso Featured Poster

I suggest replacing user input with hard coded values for testing. You should also tell us the expected output and the current output.

Have you tried stepping through the code with a debugger? This would be where I would start.

Actually, I would start with this line:

sum = sum + upperbound++ + lowerbound++;

Yikes! Why are you incrementing these things in this expression? The bounds should stay the same, and a counter is what should increment.

There is also no need to sum from 1 to the lower bound and then 1 to the upper bound. Simply sum from the lower bound to the upper bound.

daviddoria 334 Posting Virtuoso Featured Poster

The key to recursion is defining a base case. Here the base case should be "making change for a penny?".

Take a look at this:
http://programmingexamples.net/index.php?title=CPP/recursion

and see if it points you in the right direction.

You'll probably need to define an array with the possible values of a coin:

int coins[5] = {50,25,10,5,1};

and then make the recursive function

MakeChange(unsigned int coinId)

David

daviddoria 334 Posting Virtuoso Featured Poster

When you output a pointer, you will certainly get its address. That's what a pointer is, just an address.

When you dereference the pointer and print that, you'd have to have the << operator defined.

friend std::ostream& operator<<(std::ostream& output,  const Point &P);

Here is a full example:
http://programmingexamples.net/index.php?title=CPP/OverloadOperator

daviddoria 334 Posting Virtuoso Featured Poster

Is there a problem? If so, please explain it. If not, this should be moved to Code Snippets.

daviddoria 334 Posting Virtuoso Featured Poster

They will all be geometric objects. The backstory is that this this for a generic Hough transform set of classes.

The most complicated functions would be trig functions.

daviddoria 334 Posting Virtuoso Featured Poster

L7Sqr, yes, a subclass will implement the Solve(params, paramToSolve) function, so some subclasses may have complicated equations. The best case is just to have the subclass specify the equation somehow and have the superclass do all of this business of solving for different variables, etc.

daviddoria 334 Posting Virtuoso Featured Poster

Hi all,

I am trying to write a function to solve an equation for one of its variables.

E.g.

X + Y + Z = 0

This equation would be represented by its parameters: parameters[0] is X, parameters[1] is Y, parameters[2] is Z.

If the user wants to solve for X, they would have to provide Y and Z. Likewise for solving for another parameter (if they want to solve Y, they must provide X and Z).

I am thinking of doing this by requiring them to pass an array of the values of X,Y, and Z, and the index of the one they want to solve for. For example, if they want to solve X, they would do:

parameters = {unused, 3.4, 5.6};
Solve(parameters, 0)

Is there some reasonable way of writing the internals of this function besides providing a giant switch statement?

if(paramToSolve == 0)
{
  X = -Y - Z;
}
else if(paramToSolve == 1)
{
 Y = -X - Z;
}
....

This gets really annoying when the model/equation gets bigger.

I am trying not to have to break out some big symbolic solver library or something like that, but it seems like that's basically what I'm asking for :)

Thoughts?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

You should make the title more descriptive - like "Qt linker errors". Also, you will probably have better luck here: http://www.qtforum.org/

daviddoria 334 Posting Virtuoso Featured Poster

Whenever you're having an error you can't track down, the first thing I would suggest is to remove all user input and hardcode values that you know the correct behavior of. You should then try using a debugger to step through the code while watching the variables to see if you can tell where something is going wrong. Finally, if you can't find it, tell us the expected output and the current output for the simple case that you have hard coded.

Also, please use code tags.

David

daviddoria 334 Posting Virtuoso Featured Poster

This seems like a better question for a Microsoft forum. It is not really c++ related.

daviddoria 334 Posting Virtuoso Featured Poster

You could make a character array and then create random numbers:

http://programmingexamples.net/index.php?title=CPP/RandomNumbers

as indices to the array and output the character at the random index. This would all be done in a loop.

daviddoria 334 Posting Virtuoso Featured Poster

You should output the values used in the conditionals. This will show you why the "else"s aren't being hit.

std::cout << playerTotal + playerAceValue << std::endl;
if ((playerTotal + playerAceValue) > (21))

My guess is that you see something > 21.

Also, instead of generating random values, you should should hard code some values for testing. Create a case where the deal wins, another where the player wins, etc. and make sure the program operates correctly.

joru819 commented: fast and helpfull response! +1
daviddoria 334 Posting Virtuoso Featured Poster

My only guess would be: LecERDClick(this) but you'd probably be better off asking whoever wrote this.

daviddoria 334 Posting Virtuoso Featured Poster

Please use code tags. It doesn't tell you which symbol is multiply defined?

daviddoria 334 Posting Virtuoso Featured Poster

Which line is it crashing on? Have you check to make sure all of the pointers are non-null?

daviddoria 334 Posting Virtuoso Featured Poster

Please explain what is going wrong. If possible, try to shorten the code and have it still produce the problem.

daviddoria 334 Posting Virtuoso Featured Poster

Give it a shot and we'll help you if you get stuck.

daviddoria 334 Posting Virtuoso Featured Poster

You'll probably have better luck here: http://tech.groups.yahoo.com/group/OpenCV/

(Their data structures are really wacky - good luck :) )

daviddoria 334 Posting Virtuoso Featured Poster

I'd bet you Qt has some widgets that are searchable. I've never used them, but it might be worth looking into.

Also, are you sure you need to do this in c++? People tell me that perl is the way to go for string manipulation/searching/etc.

daviddoria 334 Posting Virtuoso Featured Poster

Sounds like fun! I'll tell you what, since it's so much fun, I'll let you give it a try first! When you get stuck (show us the code and a specific problem you're having, precisely stated), we'll chime in.

daviddoria 334 Posting Virtuoso Featured Poster

I think you will have better luck here: http://www.qtforum.org/

daviddoria 334 Posting Virtuoso Featured Poster

Thanks guys!

daviddoria 334 Posting Virtuoso Featured Poster

I was under the impression that you could completely gaurd anything you wanted with an #if 0 . Apparently this is not the case?

I found some code that has opening /* comments but no matching closing */, and the /* seems to comment out the #endif , resulting in everything breaking:

#if 0
  if ( operatorMap[ops].first && !operatorMap[ops].second.first )
  {
    /** Example: /path/SINimage.mhd *
    outputFileName = path + ops + name + ext;
#endif

Is this expected? Is there an even stronger "definitely ignore everything between these tags"?

David

daviddoria 334 Posting Virtuoso Featured Poster

I strongly suggest breaking down your problem into tiny pieces. People are much more willing to help you solve generally useful problems rather than your specific problem.

For example, if you start a thread asking "How do I erase a line from the terminal?" you will likely get some answers, whereas if you ask "Here is my 3000 line program, what am I doing wrong?" you will likely not get any answers.

ThingsThatNeverChange.h seems like a bad idea - it contains so many global variables! You should at least encapsulate these in a Settings class or something like that.

Dexxta27 commented: good guidlines +0
daviddoria 334 Posting Virtuoso Featured Poster

You have to make sure that the GL directory (if you use gluh.h) or the directory that contains the GL folder (if you use GL/glut.h) is on your "include path". Depending on your environment (Visual Studio, etc) the method to set the include path varies. As a test, try

#include "/full/path/to/GL/glut.h"

If that works, then google "your environment include path" to see how to set it.

I am confused why it is looking for "glut" when you told it to look for "glut.h"?

daviddoria 334 Posting Virtuoso Featured Poster

First, please use real words (anyone vs any1). Second, where did you see this cpuh.h ? We need some context.

daviddoria 334 Posting Virtuoso Featured Poster

If I want to generate all combinations of a set of numbers 0-max, I can do so with for loops. The following code will output all two-number pairs:

for(unsigned int i = 0; i <= max; i++)
 {
 for(unsigned int j = 0; j <= max; j++)
  {
  cout << i << " " << j << std::endl;
  }
 }

Output:

0 0
0 1
0 2
...
0 max
1 0
1 1
1 2
...
1 max

Say now I want to output all four-number combinations. I could easily nest two more loops. However, now I want to write generic code to do this for any number of N-number combinations. You can no longer use this nested loop structure, because the nested loops are hard coded for a particular N.

Any suggestions on how to do this?

David

daviddoria 334 Posting Virtuoso Featured Poster

There is nothing magic about compiling something with a GUI. You simply have to link to the GUI library (in your case Wx) that you want to use. You can see the setup with CMake here:
http://programmingexamples.net/index.php?title=CPP/WxWidgets/Basic

daviddoria 334 Posting Virtuoso Featured Poster

Please use 'code' tags instead of 'program' tags.

Also, please use real words instead of "frndzz, bt, dun knw wats wrng".

daviddoria 334 Posting Virtuoso Featured Poster

Why not just make a loop over the index instead of having a separate explicit loop for each index?

daviddoria 334 Posting Virtuoso Featured Poster

I suggest making the smallest possible compilable code that demonstrates the problem you are having with a dynamically allocated class.

daviddoria 334 Posting Virtuoso Featured Poster

This is a help forum, not a "we do your work for you" forum!

daviddoria 334 Posting Virtuoso Featured Poster

Change 'guess' from a char to a std::string.

daviddoria 334 Posting Virtuoso Featured Poster

Is insertIfNew called in the constructor?

I'd suggest you make the shortest possible compilable code that demonstrates the problem.

"I am having a problem" should be converted to what the problem is and where/when it occurs.

daviddoria 334 Posting Virtuoso Featured Poster

What do you mean by "tests"? Can you show us your current code? Can you tell us exactly what you are starting with and what you are wanting to produce?

David

daviddoria 334 Posting Virtuoso Featured Poster

Those are linker errors. You have not shown your compiler commands, but you'll need a -l something in there somewhere.

I have put several OpenCV examples here:
http://programmingexamples.net/index.php?title=OpenCV

They all use CMakeLists.txt files to be used with the CMake build system, so unless you switch to CMake (which I highly recommend) the building part of the examples won't help you, but maybe the actual content will :)

David

daviddoria 334 Posting Virtuoso Featured Poster

I guess I jumped the gun. First, shouldn't this:

class Point<T, U>

be

class Point

?

Second, I get

Point.h:22: error: invalid use of incomplete type ‘class Point<T, int>’

on

template<typename T>
void Point<T, int>::function1() { };

Thoughts?

daviddoria 334 Posting Virtuoso Featured Poster

Thanks Mike. Now that it gets past my syntax issue, I get the error I was expecting:

PartialClassSpecialization.cpp:14: error: ‘class Point<int, int>’ has no member named ‘function2’

so does this mean that I indeed must partially specialize EVERY class member? I.e. is there no way to instruct it to use the default template<typename T, typename U> for <T, int> unless it has been specialized?