Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 28 Jan 2011
Offline Last Active Oct 21 2014 11:42 PM

#4974469 Compiling a Desktop Application?

Posted by on 29 August 2012 - 10:19 AM

If you can't find the Qt DLLs following the excellent description by Servant of the Lord, another tip from me:
- Open up your project in QtCreator
- go to "Projects" (toolbutton in the left pane)
- select "Build Settings" (should be selected by default).
- Now in the "General" category the first entry is "Qt version". Click on "manage" right next to it.
- It should show you the path to the Qt version used by the QtCreator. The "qmake" Location shows you the path of the qmake.exe file which is located in the same folder as the Qt DLLs. Now use the windows explorer to navigate to it and copy the DLLs following SoL's description.

#4970148 Can't figure out why same results appear

Posted by on 16 August 2012 - 07:03 AM

The problem is that the if statements are all executed in exactly the order you stated them. So when, for example, you enter 40, the condition if (age >= 17) will be executed and return true. Therefore all the other if statements are actually dead code (i.e. unreachable code). What you need to do is put both conditions age >= 17 and age < 26 into one statement using the '&&;' operator:
else if (age >= 17 && age <26) {
MessageBox.Show("Fame beckons!");
This way the "Fame beckons!" message box will only appear when the age is between 17 and 26. Same applies for the "There's still time!" case.

EDIT: Damn, editing messes up the '&' showing up correctly.

#4956434 Pointer to class type?

Posted by on 06 July 2012 - 01:30 PM

Yes it's possible, yes they need to inherit the same base class.

Sorry, but while everything (except, when looking at the original question, the quoted part) you wrote is correct, the original question was

I'm looking for a feature where you can make a variable contain a class type.

and that is simply not possible in C++.

#4956253 Pointer to class type?

Posted by on 06 July 2012 - 02:15 AM

This is not possible in C++. Classes in C++ are no objects and therefore it is not possible to store them in a variable. It is possible in different languages (e.g. Java, Smalltalk...).

What problem are you trying to solve with this? Looks to me like you want to implement some kind of factory pattern with this?!

#4955580 visual studio code optimization is breaking my code.

Posted by on 04 July 2012 - 03:54 AM

I just remembered that I read, that the C++11 std::atomic feature deals with instruction reordering. However, I have never used it and can't say anything about it. If you do have a C++11 compiler the std::thread and std::mutex classes should be all you need anyways.

Less error prone is the usage of std::async and std::future, however, from what you wrote so far your use-case seems to fall out of the scope of those two.

#4917705 Replace part of string in C++

Posted by on 29 February 2012 - 05:52 AM

When processing the file, you read it line by line.
You get the first line. It contains /NAME/ and your code correctly replaces it and gives the desired output.
Now the next line is read. From what you posted it is an empty line. So your call to
will return string::npos. Same for
which is then passed to replace and causes the access out of bounds. Check the return values of your find calls and do not call replace if one of them is equal to string::npos.

#4913019 Qt in Visual Studio 2010 messes up other libs

Posted by on 14 February 2012 - 11:03 AM

From what Adam_42 wrote, I assume the problem is the emit keyword of Qt. The Qt documentation suggests this workaround: http://developer.qt.nokia.com/doc/qt-4.8/signalsandslots.html#using-qt-with-3rd-party-signals-and-slots

#4902277 c++ : Deleting object (not just its pointer) from vector

Posted by on 13 January 2012 - 03:18 AM

I have modified all the code with vector.push_back like this:

Particle a(xx, yy, 6);

Do not call destructors explicitly except when you REALLY know what you are doing. Read this: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.5

#4902271 c++ : Deleting object (not just its pointer) from vector

Posted by on 13 January 2012 - 03:03 AM

Actually, I do not see any way that one of the lines you posted could cause a memory leak (except when one of your classes allocates memory inside its contructor that is not freed in the destructor). Since you are not constructing objects on the heap using new, all objects cou create are created on the stack. Those will be deleted automatically when they go out of scope. The vector itself stores copies of your object:
Particle a(xx, yy, 6);  // creates a new Particle object on the STACK
v_particles.push_back(a); // creates a new Particle object inside push_back and uses the copy constructor of Particle to do so.

In order to find memory leaks you should look into tools that do that for you, e.g. valgrind.

#4899516 passing variable to multiple classes c++

Posted by on 04 January 2012 - 02:51 AM

No problem, glad I could help.

One thing I forgot to mention are that there are different ways of passing arguments in C++. I recommend you look up "pass by value/reference/pointer". In the example I wrote the Player object is passed by reference (noted by the '&').

#4899275 passing variable to multiple classes c++

Posted by on 03 January 2012 - 09:52 AM

I have the feeling that there is some kind of misunderstanding about the Class/Object concept. You call methods (functions) on objects not on classes. Classes are only a definition of what an object can do (at least in C++, in other languages classes themselves are objects as well, but that doesn't matter right now). So if you call the Player::getXPosition() method you need an instance (object) of the Player class.

As you did not post any code, I assume you are doing something like this (very very simplified, but I hope you'll get the picture):

// main
int main(int argc, char** argv) {
  Player p;
  int playerX = p.GetXPosition(); // returns 5
  // more code. ...

// somewhere in your Bullet class
void Bullet::someMethod() {
  Player p;  // creates a new instance of the class Player totally unrelated to the one created in your main method
  int playerX = p.GetXPosition(); // returns the default value for x (apparently 0)
  // more code ....
So what you need to pass to the method in Bullet that is supposed to use the Player object is the actual instance of the Player class. E.g.:
void Bullet::someMethod(Player& p) {
  int playerX = p.GetXPosition(); // returns whatever the player's x position was set to outside
  // more code...
Going back to your main function it may look like this:
// main
int main(int argc, char** argv) {
  Player p;
  Bullet b;
  b.someMethod(p); // will use the Player object with X position set to 5

#4895769 [C++] Are compilers allowed to optimize ...

Posted by on 20 December 2011 - 10:40 AM

We have the same method in our code and it is not optimized away (neither in visual studio nor gcc).

Just to be sure: do a little test project where you force a number to be nan (e.g. sqrt(-1) should produce nan, I think). Apply your nan check and have different output based on the result. Compile with full optimization and check the results.

#4837840 Too many thread instances [Java]

Posted by on 20 July 2011 - 02:05 AM

Actually, Java even offers to take over the whole management of the thread pool: java.util.concurrent.Executors. This way you don't need to manage the sleeping and waking up of the threads yourself.


class Pathfinder implements Runnable {
  public run() {
	// pathfinding stuff

// A simple ThreadFactory class (note that it will only be used when no more threads are available:
public class SimpleThreadFactory implements java.util.concurrent.ThreadFactory {   
	public Thread newThread(Runnable r) {
		return new Thread(r);

// initializing the thread pool (the executor):
ExecutorService mThreadPool = Executors.newFixedThreadPool(mThreadPoolSize, new FSshThreadFactory());

// starting a new pathfinder:
Pathfinder pf = new Pathfinder();

#4805881 Cross referencing headers?

Posted by on 03 May 2011 - 05:34 AM

Use forward declarations.

In your code, the SphereClass header does not need the full definition of SceneClass since you only use pointers to SceneClass. Just replace the line
#include "SceneClass.h"
class SceneClass;

and move the include to the cpp file and you should be fine.

Note that in your case it would not work the other way around, because the SceneClass header actually uses the SphereInfoStruct as a template parameter of the vector.

#4786494 Embedding output console into VS2010

Posted by on 16 March 2011 - 07:15 AM

Or run the debug version without an attached debugger (ctrl+f5)

Another way would be to use OutputDebugStr() for all your output.