2,898 Posted Topics
Re: >>Can I build such a template? You cannot. The reason this is a problem (and the reason for the errors) is because your function template, when instantiated with int, has to compile, all of it has to compile, including the parts that will not be executed (i.e. parts which insert … | |
Re: The reason why the code keeps repeating the first value all the time is because the first value "9" is the largest one, so it will be moving up the array as you are traversing it. If you want to print the original array, make a separate loop for that. … | |
Re: To get the normal out of a vertex on the edge of a 2D polygon, you take the vector that goes from the point just before to the point just after, and then you rotate that vector by 90 degrees ccw (right-hand rule). You can do that by swaping (x,y) … | |
Re: Definitely, [URL="http://www.codeblocks.org/downloads/26"]Code::Blocks[/URL] is recommended. It's the easiest one to download-install-setup-use with gcc (you should be able to pretty much compile and run a hello-world program within the minute following the install). As for GCC, the stable recent versions are good. I think the current release of CodeBlocks comes with GCC … | |
Re: Don't use setprecision if you want the computer to figure out the precision. | |
Re: You can use [URL="http://www.boost.org/doc/libs/1_46_1/libs/tokenizer/index.html"]Boost.Tokenizer[/URL]. It has nicer functions for extracting tokens from strings. You can, for instance, specify a list of characters that should be ignored and that mark the separation of tokens. This is the canonical example: [CODE] // char_sep_example_1.cpp #include <iostream> #include <boost/tokenizer.hpp> #include <string> int main() { … | |
Re: By the way, you have a memory leak in this queue class. I understand the garbage collector takes that responsibility off your back when in Java, but in C++, you are responsible for cleaning up your own mess, like grown-ups do. So, in the remove function, you need to delete … | |
Re: It surely is legal. In fact, you need to have exactly one function named main() when you build an executable. So, if you have a main function in several cpp files, you are in trouble. Usually, cpp files which are associated with a class or a set of utility functions … | |
Re: If you are just interested in visualising a graph, why not use [URL="http://www.graphviz.org/"]graphviz[/URL]? It's a library dedicated to that very purpose. It will probably be easier to just shoot your graph structure into the graphviz functions and get some image files out, instead of using a full GUI tool. | |
Re: >>What kind of bug do you classify the program's problem into? const-correctness. >>I plan to integrate a UI and upload it on sourceforge if I can It's good to do these little projects as learning experiences, but don't mislead yourself into thinking that this kind of code would be worth … | |
Re: line 56 is a simple typo. You declared and set a variable named checkWaitResult a few lines before but then use dwWaitResult. You just need have the same name in both uses. line 139 is reversed. When you use = sign, it stores whatever is on the right into the … | |
Re: There are no freebies here. We can help you if you show that you have done some efforts of your own and if you are stuck on some specific problem(s). Don't expect to just ask and get people to solve your homework assignments. That wouldn't be helpful to you, nor … | |
Re: You need to omit the specialized type from the template <...> declaration. See my edit on your example. As here: [CODE] template<typename T> //the types in here are only those that are unknown. class Point<T, int> { void function1(); }; //similarly: template<> //no type is unknown (full spec.) but you … | |
Re: **Please use code tags** >>int findDuplicate(string a,string b) In C++, strings can be compared with ==. There is no need for this function. >>int i,j=0,flag=0; Declare your variables where you first use them, whenever possible. >>for (i=0; i<(int)stringTable.size();i++) Prefer iterators to indices when doing a simple linear traversal: [CODE] for … | |
Re: The error comes from the fact that declaring a variable as extern does not "declare" the variable, it simply tells the compiler that there is a global variable with that name. Somewhere, you need to have a global variable with that name. You don't have that anywhere. You could just … | |
Re: Where is the copy-constructor in your Mascota class? That's the piece missing, and what an important one! I highly, highly recommend that you use std::string instead of char* and that you store the integer edad by value, not by pointer. Pointers are evil and you are suffering it right now. … | |
Re: Casting from int to enum just requires an explicit cast (it is meant to reduce unintentional (implicit) casts). Just use a static_cast. As so: [CODE] display.setMode(static_cast<VideoOutputMode>(atoi((**vout_mode).c_str()))); [/CODE] | |
Re: Definitely, it is the comparator that is not correct. It should be (strcmp() == 0) (strcmp returns 0 if the strings are equal). If you used a std::string, it would be even simpler. | |
Re: Well, first of all, my recommendation would be to throw away that incredibly stupid library that does this. I can't comprehend how retarded somebody must be to think that making a #define with the name "thread" is not going to cause tremendous name-clashes. Anyways, if you really have to use … | |
Re: Well, do you really have that many alternative strategies? For a test program like this, I don't see much alternative to just listing out the combinations and executing them one after the other. Anyhow, you could just make f, g, and h as static arrays of boost::function<> and loop through … | |
Re: Normally, you do need to lock both the reader and writer such that their operations are mutually exclusing (with a mutex or critical section(windows), personally I would recommend Boost.Thread since it uses the best mechanisms for whatever OS you use). And, if you lock all read and write operations, then … | |
Re: This function: [CODE] std::vector<passenger*>& passenger_queue::passengers_at_floor(int floor_) const [/CODE] Has a big flaw, which results in the error you are getting. Since the function is const, it means that you only have read-only access to all its data members. But then, you try to return a non-const reference to a data … | |
Re: I think that the easiest way to do it is to simply use a functor with both overloads. Something like this for example: [CODE] struct adaptSR_type { typedef int result_type; template<typename accStrategy> int const operator()(int const MAX_AL, float const CONFIDENT_LV, int const OFFSET, accStrategy PLAN) { return 1; } int … | |
Re: My rule of thumb to decide whether to use free functions or class member functions is as follows. First of all, if you need to keep track of a state of some kind (like an opened file or array/vector or any other construct like that), then you need a class … | |
Re: I think that a good old dictionary can give you a pretty good start: "auxiliary" and "helper" are synonyms in English. And, as far as I know, they are synonymous in programming terminology as well. "Utility functions" are similar in some sense but slightly different. A "nonmember" function is obviously … | |
Re: >>A dinosaur will pop out of your computer and eat you if you do. I wish it could be required by the C++ standard to devour any programmer who writes a "goto" statement. >>I don't understand why goto's are so bad. A goto statement is a relic from the times … | |
Re: It's totally fine. In fact, it is a safe way to do it. But it is kinda useless as is, because if the caller has access to the vtable pointer (to look up the virtual call to func()), then it can also implicitly cast the object pointer or reference to … | |
Re: If you are going to use a singleton, do it correctly. The code that L7Sqr posted has two big flaws: before calling instance(), the pointer "only_copy" is not necessarily NULL, and thus, the test (!only_copy) is likely to be false even on the first use of instance(); second, there will … | |
Re: There are two fundamental ways to do this (at least what I can understand of it, I think a more concrete example would be better): Composition and Inheritance. In the case you presented first (with A, B, C, and D), I think inheritance seems most appropriate (although the example is … | |
Re: No. ICOL is not a global variable, it is a #define. A #define is basically just a token (i.e. ICOL) and an expression (i.e. 32). Basically, the preprocessor (a basic parser that looks at your code before it gets compiled) will look through all the code and do a basic … | |
Re: This is very weird. The problem must be a sort of stack wind-up of the for-loop (at low-level a for-loop is turned into a "label, set of instructions, and goto the start label", so if "set of instructions" increases the size of the stack you get a stack wind-up). But … | |
Re: The printf function is not type-safe (it is from C, which is only weakly typed). The printf interprets its parameter types (after the formatted string) based on the format values in the string. So, if you have %d in the string, it will interpret whatever the second parameter is as … | |
Re: >>is there any additional overhead for the first case? Yes, of course. In the first case, you needlessly create an instance of the Log class. And then, when you call the member function, the this pointer is implicitly passed as a first "hidden" parameter to the function, so that's a … | |
Re: >>thinking about it made me come up with this. could this work? Yes it does. (well, you are missing one closing parenthesis) But, there is a better way. The problem with your implementation is that you are doing a lot of useless copying (you create a "large" vector and push … | |
Re: When the const is at the leftmost place, it is attached to the return value. In that = operator, it means that the function returns a const reference to itself. When the const is at the rightmost place (after the parentheses), it is attached to the object to which to … | |
Re: >>Do I need to pass it as arguments if I want to implement it by std::tr1::bind? If the AnyFunctor needs arguments when called, you need to get those arguments from somewhere. So the idea, like in your last piece of code, to create some temporary variable with some arbitrary value … | |
Re: You should bundle the string and CGameSlot into a struct and make a list of those. Then, you can define the < operator such that structs are compared by the string. For example: [CODE] struct Struct1 { string User; CGameSlot PlayerSlot; Struct1(const string& aUser, CGameSlot aPlayerSlot) : User(aUser), PlayerSlot(aPlayerSlot) { … | |
Re: Well, primitives are generally thread-safe just by the virtue of value-semantics and their primitive nature (as in, any read/write operation on them is atomic, i.e. it's extremely unlikely that a thread switch would occur in the middle of the operation, which is really where mutexes are required). For larger classes, … | |
Re: Are you sure that this line: Calculate Deposit_Amount = (0.25*Loan_Amount) + $5,000 shouldn't be: Calculate Deposit_Amount = (0.25*(Loan_Amount - $50,000)) + $3,750 and similarly for the other ones. That would seem to make more sense to me (at least, that's how income tax is calculated). I mean, the deposit amount … | |
Re: The win32 API requires wide-char strings. You need to add a leading L in front of the literal string to tell the compiler to create a wide-char literal string: [CODE] windowClass.lpszClassName = L"WindowClass"; //notice the L in front. [/CODE] And similarly for the other place where the error occurs. | |
Re: aPtr is a pointer to an array of "double" variables. When you want to copy one NumberArray object into another, the new object has to allocate a new array of doubles which can contain as many elements as the other object, and then copy all the elements from one array … | |
Re: This, to my knowledge, is not a particularly common pattern, especially in OOP. It is, however, somewhat ubiquitous in Generic Programming (which can be used alongside OOP). In OOP, I think they are mostly referred to as [I]mixins[/I] (as in, mixing in different classes to make a sort-of bast_ard superclass). … | |
Re: First, the way that you can test whether you have run out of memory in the program is to check that the pointer you get from malloc is not NULL. This pointer will be NULL if you are out-of-memory. This is also why you get the error of dereferencing a … | |
Re: There are a few options. But this is a classic problem with templated code. For instance, the STL libraries are not compiled (so basically, standard C++ libraries are open-source). The first option is to put all the definitions of the member functions inside a .cpp file and #include the cpp … | |
Re: >>I am not clear as to what a seed is I think the easiest way to think about it, from a beginner's perspective, is that, since a computer is deterministic (it can't actually produce truly random numbers), the best it can do is use a formula that produces "very wacky" … | |
Re: Well, I did a bit of image processing, so I know the basic theory. I can't explain it all but I can give some keywords and methods for you to look up. First, the problem of resolution, cropping, geometric disturbance (rotation translation scaling etc.) is pretty trivial to solve really. … | |
Re: This is the usual loop to use: [CODE] #include <fstream> #include <iostream> #include <vector> using namespace std; //define your SalesRecord. int main() { vector<SalesRecord> data; //storage for all the SalesRecords. ifstream CheckFileStream; CheckFileStream.open("SalesRecord.txt",ios::binary); if(CheckFileStream.is_open()) { SalesRecord tmp; //temporary storage for the one element to be read. while(CheckFileStream.read((char*)&tmp, sizeof(SalesRecord)) data.push_back(tmp); //adds … | |
![]() | Re: >>I am not clear as to what a seed is I think the easiest way to think about it, from a beginner's perspective, is that, since a computer is deterministic (it can't actually produce truly random numbers), the best it can do is use a formula that produces "very wacky" … ![]() |
Re: I can't answer those questions directly because they sound too much like assignment questions and you shouldn't be given a freebie (it would help you to learn). However, I can correct some of your underlying misconceptions. >>I've seen some that have 6 characters after the 0x and some that have … | |
Re: First of all, this is highly dependent on the capabilities that you want your plugin system to be able to handle. It can range from a very simple set of capabilities, leading to a fairly easy task to accomplish, to a very comprehensive set of capabilities, leading to a very … |
The End.