2,898 Posted Topics

Member Avatar for user543820

First of all, keeping a sorted linked-list is never useful, because it requires linear time for a look-up and linear time for insertion. Keeping it unsorted requires linear time for look-up and constant time for insertion, tha's much better. But I guess you are doing this as an exercise. The …

Member Avatar for mike_2000_17
0
334
Member Avatar for dospy

First of all, the second case will not compile, because the compiler will say "invalid use of incomplete type 'Point'". When you forward declare a class, you just tell the compiler that there is a class called Point, defined later. Since it is not yet defined (thus, incomplete), the compiler …

Member Avatar for dospy
0
116
Member Avatar for dospy

>>i've seen in some sources that some classes have Getters and Setters even for public data members Remember that a large amount of open-source software is designed like a steaming pile of dog... Don't consider everything you see as "good practice" or as if the design choices they made were …

Member Avatar for dospy
0
195
Member Avatar for pseudorandom21

AD is right, because of name-mangling in C++ (and ABI), you have to wrap all your C++ code into a C API (C functions). For example, say I wanted to "export" the following class from a DLL: [CODE] class Foo { private: double value; public: double GetValue() const { return …

Member Avatar for mike_2000_17
0
778
Member Avatar for YodaMerlin

What you need to do is dereference the y pointer. There are a couple of ways to do it: 1) the * (star) is a dereference operator when applied to a pointer variable: [CODE] int x = *y; //gets the value that y points to. [/CODE] 2) the [i] is …

Member Avatar for YodaMerlin
0
161
Member Avatar for ChaseRLewis

When your code grows larger, one book becomes essential: [URL="http://www.gotw.ca/publications/c++cs.htm"]C++ Coding Standards[/URL], by Sutter and Alexandrescu. And probably reading through a few of the GotW website is also a good idea. The [URL="http://www.boost.org/community/error_handling.html"]Boost guidelines[/URL] are also very good. And [URL="http://www.boost.org/community/exception_safety.html"]this article[/URL] too. Other than that, I have rarely seen any …

Member Avatar for mike_2000_17
0
156
Member Avatar for eskimo456

This is called a circular referencing problem (Particle needs MapGrid, which needs Particle, which needs MapGrid, ... ad infinitum). Headers guards won't solve that problem. You need to either break the circular referencing through a (better) design of the software, or you can turn the circular referencing to a semi-circular …

Member Avatar for mike_2000_17
0
122
Member Avatar for Labdabeta

The syntax is: [CODE] class Thing { public: template <typename T> operator T(); //notice, no return type (it is implicit for conversion operators) }[/CODE] And it certainly is possible (at least it works on GCC). I'm not sure why you would want to do that, but you can.

Member Avatar for Labdabeta
0
99
Member Avatar for VBNick

Yes there is. First of all, your solution of adding an int variable to the Base class that the most derived class will set upon construction is a very basic form of RTTI (Run-Time Type Identification). But, C++ already has RTTI, and it can be used in two ways, via …

Member Avatar for VBNick
0
236
Member Avatar for a.muqeet khan

**Please use code-tags in the future** The following expression [ICODE]numerater/denominater[/ICODE] is a division of integers, this will be computed with integer arithmetics (the remainder of the division is discarded, i.e. 1 / 2 = 0, 4 / 3 = 1, etc.). To obtain the answer of the division as a …

Member Avatar for VernonDozier
0
191
Member Avatar for dospy

The << and >> operator do have higher precedence than the bitand & operator, see [URL="http://cppreference.com/wiki/language/operator_precedence"]this table[/URL]. However, I would not recommend that you rely on operator precedence in that kind of fashion. If you need, absolutely need, to use a #define, then wrap everything with parentheses and avoid the …

Member Avatar for dospy
0
171
Member Avatar for cyman29

Just print the 3 last elements of the sorted array: [CODE] //this prints the first three elements (largest elements) for(i=0; i<=2; i++) { cout << x[o[i]] << " " ; } //this prints the last three elements (smallest elements) for(i=0; i<=2; i++) { cout << x[o[15-i]] << " " ; …

Member Avatar for mike_2000_17
0
193
Member Avatar for jackmaverick1

>>Also, are DLLs okay on Ubuntu? I think rubberman answered that. >>What format are the librarys like iostream or cmath in? What format? They are just C++ header files, without the .h at the end. You can check it out for yourself, they are usually located in "/usr/include/c++/4.4/" (or /usr/local …

Member Avatar for jackmaverick1
0
180
Member Avatar for flyboy567

What you need is basically a discriminated union (often called a variant type). This is just a union of an integer and a character along with a flag to tell which type is actually stored in the union. Here is a simple example: [CODE] class CharOrInt { private: union { …

Member Avatar for mike_2000_17
0
158
Member Avatar for Colezy

Just contact the author/distributer of the library and make your case for adding your functionality or fix to the code. If it doesn't change the interface and has good justification for the addition, the author will probably be happy to add it. Otherwise, use an ad-hoc fix, because as arkoenig …

Member Avatar for Colezy
0
116
Member Avatar for dospy

The general rule is that the lifetime of an object ends as soon as the body of the destructor is entered. This would normally mean your code is "illegal". However, it technically isn't entirely. You should still be able to call member functions or access POD data members fairly safely …

Member Avatar for dospy
0
181
Member Avatar for ChaseRLewis

The downside of using extern constants is that they will require linking. Basically, the header tells the code that you are compiling that when you link your application, there will be definitions for those constants available to the linker to find. So the compiler annotates those constants as having to …

Member Avatar for mike_2000_17
0
162
Member Avatar for CSWalls

I personally don't know much at all about hash tables (except for what they are and what they are useful for). But I think [URL="http://www.eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx"]this article by Narue[/URL] is probably a good place to start.

Member Avatar for CSWalls
0
213
Member Avatar for XodoX

A program that does this kind of manipulations is called a "symbolic math" engine or library. For example, softwares like [URL="http://www.maplesoft.com/"]Maple[/URL] or [URL="http://www.wolfram.com/mathematica/"]Mathematica[/URL] use or include symbolic math code. I have never used any free C/C++ library that does that (because it is not really a trivial thing and is …

Member Avatar for mike_2000_17
0
155
Member Avatar for super-duper

"Time Complexities" is really just a measure of what running times you should expect as a function of the amount of data you are working on. It is not a precise measure (at least it rarely is), and can't really be "used". It's more like a measure of how good …

Member Avatar for super-duper
0
114
Member Avatar for pharoah85

It is generally wasteful to compute the square using the pow function, just do [ICODE]g = d*d + e*e + f*f;[/ICODE] (you generally try to avoid using pow for integer powers (2,3,4,...)). Then, the sqrtf is a non-standard function from C (which is provided by most implementations of math.h). You …

Member Avatar for pharoah85
0
217
Member Avatar for nightcracker

Well you could just put a bit more "style" into it: [CODE] std::cout << "ERROR " << event << ": " << (origin ? origin : "?") << ": " << fulltext << std::endl; [/CODE] It takes more lines of code, but it is worth it in terms of clarity. …

Member Avatar for mike_2000_17
0
760
Member Avatar for lochnessmonster

Normally it is done as follows: [CODE] //in stack.h #ifndef STACK_H #define STACK_H //include whatever you need. #include <exception> namespace my_code { //put your stuff in a namespace of your choosing. struct stack_full_failure : public std::exception { const char* what() const throw() { return "Stack is full!"; }; }; template …

Member Avatar for mike_2000_17
0
158
Member Avatar for errorlog2

Learning a programming language involves two things: the syntax and the programming paradigm. The syntax includes all the little rules like = is an assignment and == is a comparison (in C/C++ and related languages), operator priorities, keywords, declaration syntax, etc. These are usually trivial to learn or look-up when …

Member Avatar for ntrncx
0
191
Member Avatar for lochnessmonster

"with user responsibility" == "bad design" Don't use that solution, it is bad, it violates coding standards, it is cumbersome to implement correctly, it is unmaintainable, etc. etc. Just don't do that. The second solution is already a lot better, it's easy, safe, maintainable to some degree, and a very …

Member Avatar for WaltP
0
93
Member Avatar for scarlettmoon

Try removing the sortScores call, because I don't see anything wrong in the avg function. The average should be the same whether the list is sorted or not, so that will tell you if the error is in sort or in avg (and I would bet it is in sort). …

Member Avatar for mike_2000_17
0
381
Member Avatar for subith86

First of all, the code that you posted has several errors. First, the array "b" is not a dynamically allocated array, it is a fixed-size array (deduced from the number of elements between curly braces) that resides on the stack frame of the function. This means that after you return …

Member Avatar for subith86
0
176
Member Avatar for sirko

Threading facilities can be found under [URL="http://www.boost.org/doc/libs/1_46_1/doc/html/thread.html"]Boost.Thread[/URL]. These are essentially the libraries that will form the basis for the upcoming threading standard libraries in C++0x, so better get used to those functions/classes for now, until the standard ones come out. And they are cross-platform, so it doesn't matter much whether …

Member Avatar for rubberman
0
192
Member Avatar for sdr001

You should probably read [URL="http://www.parashift.com/c++-faq-lite/templates.html#faq-35.12"]this[/URL]. Basically, you can't really split template code in the traditional hpp/cpp files. This is because templates don't get compiled until they are used ("instantiated"). So all the code in the cpp file basically gets ignored by the compiler because it doesn't find, in that translation …

Member Avatar for sdr001
0
117
Member Avatar for highflyer8

The code you posted would indicate that fourvector is a class derived from the class threevector. The constructor of fourvector simply forwards the one_two_three parameter to its base class constructor. Remark: "I have seen this syntax used for the constructor of the fourvector:" Run! Just run away from this code! …

Member Avatar for Saith
0
137
Member Avatar for benjybob

Your call to srand() cannot be put in the middle of nowhere like that. You need to put it inside a function body. The common options are at the very start of the main() function, or in the constructor of whatever class you have that needs rand(). EDIT: You should …

Member Avatar for benjybob
0
425
Member Avatar for COL_Milkshake

I'm not sure what's going on, but this looks like some strict syntax issue only (i.e. your compiler is a bit capricious). Here are a few tips that can help: - If you get some expected ';' after an #include statement, just add a ';' on an empty line following …

Member Avatar for template<>
0
110
Member Avatar for anu07

>>By the way I am using turbo compiler 3.0 OMG! That's a 20 year old compiler! This is archaic! Please consider switching to something newer. With that compiler, I'm doubting you could even compile whatever code you would like to compile, "within a C++ program" or not (whatever that means, …

Member Avatar for anu07
0
178
Member Avatar for zack654

>>What programs did you make after learning these things and can you recommend me what I should write with knowledge of these features? Some of my early programs, while at your level, mostly revolved around 3D graphics (simple 2D/3D computer games, simple physics engines, collision detection algorithms, procedural generation of …

Member Avatar for zack654
0
150
Member Avatar for Doughnuts

I'm guessing the point of this program is not to use an integer to store the values and then add them to get the result. Right? Because in that case, the program would just be: [CODE] #include <iostream> using namespace std; int main() { int a,b; cout << "\nEnter first …

Member Avatar for Doughnuts
0
353
Member Avatar for rwarlord

I think the answers so far are very good, but I could have a few things to add, because I love answering good questions like this. >>Memory and CPU efficiencies are important Then don't use the function at(i) like you do at line 16 and 36. The at function includes …

Member Avatar for rwarlord
0
406
Member Avatar for ekailan

You are misusing the sizeof() operator. This operator is computed at compile-time, not at run-time. So, it cannot be used for getting the size of anything that was allocated dynamically (btw, dynamic is usually a synonym of "at run-time" in C/C++). Basically, it outputs the memory required to store the …

Member Avatar for Sky Diploma
0
165
Member Avatar for lochnessmonster

I don't know what made Nichito and caut_baia assume that you are using the win32 API, so let me give a more platform agnostic solution. It is pretty simple really. First of all, your logger should give a [I]no-throw[/I] guarantee for almost everything it does. This could be a hard …

Member Avatar for rubberman
0
107
Member Avatar for harinath_2007

Pretty much in my order of preference (note that it might not be yours): 1. [URL="http://www.codeblocks.org/"]Code.Blocks[/URL] (free, with MinGW GCC) 1. [URL="http://www.microsoft.com/visualstudio/en-us/visual-studio-testing-tools/?WT.srch=1"]Visual Studio 2010[/URL] (or [URL="http://www.microsoft.com/express/Windows/"]Visual C++ Express[/URL] (free)) 2. [URL="http://www.eclipse.org/downloads/moreinfo/c.php"]Eclipse C++[/URL] (free, but you need to install and configure your compiler of choice yourself) Things to look for that …

Member Avatar for mike_2000_17
0
258
Member Avatar for finston

The errors you have posted are due to old (very old.. from the days when 16bit computers where common-place) C code that uses these "far" and "huge" keywords for the pointer type "DIBPTR". These keywords are completely obsolete nowadays. I guess they are not supported by C++ compilers (one of …

Member Avatar for finston
0
568
Member Avatar for ritu143

The * (star) that designates a pointer is linked to the type, not the variable name. That's why I prefer the convention [ICODE]int* ptr;[/ICODE] instead of [ICODE]int *ptr;[/ICODE], just because it makes it more obvious that it is a variable of name "ptr" which is a pointer to an integer. …

Member Avatar for rubberman
0
2K
Member Avatar for g_u_e_s_t

@arkoenig: I understand that the OP didn't show enough effort towards solving the problem himself that it shouldn't be dignified with an answer. You could just have said so. With all due respect, posting a bad solution and implying that he should make it even worse so that he can …

Member Avatar for rubberman
-1
482
Member Avatar for QuantuMechanic

You need to make your arrays of size 4, not of size three. This means, your array declarations should be: [CODE] float IC[] = {1,1,1,0}, K[] = {0,0,0,0}, K1[4], K2[4], K3[4], K4[4]; //notice 4 here. [/CODE] You basically has a problem of overlapping memory (the last element of K1 was …

Member Avatar for QuantuMechanic
0
399
Member Avatar for Howdydoody

Your constructor declares a local array of size "size" called wordList. It will not initialize the data member of DictHash called wordList. I would assume that you use std::map for the type of wordList. In that case, it should work without initialization. Otherwise, you need to use an std::vector for …

Member Avatar for Howdydoody
0
134
Member Avatar for lochnessmonster

I guess it could be implementation dependent, but you can pretty much assume that the std::string is implemented as a std::vector of char (with additional functionality of course). So, the size of the buffer it allocates to store the string literal will probably be either the exact number of characters …

Member Avatar for WaltP
0
168
Member Avatar for L3gacy

C++0x will include a new syntax option for the for-loops that is essentially the same as the foreach. Additionally, the use of for_each and many other <algorithm> will be made a lot nicer with the addition of Lambda expressions. Here is a glimpse: [CODE] #include <iostream> #include <algorithm> int main() …

Member Avatar for L3gacy
0
207
Member Avatar for Labdabeta

Let me take this in the reverse order: 3) Declaring a template function outside a class That's easy, the syntax is as follows: [CODE] class Thing { public: template <typename T> T thingy(); } template <typename T> T Thing::thingy() { //blablabla... }; [/CODE] If Thing is also a template: [CODE] …

Member Avatar for mike_2000_17
0
150
Member Avatar for passcode121

>>i cannot find a good implementation for it !! That's a good thing! The point of a homework problem is not for you to go out and google for a ready made algorithm. Nor is it for you to come to a forum like this one, paste the assignment problem, …

Member Avatar for passcode121
0
1K
Member Avatar for SourabhT

[URL="http://www.boost.org/doc/libs/1_46_1/doc/html/variant.html"]Boost.Variant[/URL] is a small library that is especially made for this case you are describing. It is basically a discriminated union, as vijayan121 suggested, but it is already implemented for you. Of course, in the case of int and char, it is somewhat useless since they are both integer types, …

Member Avatar for mike_2000_17
0
164
Member Avatar for RyanMcMillan

I'm happy that L7Sqr pointed out that IO streams have exceptions as an alternative to those ugly while-loops and successive if-statements. It's certainly the preferred way to go in many cases, and many people, even experienced programmers, forget about this or never knew at all. You can also use exceptions …

Member Avatar for Fbody
0
544

The End.