2,898 Posted Topics
Re: >>to use even more stl, To use even more STL, with C++0x: [CODE] #include <vector> #include <algorithm> #include <functional> #include <iostream> int main () { using namespace std; std::vector< int > items; for (int i = 0; i < 25; ++i) items.push_back (i % 3); std::map<int,int> counts; std::for_each( items.begin(), items.end(), … | |
Re: >> i have problem compiling this code Post the compilation errors. You may not understand them, but we do. It is hard to know the error, especially since you are using a lot of non-standard functions and headers (gotoxy(), randomize(), random(), clreol(), etc.). | |
Re: Your expression "shapes[i] - shapes[1]" takes the difference between the two pointers, not the two objects. You need to dereference the pointers before you apply the difference operator. As so: [CODE] *(shapes[i]) - *(shapes[1]) [/CODE] Also, I would suggest you make your operator - a friend function instead of a … | |
[B]Beginner's Guide to C++0x: The Big Five - Implementing a Resource-holding RAII Class[/B] This tutorial will show how to implement a [I]resource-holding class[/I] showing practical use of [I]C++0x features[/I]. A resource-holding class is any class which holds, with its data members, a resource which could include: [LIST] [*]dynamically allocated memory … | |
Re: You probably need to use an external library of some kind. [URL="http://freeimage.sourceforge.net/"]FreeImage[/URL] is one which is easy to use, supports most standard image formats and allows you to do some basic transformations on them. If you want to do more advanced transformations on the image, then you can use [URL="http://opencv.willowgarage.com/wiki/"]OpenCV[/URL]. … | |
Re: >>Or am i making this way harder than it should be? Seems like a totally reasonable approach. But if you read one string at a time, the default behavior is to read a word at a time. Read each word, check for being "T" or "H" or other (invalid input), … | |
Re: You cannot (easily) separate the implementation and the declaration of a template. The compiler needs to have access to the implementation in order to instantiate the template for a given set of template-arguments. [URL="http://www.parashift.com/c++-faq/templates.html#faq-35.12"]Read this[/URL]. Simply put all your implementations in the header file (either inside the declaration of the … | |
Re: First of all, the istream iterator gets initialized at the line 18, and so does the end iterator (default-constructed iterator marks the end). Second, the while loop simply iterates until begin reaches the end of the input. That is what the [ICODE](begin != end)[/ICODE] condition verifies. Third, the [ICODE]*begin++[/ICODE] dereferences … | |
Re: Ok... please post the relevant code. Have you tried to figure out the exact point where the program crashes? | |
Re: This forum is not a repository for posting homework assignment questions. We don't do your homeworks for you, it is not in your interest or anybody else's. Try to solve the problem yourself, and if you have problems with [B]specific[/B] things, post them back here, show [B]your[/B] code that demonstrates … | |
Re: It gives the reason why directly above the function definition: [CODE] 45 // utility function called by insertNode; receives a pointer 46 // to a pointer [B]so that the function can modify pointer's value[/B] [/CODE] If you simply pass the rootptr, any modification to the root-pointer with the function will … | |
Re: Don't use the standard libraries with the .h extension, they are out-dated (pre-1998) and are only kept around for backward compatibility and I wouldn't expect them to be updated to work fully on newer implementations (OS). Use the following headers instead: [CODE] #include <iostream> #include <string> #include <cstdlib> #include <cstdio> … | |
Re: Sure, why would you not be allowed to? Are you talking about several functions with the same name (overloads). Then, yes, you are also allowed to mix function template and normal functions, even when overloading (but you should avoid specializing function templates if you provide other overloaded functions, that's the … | |
Re: >>And of course, is it WORTH passing ints by reference? Certainly not by const-reference. Ints, like pointers and references, are usually of the native word size of the computer, which is 32bit or 64bit on most PCs. So, making a new int variable (i.e. copying it) and passing it by … | |
Re: >>Why is it a bad idea to use the std namespace for my new functions? You should not put anything in the std namespace. There are only very rare (and advanced) reasons to put anything in the std namespace. And there is nothing special about the std namespace in this … | |
Re: I would make a class to store all the information relevant to a team, with some member functions to manipulate that data. For example: [CODE] class Team { public: std::string name; std::vector<Player> players; int current_standing; //... }; [/CODE] I would probably suggest you store the data in a [URL="http://www.cplusplus.com/reference/stl/map/"]std::map[/URL] which … | |
Re: While you are using C++0x, then you might as well do: [CODE] my_list.remove_if( [](int x) -> bool { return x == 4; } ); [/CODE] | |
Re: I agree with Narue. Collecting a bunch of "fundamental" types into one header file is fairly common, as long as those types constitute a cohesive group of related types. The fact that you cannot find a better name than "types.h" sort-of hints to the fact that they do not form … | |
Re: You have one too many parenthesis on line 10, it should be: [CODE] float K5 = ((k0 * nu * pow(cos(lat),3))/6) * (1 - pow(tan(lat),2) + (esq * pow(cos(lat),2))); [/CODE] Notice that there is no parenthesis just before [ICODE]pow(tan(lat),2)[/ICODE], this was causing the subtraction to distribute over the next term, … | |
Re: Lets see what your code does, and clarify it. [CODE] int l = 1; for(l = l; l > 0; l--) [/CODE] The statement [ICODE]l = l[/ICODE] is useless, it does nothing. Replace it with the previous line. [CODE] for(int l = 1; l > 0; l--) [/CODE] Now, it … | |
Re: Just a thought: >>I am building a library of matrix and vector operations. >>then I must write a function for each of those operations. It seems odd to me that you are building a library for matrix and vector operations and you are complaining that you need to write all … | |
Re: The form [ICODE]std::string test("hello");[/ICODE] is called a "direct-initialization". This is essentially a direct call to whatever constructor matches the parameters given. Direct-initialization is the only accepted form in a class constructor's initialization list. The form [ICODE]std::string test = "hello";[/ICODE] is called a "copy-initialization", because it is meant as a initialization … | |
Re: Q1)Can we delare constant data members inside a class? Yes. As a static member with inline initialization if it is a integral type or with a definition statement outside the class if it is not an integral type. As a non-static member, you need to initialize the data member in … | |
Re: nmaillet is correct. But in response to: >>Also if you see anything in my code that needs to be fixed feel free to tell me about them. I don't see anything that _needs_ to be fixed, but certain things could be made better. First, think hard before you use the … | |
Re: All you need is to rewrite the value with 2 digit precision after the dot. Look at [URL="http://www.cplusplus.com/reference/iostream/manipulators/setprecision/"]setprecision[/URL]. There are examples that do exactly what you need. Just read the numbers from the input file, and write them to the output file with the setprecision functionality. | |
Re: The STL stands for Standard Template Library. Like any other template library, it is a header-only library (except for some low-level things, maybe). You already have the source if you have the headers, just look for them. Under linux, they will be either in /usr/include/c++ or /usr/local/include/c++. Under windows, it … | |
Re: Is there a reason why you are using such an old compiler? I'm sure that red-hat has some more recent package of GCC available. And yes, you need the function AccumSq to be declared sometime before you use it. | |
Re: So.. [ICODE]$ sudo zypper in codeblocks[/ICODE] didn't work? | |
Re: >>Is it mandatory to Use static functions for a Singleton class. I guess you could make a poor implementation of a singleton without a static function, but it is not usual or recommended. Usually the two options are a static function or a free function (non-member function). The implementation that … | |
Re: From the print out, it is clear that the stride is correct, so the use of M_pGSLData->tda is not going to solve the problem, although it is recommended to use that value and not assume another (like M_uCols). Clearly, the value of the pointer M_start is being set to the … | |
Re: hash_map is deprecated. You should use [URL="http://en.cppreference.com/w/cpp/container/unordered_map"]unordered_map[/URL] instead. It is a C++0x standard feature, but most compiler vendors support it already, without having to compile with the C++0x experimental support flag. Other than that, I am not familiar with std::hash, so I don't know how to solve your problem. But … | |
Re: Well, using shared_ptr is definitely the best thing to do in this case (I use them a lot for these kind of cases). The way to have the life-time of the object under your control is to dynamically allocate it and thus, deallocate it when it should. The problem is … | |
Re: The parameters of a function only exist as long as the function is executing. When the function has finished, its parameters disappear. They don't need to be "reset" before the second call. When you call the function, you provide the arrays it should work on, and the second time you … | |
Re: First, in response to thelamb and JasonHippy, dynamic_cast is not needed in this case, and if you have created the CVSystemDLL class in a separate DLL, then dynamic_cast will not work in the exe (if that is what is being done). Second, >>If it's related to the problem, the code … | |
Re: >>Why do you ask? Because it is a homework problem in which the prof said not to use STL algorithms, I guess. This is a typical homework problem to teach people how to do basic string or array manipulations. Ironically, it is also a typical interview question to see if … | |
Re: I have not heard or seen any book or resources online which are a "generic guide to using external libraries in C++". I think the problem with writing such a guide is the shear amount of possibilities. There are APIs, header-only libraries, source-file-only libraries, different programming paradigms, some have special … | |
Re: LOL! @WaltP: Pretty good timing for this thread! Don't you think? Yet another example of "I compile this with DevC++ and it doesn't work, but it works with other IDEs like ..". @OP: I agree with both posters. Stick to one generation of C++ code, that is, the current standard, … | |
Re: Examples of real serialization methods or libraries are: [URL="http://www.boost.org/doc/libs/1_46_1/libs/serialization/doc/index.html"]Boost.Serialization[/URL] [URL="http://code.google.com/p/protobuf/"]Google's protobuf[/URL] [URL="http://www.json.org/"]JSON[/URL] Serialization requires two things: a way to deconstruct an object tree into a buffer from which the tree can be reconstructed (which usually requires some form of type identification), and a way to read and write the buffer … | |
Re: @m4ster_r0shi: >>Something like this would work fine: (overloading operator >>) I should point out that operators >> and << are left-associative, so overloading operator >> like you did is going to be problematic when combining several calls. So, this is not recommended, especially since it inverts the normal semantics of … | |
Re: >>Definitely a bunked include path Try to locate the header files manually in the hard-drive. Possibly, add all the sub-directories to the include path list. Make sure you have all the dependencies met. | |
Re: >>Anyone can share their knowledge here?? First of all, don't expect this approximate series to give reasonable results for x > 1.0 (and less than -1, of course), there are theorems that prohibit that. Also, you need to compile with highest optimization level for this kind of applications. I actually … | |
Re: This really is eating me inside because I remember seeing the exact same symptoms in a project. It was a very long time ago, and I'm having trouble remembering what the problem was, but I do remember it was painful to find. I can at least say a few things: … | |
Re: >>CB current version is 10.5 and is distributed with gcc version 4.4.1 copyright 2009. Is that not the same as current version of MinGW without CB? The current release version of GCC is, of course, 4.6.1 (from June 27 2011). Of course, it takes a little while longer to get … | |
Re: The problem is clear, and has been answered already. std::bad_alloc is the exception that is thrown when dynamic memory allocation fails. Generally, the most likely reason why dynamic allocation can fail is because it would require more memory than you have available on your computer. When allocating a 150,000 x … | |
Re: You can use Qt. Do this in a terminal window: [CODE] $ sudo apt-get install libqtcore4 libqtgui4 libqt4-dev qt4-qmake qt4-doc qt4-demos qt4-assistant qt4-designer qtcreator qtcreator-doc [/CODE] Some of the above might be redundant, but what the heck. And most of it is probably installed already as part of the distro … | |
Re: >>ok so i'll always need a default constructor even when i add other constructors You can do both at the same time using default parameters, this is very common. For example, consider this simple class that represents a 2D vector: [CODE] class Vector2D { public: double x,y; //two data members … | |
Re: The best for C++0x right now is certainly GCC 4.6.1. If you are willing to build from source (which could be pretty hard for Windows), then you can also use 4.6.2. The current development version is 4.7.0, but it is experimental, and from personal experience with it, I don't recommend … | |
Re: The answer is: - I don't know. - [URL="http://www.boost.org/doc/libs/1_46_1/doc/html/program_options.html"]Boost.Program-options[/URL] - [URL="http://www.boost.org/doc/libs/1_46_1/doc/html/date_time.html"]Boost.Date-time[/URL] Let me elaborate: For the first problem, "IsDebuggerPresent", it is the first time I hear of such a function, I have no idea if it has or can have an equivalent in Linux. As far as I know, you … | |
Re: You forgot to implement the assignment operator, which usually implies implementing a swap function as well. As so: [CODE] class Street { private: int stretNumber; string name; int numberHouses ; int *ListHouses ; public : Street(); Street(int, string, int); //Street(const Street & otherStreet); Street(const Street & Street1); void destroyHouses(); void … | |
Re: As m4ster_r0shi said, you can use a different comparator to get a descending order for the sorting. I would just add that you can use the standard one instead of a custom compare() function, as so: [CODE] std::sort(fitness, fitness + 250, std::greater<double>()); //the default comparator is 'std::less<double>()' [/CODE] And yes, … |
The End.