1,177 Posted Topics
Re: You are generating the number, and then outputting it 6 times! You need to move the random generation into the loop [code] for (int count = 0; count < 6 ; count++) { number_back = rand() % 53 + 1; cout<<number_back<<endl; } [/code] | |
Re: Can you please ask a specific question rather than just posting code? And also in the response - what seems to be added was the foreign() definition? | |
| |
Re: I think what you want is a friend class. I've never used it, but I bet google knows about it :) | |
Re: Please ask a specific question. Where is the code broken? Does it compile? If no, where/what are the errors? | |
I am trying to write a pretty straight forward algorithm (agglomerative clustering), but the details are getting pretty hairy. (A cluster is simply a group of points. The idea is to take a list of points and decide which are "grouped" or "clustered" close together.) Here is the outline: Input: … | |
On the line [code] set<T>::iterator iter; [/code] I am getting "expected ';' before 'iter'" [code] #include <vector> #include <set> using namespace std; template <typename T> vector<T> UniqueElements(const vector<T> &V) { set<T> s; s.insert(V.begin(), V.end()); vector<T> Elements; set<T>::iterator iter; return Elements; } [/code] Can anyone see why? It compiles fine if … | |
Re: if you have a very simple program that just has to run a zillion times, you can use OpenMP parallel for loops. You just put a #pragma right before the loop and it magically splits it across all available cores. You can do more complicated things with openmp, but this … | |
This simple example fills a vector with numbers 0-9. Then I use find() to find where the 7 is. Then I saw online you can get the index by simply subtracting V.begin() from the iterator that find() returns. Has - been overloaded for iterators to do some magic and return … | |
I was using a library recently and I was getting a "undefined reference" linker error when I tried to call a template function with a type that they did not plan on me using. I asked on the mailing list and they said to call [code] VSL_VECTOR_IO_INSTANTIATE(vnl_matrix_fixed<double,3,3>); [/code] I guess … | |
Currently, my index.html is like this [code] <html> <head> <title>EngineeringNotes.net</title> <link href="style.css" rel="stylesheet" type="text/css" media="screen" /> </head> <body> <div class="header"> <div id="logo"> <h1>EngineeringNotes.net</h1> </div> <div class="menu"> <ul> <li><a href="mailto:daviddoria@gmail.com">Contact Me</a></li> <li><a href="phpBB3">Forums</a></li> <li><a href="Ideology.html">Ideology</a></li> </ul> </div><!-- end menu --> </div><!-- end header --> <div class="page"><!-- start page --> <div class="sidebar"><!-- … | |
According to this: [url]http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13[/url] One way to keep only the function declaration in the .h file is to do this [code] ////////// file: Tools.h #include <iostream> #include <vector> using namespace std; template <typename T> T Sum(vector<T> &V); [/code] [code] ///////// file: Tools.cpp #include "Tools.h" #include <iostream> #include <vector> using namespace … | |
To remove duplicates from a vector, my first thought was to insert them all into a set, it would take care of the uniqueness of the elements, and then read them back out into my vector. However, I have a vector<Point>, where Point is a point in 3d space (ie. … | |
![]() | Re: There is no reason to post all of that code to ask that question. I suppose you need to handle/trap a keypress event and manually output a *, but I'm not sure how to do that. |
Re: 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 | |
Re: You say [code] if (Puzzle[ptx]... [/code] but you haven't defined Puzzle anywhere! | |
I was reading about the new "tuple" type coming in c++0x, and I decided to try it. I saw that if you give a compiler flag -std=c++0x it will work. So I did it, and then #include <tuple> works and everything was good. Then I decided to try the default … | |
Is there a built in type to store UNordered pairs? ie. I want these [code] pair<double, double> a(4.0, 5.0); pair<double, double> a(5.0, 4.0); [/code] to be equal. Do I have to make a wrapper and override == ? Thanks, Dave | |
I often have this situation [code] class OrientedPoint { private: Point P; Vector N; Color C; bool valid; public: //////////// Constructors ////////// OrientedPoint() {} OrientedPoint(const Point &Coord); OrientedPoint(const Point &Coord, const Vector &Normal); OrientedPoint(const Point &Coord, const Color &C); OrientedPoint(const Point &Coord, const Vector &Normal, const Color &C); [/code] where … | |
Is pthreads still the way to go to detach a process from the main thread in linux? It seems seems kind of convoluted/old/c-style from looking at some examples - is there a more "c++" way? Thanks, Dave | |
Re: That seems like a good outline, but you have to attempt to write the key functions and then we can help you debug it. | |
Re: Which lines is it complaining about? Also, in general, you cannot/should not do this: [code] char stuNames[stu][21]; double stuGrades[stu][num]; [/code] The numbers in [ ] need be determined at compile time, or you need to dynamically allocate the array using "new". | |
I'm just trying to figure out how this stuff works. I wrote this function: [code] #include <algorithm> #include <vector> double VectorMax(const vector<double> &a) { vector<double>::iterator pos; pos = max_element (a.begin(), a.end()); return *pos; } [/code] and call it with: [code] vector<double> Numbers; Numbers.push_back(3.4); Numbers.push_back(4.5); Numbers.push_back(1.2); cout << VectorMax(Numbers) << endl; … | |
I am trying to use the for_each from stl algorithm. [url]http://www.cplusplus.com/reference/algorithm/for_each.html[/url] [code] #include <algorithm> #include <vector> #include <iostream> template <typename T> void OutputObject(const T &obj) { cout << obj << endl; } template <typename T> void OutputVector(const vector<T> &V) { for_each (V.begin(), V.end(), OutputObject); cout << endl; } [/code] I … | |
I found this while googling for something [url]http://msdn.microsoft.com/en-us/library/ms177203(VS.80).aspx[/url] I tried it (with g++) and it seems to be a syntax error - is this like something specific to Visual Studio or something? Dave | |
Re: With text files I don't think there is much option but to read in the whole file, and write out the whole file again, but this time with the new value in the place of the old value. I think you'd have to use an actual database to be able … | |
Is there a reason to use [code] cout << static_cast<int>(thing); [/code] instead of [code] cout << (int)thing; [/code] ? Thanks, Dave | |
If I make an array like this [code] GLubyte bufImage[100][100][3]; [/code] I can pass bufImage to a function and then get the values using: [code] r = bufImage[im_x][im_y][0]; g = bufImage[im_x][im_y][1]; b = bufImage[im_x][im_y][2]; [/code] However, if I don't know Width and Height at runtime, this does not work. I … | |
Re: You seem to have not tried at all. Give it a shot and then if it doesn't work we'll help you. | |
I've seen a few questions about this floating around here (mostly from me :) ) but I finally got this is into a nice working form - maybe it will be useful for someone. It is called like this: [code] void TestParallelSort() { vector<double> Numbers; Numbers.push_back(3.4); Numbers.push_back(4.5); Numbers.push_back(1.2); vector<string> Names; … | |
Re: I don't know how it works with windows, but in linux you have to use some kind of "UI Framework" like QT or GTK. | |
Re: Even though you are using c++, this is not a c++ question. Ask here: [url]http://www.opengl.org/discussion_boards/[/url] And I bet you'll get your answer. | |
Re: They are like arrays, but you can dynamically add items to them using push_back. They also have a function size() to get the length of the vector. I find them amazingly useful! | |
Re: Throw some code tags around there so it's a bit more readable, but thanks for the tutorial. Is this the right place to post these? I have a handful of things like this that may be useful to others - where should we put them? | |
It seems like SO often I need to attach a "valid" flag to a standard type. For example - I have a function called "CalculateLunchTime" that I pass a vector of times. If the vector has 0 elements, they did not take a lunch, so the duration is 0 minutes. … | |
Re: Any time there is an "if" I'd say it would either turn into a piecewise function or you'd need some wacky step functions. Also, please use code tags. | |
Re: This should do what you're looking to do. For some reason there is a small issue with re-using the string stream for the 4th line (so I made a new one, I left the code that doesn't work commented), but I've started a thread about why that is. [code] void … | |
Re: You cannot do this, it's simply incorrect syntax: [code] Furlong(class Kilometer& kilo) [/code] You just need to do [code] Furlong(Kilometer& kilo) [/code] And also, you really shouldn't/can't have both classes contain the other class. In this case I would make two non-member functions [code] Furlong KilometerToFurlong(Kilometer &kilo); Kilometer FurlongToKilometer(Furlong &furl); … | |
If I use the uncommented 3 lines for getting an int out of a stringstream, the value in MPV is wrong. However, if I make a new stringstream, then it works fine. I thought setting .str("") was essentially resetting the stringstream? [code] line = ""; getline(fin, line); //get fourth line … | |
I have two functions: GetDayTime and GetLunchTime. From some places in the program, all I have is the associate name and a date and I want to determine these quantities. So I just query the database inside the function: [code] Public Function CalculateLunchTime(ByVal DateToCalculate As DateTime) As Double Dim daEvent … | |
Re: I do it just to remind myself (and the compiler) that no class variables can be changed in this function. Basically just to keep me from accidentally doing something stupid! ie. [code] double YourClass::GetX() { x_ = 2.0; } [/code] You should not be modifying any of the member variables … | |
Re: I really never use arrays, I go for the vector of vectors [code] vector<vector<char> > array(columns); for(unsigned int i = 0; i < columns; i++) { vector<char> RowVector(rows); // ... fill the row vector ... array.push_back(RowVector); } [/code] If you want to use an actually array you have to use … | |
Re: I had this same question: [url]http://www.daniweb.com/forums/thread114375.html[/url] The answer was basically "you've designed it wrong"! | |
I have a delete button which does this: [code] TTDataSet.PunchEventTable.Rows(dgvData.SelectedRows(0).Index).Delete() [/code] The problem is that if I delete row 0 (the 0th index in the DataGridView and in the underlying table, all is well. But now row 1 in the table corresponds to row 0 in the DataGridView, so if … | |
I have tried to make a SumVector function for all types, but if it is called with a vector of unsigned char's, it will do something special. Here is what I tried, but I get a multiple definitions of the function compiler error. [code] template <typename T> void SumVector(vector<T> &V) … | |
Re: Which line do you get the pointer comparison error? In this code: [code] int random_integer = (rand()%6)+1; random_integer = pcdice1; [/code] You are storing the random number in random_integer, and then immediately replacing it with pcdice1. That is why you aren't seeing a different number each time. Dave | |
Re: Yea, but it looks like you're using it on line 77 and defining it on line 241 - you need to define it before you use it! | |
Re: I've found boost particularly hard to work with, especially if you don't know what you're doing! It sounds like you just want a switch statement? [url]http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Companion/cxx_crib/switch.html[/url] If you have a more exact goal then let us know. |
The End.