Search Results

Search found 3255 results on 131 pages for 'pointers'.

Page 15/131 | < Previous Page | 11 12 13 14 15 16 17 18 19 20 21 22  | Next Page >

  • Passing functor and function pointers interchangeably using a templated method in C++

    - by metroxylon
    I currently have a templated class, with a templated method. Works great with functors, but having trouble compiling for functions. Foo.h template <typename T> class Foo { public: // Constructor, destructor, etc... template <typename Func> void bar(T x, Func f); }; template <typename T> template <typename Func> Foo::bar(T x, Func f) { /* some code here */ } Main.cpp #include "Foo.h" template <typename T> class Functor { public: Functor() {} void operator()(T x) { /* ... */ } private: /* some attributes here */ }; void Function(T x) { /* ... */ } int main() { Foo<int> foo; foo.bar(2, Functor); // No problem foo.bar(2, Function); // <unresolved overloaded function type> return 0; }

    Read the article

  • How to determine 2D unsigned short pointers array length in c++

    - by tuman
    Hello, I am finding it difficult to determine the length of the columns in a 2D unsigned short pointer array. I have done memory allocation correctly as far as I know. and can print them correctly. plz see the following code segment: int number_of_array_index_required_for_pointer_abc=3; char A[3][16]; strcpy(A[0],"Hello"); strcpy(A[1],"World"); strcpy(A[2],"Tumanicko"); cout<<number_of_array_index_required_for_pointer_abc*sizeof(unsigned short)<<endl; unsigned short ** pqr=(unsigned short **)malloc(number_of_array_index_required_for_pointer_abc*sizeof(unsigned short)); for(int i=0;i<number_of_array_index_required_for_pointer_abc;i++) { int ajira = strlen(A[i])*sizeof(unsigned short); cout<<i<<" = "<<ajira<<endl; pqr[i]=(unsigned short *)malloc(ajira); cout<<"alocated pqr[i]= "<<sizeof pqr<<endl; int j=0; for(j=0;j<strlen(A[i]);j++) { pqr[i][j]=(unsigned short)A[i][j]; } pqr[i][j]='\0'; } for(int i=0;i<number_of_array_index_required_for_pointer_abc;i++) { //ln= (sizeof pqr[i])/(sizeof pqr[0]); //cout<<"Size of pqr["<<i<<"]= "<<ln<<endl; // I want to know the size of the columns i.e. pqr[i]'s length instead of finding '\0' for(int k=0;(char)pqr[i][k]!='\0';k++) cout<<(char)pqr[i][k]; cout<<endl; }

    Read the article

  • Question about C Pointers (just learning)

    - by Mike
    I am curious as to why this is an error and what the error message means. Here is some code. int *x[] = {"foo", "bar", "baz"}; int *y[] = {"foo", "bar", "baz"}; x = y; I try to compile and I get this: error: incompatible types when assigning to type ‘char [3]’ from type ‘char *’ Question #1 why is this an error? and Question #2 why are the types different? Thanks for you help.

    Read the article

  • Code formatting when using pointers

    - by HD-VRSCA
    Is there any reason why the asterisk is next to the object type in this code? I'm a little confused by the way I see this used. Some times it looks like this: NSString* stringBefore; and sometimes like this: NSString *stringBefore; Is there a difference? Or a right or wrong way to do this? Thanks

    Read the article

  • Boost Shared Pointers and Memory Management

    - by Izza
    I began using boost rather recently and am impressed by the functionality and APIs provided. In using boost::shared_ptr, when I check the program with Valgrind, I found a considerable number of "Still reachable" memory leaks. As per the documentation of Valgrind, these are not a problem. However, since I used to use the standard C++ library only, I always made sure that any program written is completely free from memory leaks. My question is, are these memory leaks something to worry about? I tried using reset(), however it only decrements the reference count, doesn't deallocate memory. Can I safely ignore these, or any way to forcibly deallocate the memory allocated by boost::shared_ptr? Thank you.

    Read the article

  • calling resize on std vector of pointers crashed

    - by user11869
    The problem can be reproduced using VS 2013 Express. It crashed when internal vector implementation tried to deallocate the original vector. However, the problem can solved by using 'new' instead of 'malloc'. Anyone can shed some light on this? struct UndirectedGraphNode { int label; vector<UndirectedGraphNode *> neighbors; UndirectedGraphNode(int x) : label(x) {}; }; int main(int argc, char** argv) { UndirectedGraphNode* node1 = (UndirectedGraphNode*)malloc(sizeof(UndirectedGraphNode)); node1->label = 0; node1->neighbors.resize(2); return 0; }

    Read the article

  • Retrieving Relationships from within two arrays of pointers

    - by DanF
    In a portion of a program I'm working on, I need to count all the times each person has worked on projects with each other person. Let's say we have "Employee" entities and "Session" entities. In each session, there are four project types, "A", "B", "C", & "D", each a many-to-many relationship to Employees. I'm making a loop to systematically review every person a selected person has worked with. First, I put all their project types in a single array, so it's easier to loop through, but by the time I ask the last nested Project for its Employee members, I get an "unrecognized selector" error. IBOutlet NSArrayController * list; int x; for(x = 0; x < [list count]; x++){ NSArray *A = [[list objectAtIndex:x] valueForKey:@"projectAs"]; NSArray *A = [[list objectAtIndex:x] valueForKey:@"projectBs"]; NSArray *A = [[list objectAtIndex:x] valueForKey:@"projectCs"]; NSArray *A = [[list objectAtIndex:x] valueForKey:@"projectDs"]; NSArray *masterList = [[NSArray alloc] initWithObjects: projectAs, projectBs, projectCs, projectDs, nil]; int y; for(y = 0; y < [masterList count]; y++){ int z; for(z = 0; z < [[masterlist objectAtIndex:y] count]; z++){ //And now to make an Array of this employee's partners on the selected object, to run comparisons on. //I also have an array of keys for each session's teams, so that's what I'm referencing here: NSArray * thisTeam = [list objectAtIndex:y] objectAtIndex:z] valueForKey:projectKey]; This throws an exception... namely, -[_NSFaultingMutableSet objectAtIndex:]: unrecognized selector sent to instance What's wrong with that last Array creation?

    Read the article

  • C++ sort array of char pointers

    - by user69514
    Can you tell me what's wrong with my method? I ends up putting the same thing everywhre and it's actually not sorting. void sortArrays(){ int i, j; for(i=0; i<counter; i++){ for( j=0; j<i; j++){ if( strcmp(title_arr[i], title_arr[j]) < 0){ char* title_temp = title_arr[i]; title_arr[j] = title_temp; } } }

    Read the article

  • Need some pointers/hints in writing a Windows Application

    - by Bragaadeesh
    Hi, I want to create applications in windows that has complete portability (within windows OSes of course). I have tried using one application written in Visual C++ but I had a real tough time in making it run in other windows OS (like it required .net framework libraries to be installed). This put me on the back foot because I had to copy a set of DLLs from one machine to another and most of the time something works some does not. And I am TOTAL amateur in writing windows based applications since my technological forte is mostly Java. Where to kick off? (like which tools/IDEs to begin with since I am seriously into writing my own utilities/tools). I am open to clarification should you guys feel my question is vague/blunt. Thanks.

    Read the article

  • free( ) pointers

    - by user1043625
    I'm required to use a special library to keep track of my memory leaks where malloc()= allocate( ) and free( ) = unallocate( ). I'm trying to complete free a linked-list but it seems like the "root" value isn't being freed. typedef struct _node { struct _node *child; char *command; } Command_list; void delete_commands(Command_list **root) { Command_list *temp; while( *root != NULL ){ temp = (*root)->child; //printf("STRING: %s\n", *root->command ); unallocate( *root ); *root = temp; } } The function that's calling it void file_processing( .... ){ Command_list *root = allocate(sizeof (Command_list)); root = NULL; .... delete_commands( &root ); } } I believe that Command_list *root = allocate(sizeof (Command_list)) isn't being properly de-allocated for some reason. Anyone can give me some hints? UPDATE: I found out that instead of Command_list *root = allocate(sizeof (Command_list)); root = NULL; this works: Command_list *root = NULL;

    Read the article

  • C++ pointers and constructors

    - by lego69
    if I have this snippet of the code A a1(i); A a2 = a1; A *pa1 = new A(a2); can somebody please explain what exactly the last line does, it makes copy of the a2 and pointer for this new object is pa1 or it just creates pointer for a2, thanks in advance

    Read the article

  • C++: Pointers and scope

    - by oh boy
    int* test( ) { int a = 5; int* b = &a; return b; } Will the result of test be a bad pointer? As far as I know a should be deleted and then b would become a messed up pointer, right? How about more complicated things, not an int pointer but the same with a class with 20 members or so?

    Read the article

  • Function to swap pointers in Objective-C

    - by Michael
    - (void) swapController:(MyViewController*)controller1 with:(MyViewController*)controller2 { MyViewController *swap = controller2; controller2 = controller1; controller1 = swap; } Looks like this doesn't work because I'm not passing references. How to do it anyway?

    Read the article

  • How to copy a structure with pointers to data inside (so to copy pointers and data they point to)?

    - by Kabumbus
    so I have a structure like struct GetResultStructure { int length; char* ptr; }; I need a way to make a full copy of it meaning I need a copy to have a structure with new ptr poinnting on to copy of data I had in original structure. Is It any how possible? I mean any structure I have which contains ptrs will have some fields with its lengths I need a function that would copy my structure coping all ptrs and data they point to by given array of lengthes... Any cool boost function for it? Or any way how to create such function?

    Read the article

  • Pointers in c/c++

    - by jammkie same
    include void main() { int p[]={0,1,2,3,4}; int *a[]={p,p+1,p+2,p+3,p+4}; printf("%u %u %u %u",a,a,(*a)); } What should be the output of the above code? And if we make array p as static(static int p[]), output gets changed .Why?

    Read the article

  • Creating a function within a loop (pointers?)

    - by user352151
    Im trying to create a simple loop that creates 50 buttons, adds them to screen and then when a button is pressed, it traces out that number. I can get it to work by doing stuff I consider hacky (such as using the buttons X/Y location to determine its value), but I'd rather just be able to hold a single value in the function. The code itself is: for (var a:int = 0; a < 5; a++) { for (var b:int = 0; b < 10; b++) { var n = (a * 10) + b + 1; var btt:SimpleButton = new BasicGameButton(); btt.x = 20 + b * 50; btt.y = 50 + a * 80; addChild(btt); btt.addEventListener(MouseEvent.CLICK, function f() { trace(n); } ); } } At the moment, whenever a button is pressed, it simply outputs "50". Is there a way of "freezing" the value of n when the function is created, for that function? (BasicGameButton is just a square button, created in the flash library) Many thanks.

    Read the article

  • Grabbing value of pointers

    - by user1205956
    In C++ I am making a static library where I must set two variables equal to the value of the objects sent in to the method. This is what I have: bool setTags(char *pStartTag, char *pEndTag) { // Code to set the tags here. return true; } Basically the calling function puts in two character arrays. I am required to do it this way so I cannot deviate on these ways. How do I set two variables equal to the whole character array that is passed through?

    Read the article

  • Assigning variables to pointers

    - by tys
    When compiling the below, the program seem to crash. However, there is no error in the compiling process. ... int *x; *x = 3; printf("%d", *x); ... From what I know, this program initializes the pointer *x to an integer value, and subsequently assigns the value of 3 to the deferenced pointer *x. So why does the program crashes? If I do this instead, the program can work normally. ... int *x, y; y = 3; x = &y; printf("%d", *x); ... So, what seems to be the problem with the skipping of the y variable, and instead, assigning the pointer *x directly to an integer value?

    Read the article

  • Is it a good practice to always use smart pointers ?

    - by Dony Borris
    Hi, I find smart pointers to be a lot more comfortable than raw pointers. So is it a good idea to always use smart pointers? ( Please note that I am from Java background and hence don't much like the idea of explicit memory management. So unless there are some serious performance issues with smart pointers, I'd like to stick with them. ) Any advice would be greatly appreciated. Thanks.

    Read the article

  • C# Implementing a custom stream writer-esque class

    - by Luke
    How would I go about writing my own stream manipulator class? Basically what I'm trying to wrap my head around is storing the reference to the underlying stream in the writer. For example, when writing to a memory stream using a StreamWriter, when a Write() is made, the underlying memory stream is written to. Can I store the reference to an underlying stream without using pointers or unsafe code? Even if it was just a string I wanted to "write" to. Really this has little to do with stream writers, and I'm just wondering how I could store references in a class. The StreamWriter was the best example I could come up with for this.

    Read the article

  • NSArrays in NSArrays. A pointer problem?

    - by RyJ
    I believe my problem involves pointers, a concept I often struggle with, but here's what I'm trying to do. I have six NSArrays. I want an additional NSArray comprised of these six arrays, so: self.arr1 = [NSArray array]; self.arr2 = [NSArray array]; self.arr3 = [NSArray array]; self.arr4 = [NSArray array]; self.arr5 = [NSArray array]; self.arr6 = [NSArray array]; NSArray *containerArray = [[NSArray alloc] initWithObjects:self.arr1, ... etc, nil]; Whenever I update one of the first 6 NSArrays, I want the object updated in containerArray. (I know I'm using an NSArray and not an NSMutableArray, when I update the arrays I create a new one and assign it to the instance variable). Currently, any manipulation of arr1 is not reflected in [containerArray objectAtIndex:0].

    Read the article

  • Initialize Pointer Through Function

    - by SoulBeaver
    I was browsing my teacher's code when I stumbled across this: Order* order1 = NULL; then order1 = order(customer1, product2); which calls Order* order(Customer* customer, Product* product) { return new Order(customer, product); } This looks like silly code. I'm not sure why, but the teacher initialized all pointers to NULL instead of declaring them right away(looking at the code it's entirely possible, but he chose not to). My question is: is this good or acceptable code? Does the function call have any benefits over calling a constructor explicitely? And how does new work in this case? Can I imagine the code now as kind of like: order1 = new Order(customer, product);

    Read the article

  • can these be made unambiguous

    - by R Samuel Klatchko
    I'm trying to create a set of overloaded templates for arrays/pointers where one template will be used when the compiler knows the size of the array and the other template will be used when it doesn't: template <typename T, size_t SZ> void moo(T (&arr)[SZ]) { ... } template <typename T> void moo(T *ptr) { ... } The problem is that when the compiler knows the size of the array, the overloads are ambiguous and the compile fails. Is there some way to resolve the ambiguity (perhaps via SFINAE) or is this just not possible.

    Read the article

  • Trim function in C, to trim in place (without returning the string)

    - by user364100
    I can't figure out what to do to make this work. Here's my code: char* testStr = " trim this "; char** pTestStr = &testStr; trim(pTestStr); int trim(char** pStr) { char* str = *pStr; while(isspace(*str)) { (*pStr)++; str++; } if(*str == 0) { return 0; } char *end = str + strlen(str) - 1; while(end > str && isspace(*end)) end--; *(end+1) = 0; return 0; } I get an access violation on *(end+1) = 0;, but I can't declare my testStr[] as such to avoid that, because I can't pass the pointers that way. Any ideas?

    Read the article

  • can these templates be made unambiguous

    - by R Samuel Klatchko
    I'm trying to create a set of overloaded templates for arrays/pointers where one template will be used when the compiler knows the size of the array and the other template will be used when it doesn't: template <typename T, size_t SZ> void moo(T (&arr)[SZ]) { ... } template <typename T> void moo(T *ptr) { ... } The problem is that when the compiler knows the size of the array, the overloads are ambiguous and the compile fails. Is there some way to resolve the ambiguity (perhaps via SFINAE) or is this just not possible.

    Read the article

< Previous Page | 11 12 13 14 15 16 17 18 19 20 21 22  | Next Page >