daviddoria 334 Posting Virtuoso Featured Poster

oh hahahahaha geez I thought it was something to do with the iterator.. but it's just PEBCAK (isn't that the acronym? problem exists between chair and keyboard?) haha

daviddoria 334 Posting Virtuoso Featured Poster

I would expect this to ouput the 5 doubles, but instead I get the output below.

vector<double> a(5);
	a.push_back(1.2);
	a.push_back(1.3);
	a.push_back(1.4);
	a.push_back(1.5);
	a.push_back(1.6);

    vector<double>::iterator i;
 
    i = a.begin();

    while( i != a.end() )
	{
		cout << *i << endl;
		i++;
	}
0
0
0
0
0
1.2
1.3
1.4
1.5
1.6

Any ideas why?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

and actually, why not just

class foo
{
int A;
};

int main()
{
foo MyFoo;
int b = MyFoo.A; //get
MyFoo.A = 4; //set
}

??

daviddoria 334 Posting Virtuoso Featured Poster

I just learned about returning a reference... and it seems to me that I can delete my mutators now? ie.

class foo
{
int A;

//mutator
void set_A(const int a) {A=a;}

//accessor
int get_A() {return A;}

}

int main()
{
foo MyFoo;
int b = MyFoo.get_A(); //get
MyFoo.set_A(4); //set
}

vs

class foo
{
int A;

//accessor AND mutator
int& a() {return A};
}

int main()
{
 foo MyFoo;
int b= MyFoo.a(); //get
MyFoo.a() = 4; //set
}

Can anyone tell me when I should keep them separate and when I can combine them like this? Or do these do something different (ie. I am understanding it incorrectly) ?

Thanks!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

haha OH NO! The file was in the wrong directory... it works now.

I didn't gaurd it with

if(fin == NULL)

because it was just a toy example... but clearly I should have!

Sorry for the silly mistake.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

That's what I thought I was doing with

linestream.str("");

But I changed all of those to

linestream.clear()

And I get the same garbage results.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I put together this little example to test my sanity, and it failed!

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

/* Example.txt
23
test
4.5
*/

int main(int argc, char *argv[])
{
	string Filename = argv[1];
	cout << "Filename: " << Filename << endl;

	int Integer;
	string String;	
	double Double;

	ifstream fin(Filename.c_str());

	string line;
	stringstream linestream;
		
	getline(fin, line);
	linestream.str("");
	linestream << line;
	linestream >> Integer;	

	getline(fin, line);
	linestream.str("");
	linestream << line;
	linestream >> String;

	getline(fin, line);
	linestream.str("");
	linestream << line;
	linestream >> Double;

	fin.close();

	cout << "Integer: " << Integer << endl;
	cout << "String: " << String << endl;
	cout << "Double: " << Double << endl;
	
	return 0;
}

The output is

Filename: Example.txt
Integer: 65535
String: 
Double: 4.86439e-270

Can anyone spot the stupid bug?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Here's what I did:

class FunctionClass
{
	public:
		virtual double f(const double x) = 0;
};

double integrate(FunctionClass &Func, const double a, const double b, const double epsilon = 1e-6);

Inside Integrate() I can use Func.f(some double).

Then In main.cpp I do

class MyFunction : public FunctionClass
{
	public:
		double a;
		
		double f(const double x)
		{
			return pow(x,2) + a;
		}
};

int main()
{
	MyFunction TestFunction;
	TestFunction.a = 10;
	
	cout << integrate(TestFunction, 0, 5) << endl;

I guess this is the "new" style of passing function pointers? haha

Dave

daviddoria 334 Posting Virtuoso Featured Poster

That's a good idea for that simple case, but that was just an abstraction of my case.

I really have something more like this:

double f(double x, double clutter, double mismatch)
{
  f = pow(x,2) + clutter + 2*mismatch;
}

Where I want to integrate over the first variable, and the rest of the arguments are just constants.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I have written an integration function (of one variable)

double integrate(double (*f)(double), const double a,  const double b)

However, now I want to integrate a function of one variable, but the c++ function has an additional parameter:

double f(double x, bool flag)
{
if(flag == true)
  f = pow(x,2);
else
  f = pow(x,3);
}

I can't pass that to my integrate() function because it has the wrong number of arguments. How do you get around this?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Anyone know a good, simple set of classes to do thing like cartesian to spherical conversion, coordinate system transformations, etc? I wrote some of this stuff, but my code keeps breaking because of "special cases" that I have not handled. I just want to be able to use this stuff and not worry about it.

Suggestions?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

hmm I moved the function to a different file and then it compiled.... I didn't see any name conflicts with List or Map, so I still have no idea why it wasn't working?

daviddoria 334 Posting Virtuoso Featured Poster

Anyone see a problem with this line? My Color header is included (I use it all the time).

map and vector are included.

void GenerateColors(map<Color<unsigned char>, int> &Map, vector<Color<unsigned char> > &List, int num)

I don't see anything wrong?

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Yea, I did that at one point, but then I was modifying the source of the dependencies so much that I added those files to the project so they compiled when I built the top level project because I kept forgetting to open the old projects and rebuild them. Know what I mean?

I guess that is the only option though?

Dave

daviddoria 334 Posting Virtuoso Featured Poster

This problem comes up quite a bit for me. I'll have a project that I work on for a couple months (the point is that it is a large project with multiple classes, etc). As an example, say I made a program that draws a square. So I have this project all setup in KDevelop (or image Visual Studio for all you non-linux-ers!) called Square. Now, moving on to bigger and better things, I want to draw a checkerboard, using all the functionality I have just put into drawing a square. So I make a project called Checkerboard, and either add the Square directory to the Include path, or copy some of the files from square into checkerboard, or do something like that. As soon as there are a couple of levels of this, it gets really out of hand.

Is there some concept that I'm missing to be able to layer projects/thoughs on each other like this. The problem is that the next project is thought of after analysing the results from the first one, so it's not like I could have just made the Checkerboard project from the beginning, because I didn't know I was going to make a checkerboard, I only knew I was going to make a square!

Does this make sense? How does everyone do this?

Thanks,

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Don't simply post your code - ask a question!

daviddoria 334 Posting Virtuoso Featured Poster

There's a really easy to use timer in VUL (part of VXL). It may be a bit of overhead to install it, but it works great once it's installed.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Is there a way to remove these threads altogether? It really pollutes the forums.

Dave

mitrmkar commented: Well said ... +6
daviddoria 334 Posting Virtuoso Featured Poster

Debug symbols are needed if you are trying to debug though! If by profiling you mean something like cachegrind, that will be VERY slow... (maybe 50x slower?), but you get an unbelievable view of any bottlenecks in your code.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

You may look into some kind of library for working with videos. I think VXL (http://vxl.sourceforge.net/) has a VIDL library for working with videos. VTK also can do things with videos (http://www.vtk.org/doc/nightly/html/classvtkFFMPEGWriter.html)

Good luck!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Generally it's a good idea to abstract the problem into as simple and small piece of source code that you can rather than upload your entire project. This way when an answer is obtained, it will help others in the future as well, rather than just help the king move properly!

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Is this being done with some external program? I wasn't aware c++ could upload files... lol

daviddoria 334 Posting Virtuoso Featured Poster

Please wrap your code in code tags.

I think you have to explicitly define an update command for the data adapter.

daviddoria 334 Posting Virtuoso Featured Poster

I agree, I have been unable to find anything good about this so far!

daviddoria 334 Posting Virtuoso Featured Poster

nope, if you make an array of length 5

int test[5];

then you have to use elements 0 to 4

for (i=0; i<5; i++)
daviddoria 334 Posting Virtuoso Featured Poster

Note that c++ arrays are 0 based. That is, array[0] is the first element, not array[1].

Dave

daviddoria 334 Posting Virtuoso Featured Poster

please use code tags

daviddoria 334 Posting Virtuoso Featured Poster

There are also linux utilties dos2unix and unix2dos (or something like that) that will convert the line endings to the appropriate type.

I'm glad you got it working.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Probably more of a linux question than a c++ question.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I don't see "store" defined anywhere?

Dave

daviddoria 334 Posting Virtuoso Featured Poster

that looks like the log function, "L"n (LN) not "I"n (IN). That should be a good place to start. You seem not to have attempted to write that part of the code at all. Give it a shot and let us know how it goes.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

The constructor of your class simply needs to be called after the values have been input, and passed those input values.

class Person
{
string Name;
int Age;
Person(const string name, const int age) : Name(name), Age(age) {}
};

That syntax is called an "initialization list". It means class variables are assigned during the construction of the class.

int main()
{
//input from user
string name;
int age;
cin >> name >> age; //or however you want to input them
Person(name, age);
}

That should construct the class with the user input values.

Hope that helps.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

cout<<input * (input-1)<<"\n"; //multiplies it all together

that line is not dependent on the function call before it.

also, you should call your function "factorial" instead of "recurse". The recursiveness should be implied by the behavior, it shouldn't need to be stated explicitly.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

You should try to make the question as limited as possible.

"I would like to write a number to a file, and then later retrieve that number from a file." is much clearer than something about a game and a quest, etc.

Try to get a sample application working (using unbeatables post as a starting point) before you try to integrate it into your game. You sample program should do exactly what you're trying to do and nothing else (ie. write a number to a file, and then read it in again.)

If you get that to work on its own, you should have no problem integrating it into the game.

Post back with results from the sample program.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

did you really think "pls?..." was gona change the forum rules/attitude?

daviddoria 334 Posting Virtuoso Featured Poster

ah great - thanks!

daviddoria 334 Posting Virtuoso Featured Poster

changing to release mode just tells it not to run the assert commands. that is a TERRIBLE idea because now something is going wrong, its just not telling you! It will come back to bite you for sure!

Use step through in debug mode to see which line the assert fails and let us know.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I dont' really understand either, but maybe you just want to put a vector in the derived class instead of in the base class?

daviddoria 334 Posting Virtuoso Featured Poster

Yea - this question is clearly a "hello world" type program - they are obviously not getting into stacks yet. You're just confusing the issue instead of adding something constructive...

daviddoria 334 Posting Virtuoso Featured Poster

makefiles is a question for a linux forum, not a c++ forum.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

I have seen some examples like this

p.class1 { some stuff }
p.class2 {some stuff }

and some like this

class1.p {stuff}
class2.p {stuff}

In one case, the class name is before the dot, in the other case the class name is after the dot. What is the difference between these two notations?

Thanks,
Dave

daviddoria 334 Posting Virtuoso Featured Poster

use the "top" command, and look under the %MEM column... this is a linux question though, not a c++ question.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

1) make sure you open the file in append mode in the add() function (i'm not sure what the default mode is)

2)there is no need to do

while (!a.eof()){
getline(cin, line);

you can simply do

while(getline(cin,line))

If you don't tell us what's wrong, we can tell you how to fix it! (unless we compile it ourselves - which you're supposed to do for us! haha)

Dave

daviddoria 334 Posting Virtuoso Featured Poster

Arrays are useful because they help you understand how things work a little bit, but seem very unpractical to me now that STL has come around and has such a nice vector class.

Dave

daviddoria 334 Posting Virtuoso Featured Poster

didn't we answer the question??

daviddoria 334 Posting Virtuoso Featured Poster

yea, vector's have a size() function

vector<int> A(5); //declare a vector of length 5
cout << A.size() << endl;
daviddoria 334 Posting Virtuoso Featured Poster

fantastic, thanks!

daviddoria 334 Posting Virtuoso Featured Poster

what do you mean xeuron?

daviddoria 334 Posting Virtuoso Featured Poster

you could use a vector instead of an array

#include <vector>

then you can return a vector<int> and not have to worry about every saying pointer!

Dave

Salem commented: Definitely the best idea +25
daviddoria 334 Posting Virtuoso Featured Poster

yep, exactly the same thing in this case because we haven't used any of the "fancy" ideas of classes. Just replace "class" with "struct".

Dave