sepp2k 378 Practically a Master Poster

Can the two features of C++ Function overloading and function templates be applied together ??

Yes. You can define multiple functions with the same name even if one or all of the overloads are template functions. When you do this, the non-templated overloads will be considered first and if none of them fit, the most specific of the templated overloads is chosen. As an example take this:

template<class T>
void f(T x); // 1

template<class T>
void f(T* x); // 2

void f(int x); // 3
void f(int* x); // 4

Now if you call f with an int as an argument, overload 3 is chosen. With an int pointer overload 4 is chosen. With any other kind of pointer overload 2 is chosen. And with any argument that is not an int and not a pointer, overload 1 is chosen.

Look into template specialization. I think that may be what you're referring too.

No, don't. It's perfectly possible to overload templated function definitions using regular overloading as described above. In the majority of cases this is preferable to using template specialization.

For more information see Why Not Specialize Function Templates.

mike_2000_17 commented: Could not have said it better! +13
sepp2k 378 Practically a Master Poster

There are many GUI libraries for Linux/X11. The most popular ones are GTK (the one that Gnome and XFCE use (among others)) and Qt (the one that KDE uses). Both of them run on all major platforms, not just X11.

sepp2k 378 Practically a Master Poster

Your code is wrong. It has a couple of syntax errors and you're accessing your array out of bounds. The last valid index for an array of size n is n-1, so your for-loops should use <, not <=. Also the inner for-loop doesn't ensure that $j is a valid index at all.

Your logic is also wrong.

Your inner for-loop doesn't make any sense at all to me. Why do you use the value of $j directectly? Shouldn't it be an index into the array?

Also what sorting algorithm is this supposed to be? It sort of looks like a weird mix between selection sort and bubble sort. It's not going to work like this.

PS: PHP already has a built-in sorting function, so there's no need to build one yourself except for learning purposes.

sepp2k 378 Practically a Master Poster

You have allocated the array of poitners rowptr, but not the elements of each row.

Not true. He allocated enough memory for all the rows on line 14. He's then making each pointer in rowptr point to one row inside arrayptr on line 17. So that part's fine.

sepp2k 378 Practically a Master Poster

Your types are wrong. Your compiler should have warned you about that. If it didn't, increase your warnings level - that will save you a lot of pain in the future.

arrayptr and rowptr should have types char* and char** respectively. Since you made arrayptr and int-pointer, your second for loop will iterate int-by-int, not char-by-char. So it's skipping over characters because ints are bigger than chars.

sepp2k 378 Practically a Master Poster

It contains a list with a single record in it. That's because you added a condition to the generator. That's not what I did in my example.

In my example I had a simple generator without a condtion attached to it. I then used if-then-else in the body of the list comprehension to replace matching items and keep all other items unchanged. If you do the same thing, you'll get the result you want.

sepp2k 378 Practically a Master Poster

I'm not sure what you mean by that. Both using a list comprehension or fixing the code in your originial post will give you a whole list as the result, not a single record.

sepp2k 378 Practically a Master Poster

I can't reproduce the behavior you're describing.

Your code doesn't produce the output you say it does - your code doesn't even compile. If I correct the code's syntax error, the result will always be the empty list because you have overlapping cases with the less specific case coming first. If I rearrenge the order of your cases, I get the correct output and no entries are duplicated.

Regarding list comprehensions: In its simplest form a list comprehension has the form [expressionInvolving item | item <- items]. This will evaluate the expression expressionInvolving item for every item in the list items. For example you can use a list comprehension to replace each occurence of the string "foo" with "bar" in a list of strings like this:

strings = ["foo", "lala", "lulu", "foo", "baz"]
newstrings = [if string == "foo" then "bar" else "string" | string <- strings]

Now newstrings will be ["bar", "lala", "lulu", "bar", "baz"]. The same logic can be applied to solve your problem.

Note that in simple cases like this a single call to map can do the same thing as the list comprehension. Also in real code I would move the if-then bit in its own named function for readability.

sepp2k 378 Practically a Master Poster

a) In Java we usually speak of "object references", not "memory addresses". Memory addresses are a low-level concept that isn't really visible to Java programmers. But yes, when you pass m2 to the Mixer constructor, you're passing a reference to the object that m2 refers to. So the m1 variable of the newly creater Mixer object will refer to the same object as the variable m2.

b) m1 is an instance variable of the Mixer class. So each Mixer object has its own variable called m1. Writing someMixerObject.m1 will access the variable m1 that belongs to that Mixer object.

c) If a Mixer object is created using the default constructor, its m1 variable will be null (because the default constructor does not set m1). If it is created using the one-argument constructor its m1 variable will be the argument passed to that constructor.

So when you create m2 in your main method, its m1 variable is null because you used the default constructor. m3's m1 variables is equal to m2 because you used the 1-argument constructor with m2 as the argument. So when you do m4 = m3.m1, m4 will be equal to m2. And when you do m5 = m2.m1, m5 will be equal to null because, as already said, m2's m1 variable is null.

Since you're not allowed to call methods on null, you get an exception when you invoke m5.do().

JamesCherrill commented: Very clear and comprehensive reply +14
sepp2k 378 Practically a Master Poster

secondString = number_list[1:2] will give you a list that contains the second string as its only element. Then when you do secondString[0:2] you're saying "take the first two elements of this list", but the list only has one element, so you get the same list back unchanged.

What you want is for secondString to be the string directly and not a list containing the string. So you should be doing secondString = number_list[1].

sepp2k 378 Practically a Master Poster

You need it because you don't know whether x_1 is greater or less than x_0. If x_1 is less than x_0, then x_1 - x_0 will be negative and thus less than pres even if x_1 and x_0 are far apart.

sepp2k 378 Practically a Master Poster

You're getting the parse error because you forgot a semicolon on line 2. Whenever you get "Unexpected blabla" at the beginning of a line, there's a good chance that you forgot a semicolon on the line before.

sepp2k 378 Practically a Master Poster

Yes, it does.

PS: Your error function shouldn't be calling maingame() back. This infinite recursion will eventually lead to a stackoverflow error if error is called often enough (though admittedly the user would have to enter invalid input a lot of times before this happens). You should use loops instead.

sepp2k 378 Practically a Master Poster

booleanExpression ? true : false is just a more clumsy way of writing booleanExpression. Other than that (and not using std::string), I don't see anything that needs improvement.

Edit: On second thought you can simplify your setter methods by doing foo = temp_foo; after doing delete [] foo; instead of mallocing memory, strcpying and deleting a second time.

sepp2k 378 Practically a Master Poster

My bad, I meant albero.

sepp2k 378 Practically a Master Poster

Your copy constructor calls your setFoo methods. Your setFoo methods use delete on the old value of the variable that they're setting. When the copy constructor is invoked, the variables won't have old values because the object is brand new. That means that your setFoo methods will call delete on uninitialized pointers. This makes your program crash.

The quick fix would be to set your pointers to NULL in the copy constructor before you invoke your setter methods (deleting a null pointer is safe). A better solution would be to get rid of your char pointers altogether and use std::string instead.

Lucaci Andrew commented: std::string is the way +5
sepp2k 378 Practically a Master Poster

One obvious problem I can see is that you call malloc(sizeof(treepointer)) to allocate memory for a node of the tree, but that will only allocate enough memory to store a pointer.

You need to allocate enough memory to store an entire node, so you need to use sizeof(nodo) as the argument to malloc.

sepp2k 378 Practically a Master Poster

If you let i get larger than 9, it will cause problems because acc is only large enough to hold 10 elements. That is unrelated to your earlier error where acc[i] contained a junk value for reasons that sfuo and I already explained.

sepp2k 378 Practically a Master Poster

when break() occurs, i assumed that it would be a clean break()

It is.

but clearly it isnt turning out so, and acc[i] is catching a junk value.

When break occurs it leaves the loop just as it should. But acc[i] simply isn't initialized. Let's play it through for ip_num = "12" and op_base = 10:

In the first iteration i is 0, ip_num is 12 and acc[0] is set to 2.
In the second iteration i is 1, ip_num is 2 and acc[1] is set to 1.
In the third iteration i is 2 and the loop end because ip_num is 0. Nothing is written to acc[2].

So after the loop i is 2 and acc[i] is a junk value because nothing has been written to acc[2].

sepp2k 378 Practically a Master Poster

You can use the str_replace function to replace each occurence of "." in $description with ".\n", i.e. to insert a newline (\n) after each period. If you want to display your string as HTML, you should use "<br>" instead of (or in addition to) "\n".

Synestic commented: Thanks +0
sepp2k 378 Practically a Master Poster

I think you want if( getVal.indexOf("pepsi") != -1). Note that this also returns true if the given string looks like "FOO,BARpepsiBLA,BAZ". If you want to avoid that you can either split the string or use arrays instead of strings in the first place.

PS: There's no need for loop labels here as you only have one loop.

sepp2k 378 Practically a Master Poster

QRC files are XML files, so comments can be written using the XML comment syntax:

<!-- This is a comment! -->
sepp2k 378 Practically a Master Poster

The braces are optional when the function only contains one statement. If you have more than one statement though, you need them - if you leave them off only the first statement will be interpreted as part of the function and the others will be outside of the function.

It is good style to always use the braces.

sepp2k 378 Practically a Master Poster

You have a plus sign on either side of choice. But on the left side there's no left operand for addition. So the left plus is interpreted as the unary plus operator (which is the opposite of the unary minus (negation) and basically does nothing on numeric operands). Like most numeric operations, the unary plus operator converts its argument to a number. So if choice is not a valid number, you will get NaN.

If you don't want choice to be converted to a number, you should remove the left plus from your call to alert.

PS: You should avoid using global variables like you have in your code and instead make choice a local variable that you pass to error as a parameter.

sepp2k 378 Practically a Master Poster

That error message usually means that you have a missing } somewhere. Proper indentation and an editor that highlights matching braces help with finding that kind of error quickly.

sepp2k 378 Practically a Master Poster

You're missing a semicolon at the end of line 40, so the parser is confused when it sees the variable at the beginning of line 41.

sepp2k 378 Practically a Master Poster

In Firefox it's in the menu under Tools -> Web Developer -> Web Console (shortcut: Ctrl + Shift + K).

sepp2k 378 Practically a Master Poster

Your else branch is missing a closing }.

As a tip for the future: Many browsers have some sort of Javascript console, which lists any errors that were encountered while trying to execute the Javascript on the current page. Looking at the messages in that console will help you find errors like this quickly.

sepp2k 378 Practically a Master Poster

Your Javascript shows a bit of a Python influence. In Javascript you use curly braces to denote the beginning and end of a function body - not indentation. And there's no colon after the function signature.

sepp2k 378 Practically a Master Poster

The problem is unrelated to the fact that test takes a variable number of arguments. The problem is that you're calling test as if it was a method of the VariableArguments1Test class, which it's not.

If you want to call a static method of another class, you'll need to specify the class when calling it (or use a static import). So you need to write VariableArguments1.test instead of just test.

sepp2k 378 Practically a Master Poster

csc.exe is the C# compiler that comes with Microsoft's .net SDK. mcs is the C# compiler that comes with Mono. Neither of these ship with MonoDevelop, but you need to have one of them installed in order to use MonoDevelop.

So if you want to find csc.exe, you should look in the directory where you installed the .net SDK, not in the directory where you installed MonoDevelop. And if you're not on Windows, you should be looking for mcs instead (which will presumably be somewhere in your PATH).

sepp2k 378 Practically a Master Poster

Also you create a new Random object each time you call RandomNumber. This won't cause a compilation error or anything, but it will cause RandomNumber to return the same number repeatedly when invoked multiple times in a short period of time (e.g. in a loop).

You should create a new Random object exactly once and then always use that one instance.

sepp2k 378 Practically a Master Poster

How to implement sizeof api. Means sizeof is already there but i do not want to use that.

You can get the size of a given type T by doing by adding 1 to a T null pointer and then converting the result to a size_t. You can get the size of a variable by taking the address of the variable and the address of the variable + 1, convert both to char* and then calculate then subtract them.

Note that that this technically invokes undefined behavior because it creates pointers to illegal memory, but to the best of my knowledge there is no commonly used compiler on which this would cause a problem. That said there is no reason to do this instead of sizeof other than intellectual curiosity.

I think that we are passing "array" which is pointer to an integer (or first element of array) to f. How does f know that array has 3 elements?

It doesn't. If p is a pointer to int, sizeof(p) will return sizeof(int*), it will not give you the size of the array that p points to. The only context where sizeof tells you the size of an array is if you have an actual array (not a pointer) of static size or, in C99 and above, a VLA. If you pass an array to a function, it will be passed as a pointer and it will not be possible to access the arrays size inside the …

sepp2k 378 Practically a Master Poster

@Fulctrum Your first input function is broken - it returns the address of a local variable.

sepp2k 378 Practically a Master Poster

Image.FromStream throws an ArgumentException if you call it with null as an argument or if the given stream does not contain an image in a valid (and supported) format.

Since you set streamImage on the line above, it is probably safe to assume that it is not null. So the likely explanation is that the data in byteImage is badly formatted or corrupted.

sepp2k 378 Practically a Master Poster

Your code doesn't seem to match your description at all. You said the function should return a Rectangle, but the function in the code actually takes a rectangle (that it never uses) and returns an int.

Your function also calculates the area of the rectangle that the user entered which your description didn't say anything about. The calculation of the area should really happen in its own function.

PS: In C++ you don't need the struct keyword when declaring variables or parameters of a struct type.

TrustyTony commented: Observant, good suggestion to separate calculation and printing +12