Moderators: phlip, Moderators General, Prelates
In Haskell, pairing binds looser than everything else:EvanED wrote:Yakk wrote:The type of your repeat is then:
(A->A, N)->(A->A)
where A is some unknown type, and N is your "repeat count".
Better would be to parenthesize it as
- Code: Select all
( (A->A), N ) -> (A->A)
Pairing binds tighter than function application in every typed functional language I know, so (A -> A, N) would be interpreted as a function of one argument that returns a pair. While there's nothing fundamentally wrong with doing the other thing, it goes against expectations heavily. At least for people who have the background to have expectations.
Prelude> :t (id, 1)
(id, 1) :: Num a1 => (a -> a, a1)
Prelude> :t (\x -> (x, 1))
(\x -> (x, 1)) :: Num a => t -> (t, a)
Prelude> (sqrt 2, 3)
(1.4142135623730951,3)
mittfh wrote:I wish this post was very quotable...
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
chridd wrote:In Haskell, pairing binds looser than everything else:
Prelude> 2, 3
<interactive>:1:2: parse error on input `,'
Prelude> (2, 3)
(2,3)
# let f (a,b) = a + b
;;q
val f : int * int -> int = <fun>#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/freeglut.h>
using namespace std;
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(0, 0);
glutInitWindowSize(1920, 1080);
glutCreateWindow("TEST");
GLenum err = glewInit();
if(err != GLEW_OK)
{
cout <<"ERROR: could not load GLEW. " <<glewGetErrorString(err) <<endl;
return 0;
}
cout <<"GLEW loaded, running version " <<glewGetString(GLEW_VERSION) <<endl;
return 0;
}
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
sourmìlk wrote:never mind, I think I found the loop. It's Face -> Material -> Texture -> Image -> PixelBuffer -> GLBuffer -> Face...
I need to find a way around that, though I'm not sure how.
headprogrammingczar wrote:Or better, refactor your code so you aren't looping at all.
To clarify, do you mean that it has a member variable of type image (as opposed to the image class itself being inside the class)?Shivahn wrote:I have a class who has a member that is another class (an image class).
Do any of image's constructors have default arguments?So I tried to overload the constructor so that if no arguments are passed it doesn't do anything (since the first class will end up loading the image on its own later anyway), but I keep getting told that the call to the member object's constructor is ambiguous. I don't really understand this: shouldn't it be obviously the one with no arguments?
mittfh wrote:I wish this post was very quotable...
class BST<T extends Comparable<? super T>>{
BSTNode<T> root;
BST() {
root = null;
}
//...//
private void Insert(T newEl, BSTNode<T> node) {
if(node == null)
node = new BSTNode<T>(newEl);
else if(node.El() == newEl) //no repeats
;
else if (newEl.compareTo(node.El()) > 0)
Insert(newEl, node.Right());
else
Insert(newEl, node.Left());
}
public void Insert(T newEl) {
Insert(newEl, root);
}
//...//
BST<Integer> tree = new BST<Integer>();
Integer i = new Integer(7);
tree.Insert(i);Splanky222 wrote:So I accidentally saved a blank program over my Java Binary Search Tree class for my Data Structures class, so I'm rewriting it.
Java references have pointer semantics. A = B means "A now refers to B"
PFNGLFRAMEBUFFERTEXTURE2DPROC glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) wglGetProcAddress("glFramebufferTexture2D");
Terry Pratchett wrote:The trouble with having an open mind, of course, is that people will insist on coming along and trying to put things in it.
Shivahn wrote:The first class has a member that is an instance of the image class, yeah. The image class as a whole is not a member of the first class.
I've only programmed one constructor into the image class, and it does have a default argument.
Something else that may be relevant is that there's also a static member of type image in the class. Also, it looks like I segfault even if I comment out my main loop. Like, all of it.
Edit: Actually, it looks like that is the main problem. If I cut that out it still segfaults, but it's much later. So I clearly have two bugs, but the static member is the biggest problem.
phlip wrote:Does SDL need to be initialised before you use it? Being a static variable, the constructor for your Chunk::tiles variable is going to be run before anything in the main-line of your code is run...
Shivahn wrote:Ok. This is all a bit moot now since I am pretty quickly realizing that since I wrote this when I was terrible-er at coding it's better to start from the ground up. (Incidentally, if anyone has general advice for programming well besides "make things modular" "build and enforce abstraction barriers" and "document everything," I'd really appreciate you mentioning it).
class Image { // This structure holds an image void generateImage(int, int); // Generates a blank image with width and height of whatever's passed to it void generateImage(int width, int height); // Creates a surface to load the image on, and a surface to optimize it on
SDL_Surface * loadedImage=NULL;
SDL_Surface * optimizedImage=NULL; loadedImage=IMG_Load(filename.c_str()); // Loads the image after typecasting the string to a constant character pointer if (loadedImage!=NULL)
{
optimizedImage=SDL_DisplayFormatAlpha(loadedImage); // Optimizes the loaded image and saves it as the optimized image
SDL_FreeSurface(loadedImage);
}
// Checks for errors
if(optimizedImage==NULL)
printf("Error loading image: %s", IMG_GetError()); if (loadedImage != 0)
{
optimizedImage = SDL_DisplayFormatAlpha(loadedImage);
if (optimizedImage == 0)
{ /* handle error */ }
else
SDL_FreeSurface(loadedImage);
}
else
printf("Error loading image: %s", IMG_GetError());Non-const static data members always have to be initialized outside the class, and exactly once. Have a look at StackOverflow.Shivahn wrote:phlip wrote:Does SDL need to be initialised before you use it? Being a static variable, the constructor for your Chunk::tiles variable is going to be run before anything in the main-line of your code is run...
I believe so, that would make sense. How would I go about doing that?
If you don't initialize Chunk::tiles properly, I think that'll be your best bet. Unless there are other errors in your program that I don't know about.Shivahn wrote:And why does it segfault? I don't understand the technical reason. I feel like that's a weird error instead of.. I don't know, something about an undefined function or something.
Shivahn wrote:(Incidentally, if anyone has general advice for programming well besides "make things modular" "build and enforce abstraction barriers" and "document everything," I'd really appreciate you mentioning it).
The first check is good, but the second is ambiguous. There are two possible reasons why optimizedImage might be a null pointer: (1) because IMG_Load failed and the first if statement wasn't executed, or (2) because SDL_DisplyFormatAlpha failed. If you want to be able to distinguish the two cases and not treat failed optimizing as failed image loading, you should write something like below.
Jplus wrote:This is a better comment, because it adds information. There is, however, a more concise way to document your code:
- Code: Select all
void generateImage(int, int); // Generates a blank image with width and height of whatever's passed to it
- Code: Select all
void generateImage(int width, int height);
By the way, NULL has no special meaning in C++ (it's just the integer value 0) and in order to be portable you have to #include <cstdlib> for it, so usually we prefer to initialize and compare pointers simply with a literal 0.
const // this is a const object...
class {
public:
template<class T> // convertible to any type
operator T*() const // of null non-member
{ return 0; } // pointer...
template<class C, class T> // or any type of null
operator T C::*() const // member pointer...
{ return 0; }
private:
void operator&() const; // whose address can't be taken
} nullptr = {}; // and whose name is nullptrconst // this is a const object...
class nullptr_t { // of type nullptr_t
public:
template<class T> // convertible to any type
operator T*() const // of null non-member
{ return 0; } // pointer...
template<class C, class T> // or any type of null
operator T C::*() const // member pointer...
{ return 0; }
private:
void operator&() const; // whose address can't be taken
} nullptr = {}; // and whose name is nullptrYakk wrote:gcc 4.6 supports it. MSVC 10 supports it.
Technical Ben wrote:PS, doogly, way to miss the point.
Technical Ben wrote:PS, doogly, way to miss the point.
Parallel.Invoke(() => QuickSortFast(listOfNumbers, leftBound, _partition - 1), () => QuickSortFast(listOfNumbers, _partition + 1, rightBound));Jplus wrote:Finally, you can improve a little by delaying the insertion sort until the end. If the subrange is shorter than your insertion sort threshold, just return directly from the quicksort function. When the topmost quicksort has returned, do a single insertion sort pass over the entire array.
Users browsing this forum: Carnildo, Fekeenuisance and 9 guests