daviddoria 334 Posting Virtuoso Featured Poster

This doesn't seem to behave as I would expect (as it does in matlab, for example). I want to execute a command, but give some readable output instead of the often very messy auto-generated error (index out of range or something).

try
{
  MyFunction(1,2);
}
catch(char* str)
{
  cout << "There was a problem." << endl;
}

Is that now how you do this in c++?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

that is exactly what it needs to do - but now I need to understand!

fn(&d); //pass the address of the beginning of the vector d to the function f

void fn(void *ptr) //accept an address of an unknown type

MyParam*p = static_cast<MyParam*>(ptr); //take the unknown type and force it to be a MyParam called p

My only question is why create a
MyParam*p
instead of a MyParam p ?

(Then you are using the -> operator since it is a MyParam*, but could you have cast to a MyParam and used . instead?)

Thanks!

daviddoria 334 Posting Virtuoso Featured Poster

I have to call a function in the form f(void* params)
I would like to pass two vector<double> to this function.

I suppose I should make a struct

struct MyParam_t {
vector<double> myvector1;
vector<double> myvector2;
};

and then somehow fill it and pass it to the function. Someone recommended that I use this:

struct MyParam_t {
  vector<double> *myvector1;
  vector<double> *myvector2;
};

but i'm not really sure the difference. Can someone explain how I would fill this struct - My guess was

vector<double> a;
a.push_back(0);
MyParam_t MyParam;
MyParam.myvector1 = a;
f(MyParam)

but it doesn't work.

Also, then when I am in the function and have a variable of type void* called params, how do i get back my variable of type MyParam_t?

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I ran this:

cout << "has nan q?" << numeric_limits<int>::has_quiet_NaN << endl;
	cout << "has nan s?" << numeric_limits<int>::has_signaling_NaN << endl;
	cout << "has nan q?" << numeric_limits<double>::has_quiet_NaN << endl;
	cout << "has nan s?" << numeric_limits<double>::has_signaling_NaN << endl;

and I get
0
0
1
1

why would int not have NaN?? I'm using g++.

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

finally got it working...

//compile and link with
// g++ main.cpp -o ss -I/usr/include/QtGui -lQtGui

 #include <QWidget> 
 #include <QDesktopWidget>
 #include <QApplication>
 #include <QPixmap>


 void shootScreen(char* fileName);

 int main(int argc, char *argv[])
 {
	QApplication MyScreenshot(argc,argv);
	shootScreen("test.png");
	return 0;
 }

 void shootScreen(char* fileName)
 {
        QPixmap originalPixmap;
	originalPixmap = QPixmap::grabWindow(QApplication::desktop()->winId(), 100, 500, 200 , 50);//x, y, width, height
	originalPixmap.save(fileName);
 }
daviddoria 334 Posting Virtuoso Featured Poster

I have some code like this:

vector<double> Saved;
for(int i = 0; i<10; i++)
{
  if(some condition on i)
      Saved.push_back(i);
}

Then I want to see what order the things were saved... so naturally I do

cout << Saved.at(0) << endl << Saved.at(1) << endl;

But to my surprise they aren't in the right order!! Pretty odd if you ask me...

I would expect the first thing output there to always be less than the second thing... correct??

The actual code follows, but relies on a bunch of stuff so I don't think it will be very helpful:

for(int scancounter = 0; scancounter < NumScans; scancounter++) //alpha loop

	{

		for(int concentriccol = 0; concentriccol < ColsPerScan; concentriccol++) //beta loop

		{
			B.clear();
			V1.clear();
			V2.clear();

			B = LiDARGrid.ConcentricScans.at(scancounter).getColumn(concentriccol);	

		
			double Penalty = CreateComparableVectors(A,B,V1,V2);


			double distance = VectorDistance(V1,V2) + Penalty;


			if(distance < 100) //distance bigger than this is unreliable

			{
				if(distance <= BestDistance)

				{

					BestDistance = distance;

					BestScanPositions.push_back(scancounter);

					BestCols.push_back(concentriccol);

				}
			}
			

		}//end col loop

	}//end scanner loop

	cout << "Input Scan: " << CorrectPosition << " Col: " << CorrectCol << endl

			<< "Matching Scans: " << endl
			<< BestScanPositions.at(0) << " Col: " << BestCols.at(0) << endl
			<< BestScanPositions.at(1) << " Col: " << BestCols.at(1) << endl
			<< "Best Distance: " << BestDistance << endl
			<< "There were " << BestScanPositions.size() << " good matches." << endl << endl;

An example is BestScanPositions.at(0) is 0 and BestScanPositions.at(1) is also …

daviddoria 334 Posting Virtuoso Featured Poster

surely someone knows how to do this??

daviddoria 334 Posting Virtuoso Featured Poster

hmmm
It ended up that "AnotherFIle.h" was part of a static library that I had compiled. When I looked in it's directory, there were a bunch of .gch files!! (I think they are the precompiled headers??) I had just compiled the library the same day and I definitly did not turn on any flags for precompiled headers... does it default to something different when you compile with just -c so only object files are created?

Very strange...

daviddoria 334 Posting Virtuoso Featured Poster

n windows I used GDI+ to capture a region of the screen. Is there something similar for linux? I want to do

ScreenCapture(Top, Left, Width, Height);

and save it to a file (png or something).

What's the easiest way to do this?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

When trying to compile my code with g++ i get this:

MayaFunctions.h:5:27: error: calling fdopen: Bad file descriptor

in that line, I simply have
#include "AnotherFile.h"

Google told me this has something to do with precompiled headers, but I can't figure out how to turn them off.

1) how to turn off using precompiled headers with g++ flags?
2) what else would be causing this??

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

dougy83 - how do i save it to a file? I have the simple example of just writing it back to the screen working so far!

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

dougy83, thanks, i'll take a look

niek_e - that example seems to use a sendkeys printscreen type of thing - i was thinking more along the lines of selecting a region to capture

i'll report back on the GDI stuff - dougy83, how would I save that to a file?

daviddoria 334 Posting Virtuoso Featured Poster

I would like to take a screenshot of a region of my screen programmatically (so I can do it every time through a loop, for example.) Does anyone know how to do this? I guess it would involve the win32api - I've not used this before so any hints would be great!

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I am using VS2008. Turns out I was using a library that was using the 80 version of the file.
I used this
http://www.dependencywalker.com/

and it showed me exactly what the problem was! Highly recommended and free!!

Sorry for the confusion.

Dave

Ancient Dragon commented: Excellent :) +26
daviddoria 334 Posting Virtuoso Featured Poster

I keep getting this error:

this application has failed to start because MFC80.dll was not found.

In the past, when I've got this error i've played around with "Runtime library", switching it from /MDd to /MD to /MTd......etc etc

then I turn off and on the manifest files

then I delete the manifest file

i keep doing these things until the error magically goes away. I am wondering if anyone can shed some high level light on what these things are and when I should have them set a particular way.

I cant seem to find a very good tutorial (mainly cause i didn't know what it would be called!) so if you just have a link for me i'd appreciate it.

Thanks!
Dave

daviddoria 334 Posting Virtuoso Featured Poster

I seem to be able to write as many characters as I want into "buffer" even though I declare it as length 1 - why is this?

string teststring = "teststring";
	cout << teststring << endl;

	char buffer[1];
	itoa(34567,buffer,10);

	teststring.append(buffer);
	cout << teststring << endl;

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

GOT IT!! I had to make the accessor functions const!

geom_Point getP1() const;

weird... why do you not have to do that in VS?

daviddoria 334 Posting Virtuoso Featured Poster

Ah I forgot to mention - neither do I!! I am trying to compile it with g++ (through KDevelop) because I have to make a linux static library (.a) file. I thought that g++ was much stricter about some things than VS. Can you try it on linux? I've tried it on 2 machines and got the same error both times.

-Dave

daviddoria 334 Posting Virtuoso Featured Poster

Sorry, I don't know why I used a forward declaration. SInce geom_Edge is not used in geom_Point, I can simply #include "geom_Point.h" in geom_Edge like so:

geom_Edge.h

#include <iostream>
using namespace std;

#include "geom_Point.h"

class geom_Edge
{
	geom_Point P1;
	geom_Point P2;
public:
	geom_Point getP1();
	geom_Point getP2();
	//default constructor
	geom_Edge()
	{

	}
};

ostream & operator << (ostream &output, geom_Edge &e);

NOW I am back to the << problem (phew, i was hoping it wouldn't just go away hahaha)

Try it with that geom_Edge.h and see what you get.

error: no match for 'operator<<' in 'output << ((geom_Edge*)e)->geom_Edge::getP1()'

daviddoria 334 Posting Virtuoso Featured Poster

Ok I made a simple demo program that does exactly the same thing, now I get a different error though:

In geom_Edge.h (on this line "geom_Point P1")
error: field P1 has incomplete type

geom_Point.h

#include <iostream>
using namespace std;

class geom_Point
{
	double x;
	double y;
	double z;
public:
	double getX();
	double getY();
	double getZ();
	
	//default constructor
	geom_Point()
	{
	
	}

};

ostream & operator << (ostream &output, geom_Point &p);

geom_Point.cpp

#include "geom_Point.h"

double geom_Point::getX()
{
	return x;
}

double geom_Point::getY()
{
	return y;
}

double geom_Point::getZ()
{
	return z;
}


ostream & operator << (ostream &output, geom_Point &p)
{
	output << p.getX() << " " << p.getY();
	return output;
}

geom_Edge.h

#include <iostream>
using namespace std;

class geom_Point; //forward declaration

class geom_Edge
{
	geom_Point P1;
	geom_Point P2;
public:
	geom_Point getP1();
	geom_Point getP2();
	//default constructor
	geom_Edge()
	{

	}
};

ostream & operator << (ostream &output, geom_Edge &e);

geom_Edge.cpp

#include "geom_Edge.h"

ostream & operator << (ostream &output, geom_Edge &e)
{
	output << e.getP1() << " " << e.getP2();
	return output;
}

geom_Point geom_Edge::getP1()
{
	return P1;
}

geom_Point geom_Edge::getP2()
{
	return P2;
}
daviddoria 334 Posting Virtuoso Featured Poster

In the original post I mentioned that I have already overloaded << for Point. And also since the operator<< has to be public (I could make it friend, but i chose not to), it has to look like this

ostream & operator << (ostream &output, Point &p)
{
output << p.getX() << " " << p.getY();
return output;
}

Is there anything else I can check??

daviddoria 334 Posting Virtuoso Featured Poster

I have 2 classes, Edge and Point. Edge contains Point P1 and Point P2 as private memebers. Edge has accessor functions

Point getP1()
{
return P1;
}

and the same for P2.

Point has << overloaded -

ostream & operator << (ostream &output, Point &p);

I am trying to overload << for Edge:

ostream & operator << (ostream &output, Edge &e);

this works fine:

ostream & operator << (ostream &output, Edge &e)
{
Point x = e.getP1();
Point y = e.getP2();

output << "P1: " << x << " P2: " << y;

return output;
}

but this doesn't work:

ostream & operator << (ostream &output, Edge &e)
{
output << e.getP1() << " " << e.getP2();
return output;
}

The compiler says "Error: no match for operator << in output << Edge::getP1()()"

First, why are there two sets of parentheses in the compilers message?

Second, why does this happen?

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

great link! I haven't tried it yet, but it seems like I have to "re" define the variable using the "extern" command:

void FuncThatCantSeeTheVariable()
{
extern int TheVariable;
cout << TheVariable;
}

I'll report back with results.

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

sure I could use a define, but I need to determine the value of this variable at the beginning of the program, so I can't #define it! So can you just not see this type of global variable inside another .cpp file?

daviddoria 334 Posting Virtuoso Featured Poster

I had a global variable that could be seen by all of my functions. I was getting too many functions in the same file, so I made functions.h and functions.cpp. I put the function definitions in functions.cpp and the declarations in functions.h. In main.cpp, I include functions.h. The problem is, now I can't see the global variables in the functions that I have moved! Is there a way to see them still?

I thought I might just have to move the global variable definition to BEFORE the include statements, but that didn't change anything, I still get "undeclared identifier".

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

oh, so mingw is like cygwin? I don't need to just run it in windows under "linux", i need to run koolplot with VS2008. Is this not possible? (Please dont just say "look at the webpage" because I am unable to find anything about this there!)

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

so that totally undos everything that was done to / stored in A and sets A.name to 3 (and now date is a junk pointer)?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

I have a class called MyClass

in main, I have

MyClass A; //calls the default constructor
A.date = 4;

I would like a function to do some stuff and then use one of A's constructors , the result being I have A back in main

class MyClass
{
public:
int name;
int date;
MyClass(){};
MyClass(int i)
{
name = i;
}

};

void MyFunc(MyClass &A)
{
 int i = 3;
MyClass A(3);
}

Can you do this at all? Back in main, will A have date=4 and name=3? or just date=4?

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I found quite_NaN and it seems to work well - i'm surprised there is no "isnan" function included though!
you can write a simple one though

bool isnan(double x)
{
return (x!=x);
}
which utilizes the fact that NaN's are not equal to anything, even themselves.

I have a big grid of numbers that I am reading in, but sometimes instead of a number i get a "blank" that i need to put something in to recognize that there was infact nothing there.  How else would I do that without nan's if the possible values of these numbers range from -inf to inf?

Thanks,

Dave
daviddoria 334 Posting Virtuoso Featured Poster

I get this error:
Cannot open include file: 'graphics.h': No such file or directory

daviddoria 334 Posting Virtuoso Featured Poster

I found _isnan() in float.h for VS2008, but I would like to SET a variable to a NaN intentionally, not check if it is one.

Is this possible?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

so how do i make PI a standard name on my compiler? It doesn't seem to work with PI or M_PI if i include math.h and/or cmath.h

daviddoria 334 Posting Virtuoso Featured Poster

Should I use cmath to use PI and trig functions? I dont think math.h has it - and cmath and math seem like redefine alot of the same things.

What is standard practice?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

is there a windows version of koolplot?

daviddoria 334 Posting Virtuoso Featured Poster

what about the other way, can you use a .a in windows?

daviddoria 334 Posting Virtuoso Featured Poster

I made a library (.lib) with VS2005. Is there a way to convert it to a .a linux library without moving the code and setting up a project and compiling in linux?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

that looks perfect! i'll try it out tomorrow

daviddoria 334 Posting Virtuoso Featured Poster

is there a package for c++ that will allow me to make a matlab/gnuplot style plot from within c++? I would like to skip the step of writing data to a file and then using something else to read in the data and then plot it. Let me know what you do when you have this situation.

Thanks!

David

daviddoria 334 Posting Virtuoso Featured Poster

hmm sounds like a good fix - but why didn't (#define _CRT_SECURE_NO_DEPRECATE) work? I shouldn't have to do that #pragma for every error like this that I'm getting should I?

daviddoria 334 Posting Virtuoso Featured Poster

I am getting a whole bunch of these:

warning C4996: 'vsprintf' was declared deprecated
see declaration of 'vsprintf'
Message: 'This function or variable may be unsafe. Consider using vsprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_DEPRECATE.

I put this at the top of my code:

#define _CRT_SECURE_NO_DEPRECATE

but nothing changed, I still get the warnings.

Any reason why this wouldn't fix it?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

This is a very general question. I'm sure there is no "right" way so I thought I'd ask for opinions/common practice.

In my file MainProgram.cpp (containing main()), sometimes I will have LOTS of functions. It seems very distracting to have a thousand lines of code all in the same file, I'll have to page down many many times to find what I am looking for. Is there a good way to kind of group functions together into separate files that make them easier to locate and less "in the way"?

Thanks!

David

daviddoria 334 Posting Virtuoso Featured Poster

since V is already sorted, how would I sort Index based on how V WAS sorted??

daviddoria 334 Posting Virtuoso Featured Poster

I've seen that a vector can be sorted using

vector<double> V;
vector<int> Index;
V.push_back(9);
V.push_back(10);
V.push_back(8);
Index.push_back(0);
Index.push_back(1);
Index.push_back(2);
sort(V.begin(), V.end());

If I want to sort Index according to how V was sorted, there is an optional third parameter to sort() that will do this. I don't understand how to specify this third parameter. I saw an example that defined an iterator, but that was for sorting a fancier class than just doubles.

Can someone point me in the right direction?

Thanks!

David

daviddoria 334 Posting Virtuoso Featured Poster

Sorry for the silly question - I figured it out.

Turns out you only have to add the .ccp file to the project. When you #include "director/myclass.h", that declares the functions, but without the .cpp as part of the project, the function definitions are no where to be found!

David

daviddoria 334 Posting Virtuoso Featured Poster

I wrote a simple class:
c:\geometry\Point.h
c:\geometry\Point.cpp

I made a VS2005 project and added a Source File test.cpp which contains main(). In test.cpp, I #include "c:/geometry/Point.h"

When I build the project I get:

Unresolved external symbol "public: __thiscall Point::Point(double,double,double)" referenced in function _main.

However, if add Point.h to my Header Files and Point.cpp to my Source Files the error goes away. Why do I have to add these to the project? Can't I just use them by including them like I tried to?

Thanks,

David

daviddoria 334 Posting Virtuoso Featured Poster

Thanks guys. I ended up making the point class completely independent and everything is much smoother now. I tested out the pointers also, and it works fine, but then I just have to dereference everything in the main program , which still seems annoying to my beginner self. haha

daviddoria 334 Posting Virtuoso Featured Poster

right right, I do need both classes to have the other type. I should have been clearer that in this example I didn't actually have the Point class contain a Line object, but I thought we could address the problem without having the circularity.

The reason I want these is so I can do MyPoint.GetLineTo(AnotherPoint)
and
MyLine.GetTwoPoints()

daviddoria 334 Posting Virtuoso Featured Poster

Last week I asked about this so I tried to write another example to try it out. I have a Line class which needs to have and members of type Point and member functions that return type Point. The Point class needs to have member functions that return type Line (this has been omitted from the example because the solution will be the same).

Last week I was informed that if I have a situation like this, then in class Line I can only have members of type *Point. Clearly, I would return them to the main program with return &MyPoint. However, with member functions, since they can only return *Point, there is obviously no way to get the value to main without in main doing Point P=&Line.getP1(). That seems like it would get a bit old.

1) is this all correct?
2) is there not a better way to do it?

Thanks!

David


Line.h

#ifndef LINE_H
#define LINE_H

#include "Point.h"
class Point;

class Line
{
	// Ax+By+C = 0
	double A,B,C;
	Point *p1, *p2;

public:

	Line();

	Line(Point P1, Point P2);

	~Line() {}

	double getA() { return A;}
	double getB() {	return B;}
	double getC() {	return C;}
	//Point getP1() {return *p1;}
	//Point getP2() {return *p2;}
};

#endif

Line.cpp

#include "Line.h"

Line::Line()
{
	A=0;
	B=0;
	C=0;
}

Line::Line(Point P1, Point P2)
{
	p1=&P1;
	p2=&P2;
	double x1,x2,y1,y2;
	x1=P1.getX();
	x2=P2.getX();
	y1=P1.getY();
	y2=P2.getY();

	//derived from slope(m) intercept(b) form
	double m,b;
	m = (y2-y1)/(x2-x1); …
daviddoria 334 Posting Virtuoso Featured Poster

Anyone know a good package that will take a multidimensional function and find its minimum? I can't get opt++ to work under windows. My function is not analytic so the package should have numerical gradient stuff built in.

I had no idea this would be so hard to find!

daviddoria 334 Posting Virtuoso Featured Poster

I am trying to use OPT++ for multidimensional optimization with Visual Studio 2005. I added the include directory to my project. I added the lib directory to my project. When I try to compile the example that ships with the package, i get 102 of the same error:

NEWMAT is not a class or namespace

I also added the .../opt++/newmat directory to my projects include directories, but nothing changed.

Anyone have an experience with this?

Thanks!

Dave