Search Results

Search found 976 results on 40 pages for 'typedef'.

Page 24/40 | < Previous Page | 20 21 22 23 24 25 26 27 28 29 30 31  | Next Page >

  • C++ function-pointer and inheritance

    - by pingvinus
    In parent class I have function, that operates under an array of functions, declared in child-class, number of functions for every child-class may vary. But since every function uses some object-variables, I can't declare them as static. I've try to do something like this: class A { public: typedef int (A::*func)(); func * fs; void f() { /*call functions from this->fs*/ } }; class B : public A { public: int smth; B(int smth) { this->smth = smth; this->fs = new func[1]; fs[0] = &B::f; } int f() { return smth + 1; } }; But, obviously it doesn't work. Any suggestions?

    Read the article

  • Cannot refer to a template name nested in a template parameter

    - by chila
    I have the following code: template <typename Provider> inline void use() { typedef Provider::Data<int> D; } Where I'm basically trying to use a template class member 'Data' of some 'Provider' class, applied to 'int', but I get the following errors: util.cpp:5: error: expected init-declarator before '<' token util.cpp:5: error: expected `,' or `;' before '<' token I'm using GCC 4.3.3 on a Solaris System.

    Read the article

  • Global list in C/gtk+

    - by sterh
    Hello, I need in global list in my gtk+ application, i use for it GList: For example: I have structure: typedef struct _data { Glist list; }Data; I want to use one copy of the list in the whole program: I have a function bulid my list: gboolean build_list() { Data->list = g_list_append(mw->a, "First "); Data->list = g_list_append(mw->a, "Second "); Data->list = g_list_append(mw->a, "Third "); g_list_foreach(Data->list, (GFunc)printf, NULL); } After calling this function to display all items in the list. zbut when i try to make it in another function - for example: void foreach() { g_list_foreach(Data->list, (GFunc)printf, NULL); } I see error in gdb: *Program received signal SIGSEGV, Segmentation fault. [Switching to Thread 0xb7335700 (LWP 5364)] 0xb765a7d7 in strchrnul () from /lib/i686/cmov/libc.so.6 * How can i create global list in my application? Thank you.

    Read the article

  • Multithreading with STL container

    - by Steven
    I have an unordered map which stores a pointer of objects. I am not sure whether I am doing the correct thing to maintain the thread safety. typedef std::unordered_map<string, classA*>MAP1; MAP1 map1; pthread_mutex_lock(&mutexA) if(map1.find(id) != map1.end()) { pthread_mutex_unlock(&mutexA); //already exist, not adding items } else { classA* obj1 = new classA; map1[id] = obj1; obj1->obtainMutex(); //Should I create a mutex for each object so that I could obtain mutex when I am going to update fields for obj1? pthread_mutex_unlock(&mutexA); //release mutex for unordered_map so that other threads could access other object obj1->field1 = 1; performOperation(obj1); //takes some time obj1->releaseMutex(); //release mutex after updating obj1 }

    Read the article

  • How can a FILE* (pointer to a struct) be tested (if == NULL)?

    - by m4design
    I was playing around with C, anyways I was thinking how can file pointer (which points to a struct type), be tested if NULL as for instant: FILE *cfPtr; if ( ( cfPtr = fopen( "file.dat", "w" ) ) == NULL ) I tried to do that myself, but an error occurs. struct foo{ int x; }; struct foo bar = {0}; if (bar == NULL) puts("Yay\n"); else puts("Nay"); error C2088: '==' : illegal for struct Here's the FILE deceleration in the stdio.h file: struct _iobuf { char *_ptr; int _cnt; char *_base; int _flag; int _file; int _charbuf; int _bufsiz; char *_tmpfname; }; typedef struct _iobuf FILE;

    Read the article

  • C programming: hashtable insertion/search

    - by Ricardo Campos
    Hello i have a problem with my hash table its implemented like this: #define HT_SIZE 10 typedef struct _list_t_ { char key[20]; char string[20]; char prevValue[20]; struct _list_t_ *next; } list_t; typedef struct _hash_table_t_ { int size; /* the size of the table */ list_t ***table; /* first */ sem_t lock; } hash_table_t; I have a Linked list with 3 pointers because i want a hash table with several partitions (shards), here is my initialization of my Hash table: hash_table_t *create_hash_table(int NUM_SERVER_THREADS, int num_shards){ hash_table_t *new_table; int j,i; if (HT_SIZE<1) return NULL; /* invalid size for table */ /* Attempt to allocate memory for the hashtable structure */ new_table = (hash_table_t*)malloc(sizeof(hash_table_t)*HT_SIZE); /* Attempt to allocate memory for the table itself */ new_table->table = (list_t ***)calloc(1,sizeof(list_t **)); /* Initialize the elements of the table */ for(j=0; j<num_shards; j++){ new_table->table[j] = (list_t **)calloc(1,sizeof(list_t *)); for(i=0; i<HT_SIZE; i++){ new_table->table[j][i] = (list_t *)calloc(1,sizeof(list_t )); } } /* Set the table's size */ new_table->size = HT_SIZE; sem_init(&new_table->lock, 0, 1); return new_table; } Here is my search function to search in the hash table list_t *lookup_string(hash_table_t *hashtable, char *key, int shardId){ list_t *list ; int hashval = hash(key); /* Go to the correct list based on the hash value and see if key is * in the list. If it is, return return a pointer to the list element. * If it isn't, the item isn't in the table, so return NULL. */ sem_wait(&hashtable->lock); for(list = hashtable->table[shardId][hashval]; list != NULL; list =list->next) { if (strcmp(key, list->key) == 0){ sem_post(&hashtable->lock); return list; } } sem_post(&hashtable->lock); return NULL; } And my insert function: char *add_string(hash_table_t *hashtable, char *str,char *key, int shardId){ list_t *new_list; list_t *current_list; unsigned int hashval = hash(key); /*printf("|%d|%d|%s|\n",hashval,shardId,key);*/ /* Lock for concurrency */ sem_wait(&hashtable->lock); /* Attempt to allocate memory for list */ new_list = (list_t*)malloc(sizeof(list_t)); /* Does item already exist? */ sem_post(&hashtable->lock); current_list = lookup_string(hashtable, key,shardId); sem_wait(&hashtable->lock); /* item already exists, don't insert it again. */ if (current_list != NULL){ strcpy(new_list->prevValue,current_list->string); strcpy(new_list->string,str); strcpy(new_list->key,key); new_list->next = hashtable->table[shardId][hashval]; hashtable->table[shardId][hashval] = new_list; sem_post(&hashtable->lock); return new_list->prevValue; } /* Insert into list */ strcpy(new_list->string,str); strcpy(new_list->key,key); new_list->next = hashtable->table[shardId][hashval]; hashtable->table[shardId][hashval] = new_list; /* Unlock */ sem_post(&hashtable->lock); return new_list->prevValue; } My main class runs some of tests by executing the insertion / reading / delete from the elements of the hash table the problem is when i have more than 4 partitions/shards the tests stop at the first reading element saying it returned the wrong value NULL on the search function, when its less than 4 it runs perfectly well and passes all the tests. You can see my main.c in here if you want to give a look: http://hostcode.sourceforge.net/view/1105 My complete Hash table code: http://hostcode.sourceforge.net/view/1103 And other functions where hash table code is executed: .c file http://hostcode.sourceforge.net/view/1104 .h file http://hostcode.sourceforge.net/view/1106 Thank for you time, i appreciate any help you can give to me this is a college important project that I'm trying to solve and I'm stuck here for 2 days.

    Read the article

  • C++: retrieve map values and insert into second map

    - by donalmg
    Hi, I have one map within one header file class: class One { // code typedef map<string, int> MapStrToInt inline MapStrToInt& GetDetails(unsigned long index) { return pData[index]; } // populate pData.... private: MapStrToInt *pData; }; And a second class which implements another map and wants to get the first 10 details from the class One's map. class Two { // code One::MapStrToInt pDataTen; int function1() { for (int i =0; i < 10; i ++) { One::MapStrToInt * pMap = &(One::GetDetails(i)); pDataTen.insert(pair<string, int>(pMap->first,pMap->second)); } } When I compile this, it states that pMap: has no member named 'first' has no member named 'second' Any suggestions? Thanks..

    Read the article

  • C++ packing a typdef enum

    - by Sagar
    typedef enum BeNeLux { BELGIUM, NETHERLANDS, LUXEMBURG } _PACKAGE_ BeNeLux; When I try to compile this with C++ Compiler, I am getting errors, but it seems to work fine with a C compiler. So here's the question. Is it possible to pack an enum in C++, or can someone see why I would get the error? The error is: "semicolon missing after declaration of BeNeLux". I know, after checking and rechecking, that there definitely is a semicolon there, and in any places required in the rest of the code.

    Read the article

  • recursively reverse linked list.

    - by Amanda
    I am implementing a function to recursively reverse a linked-list, but getting seg-fault. typedef struct _node { int data; struct _node *next; } Node, *NodeP; NodeP recursiveReverseList(NodeP first){ if(first == NULL) return NULL; if(first->next == NULL) return head; NodeP rest = recursiveReverseList(head->next); rest->next = first; first->next = NULL; return first; } Can you please help. P.S. The iterative version is working fine though. Its not homework. Just practicing C.

    Read the article

  • Looking at the C++ new[] cookie. How portable is this code?

    - by carleeto
    I came up with this as a quick solution to a debugging problem - I have the pointer variable and its type, I know it points to an array of objects allocated on the heap, but I don't know how many. So I wrote this function to look at the cookie that stores the number of bytes when memory is allocated on the heap. template< typename T > int num_allocated_items( T *p ) { return *((int*)p-4)/sizeof(T); } //test #include <iostream> int main( int argc, char *argv[] ) { using std::cout; using std::endl; typedef long double testtype; testtype *p = new testtype[ 45 ]; //prints 45 std::cout<<"num allocated = "<<num_allocated_items<testtype>(p)<<std::endl; delete[] p; return 0; } I'd like to know just how portable this code is.

    Read the article

  • Define and return a struct in c

    - by nevan
    I'm trying to convert some code from Javascript to c. The function creates an array (which always has a fixed number of items) and then returns the array. I've learned that in c it's not straightforward to return an array, so I'd like to return this as a struct instead. My c is not all that great, so I'd like to check that returning a struct is the right thing to do in this situation, and that I'm doing it the right way. Thanks. typedef struct { double x; double y; double z; } Xyz; Xyz xyzPlusOne(Xyz addOne) { Xyz xyz; xyz.x = addOne.x + 1; xyz.y = addOne.y + 1; xyz.z = addOne.z + 1; return xyz; }

    Read the article

  • c++ Initializing a struct with an array as a member

    - by Drew Shafer
    I've got the following reduced testcase: typedef struct TestStruct { int length; int values[]; }; TestStruct t = {3, {0, 1, 2}}; This works with Visual C++, but doesn't compile with g++ under linux. Can anyone help me make this specific kind of initializer portable? Additional details: the actual structure I'm working with has several other int values, and the array can range in length from a single entry to over 1800 entries. Any help much appreciated. Thanks!

    Read the article

  • How can I copy one map into another using std::copy?

    - by Frank
    I would like to copy the content of one std::map into another. Can I use std::copy for that? Obviously, the following code won't work: int main() { typedef std::map<int,double> Map; Map m1; m1[3] = 0.3; m1[5] = 0.5; Map m2; m2[1] = 0.1; std::copy(m1.begin(), m1.end(), m2.begin()); return 0; } Is there any way to make it work with std::copy? Thanks!

    Read the article

  • Syncronizing indices of function pointer table to table contents

    - by Thomas Matthews
    In the embedded system I'm working on, we are using a table of function pointers to support proprietary Dynamic Libraries. We have a header file that uses named constants (#define) for the function pointer indices. These values are used in calculating the location in the table of the function's address. Example: *(export_table.c)* // Assume each function in the table has an associated declaration typedef void (*Function_Ptr)(void); Function_Ptr Export_Function_Table[] = { 0, Print, Read, Write, Process, }; Here is the header file: *export_table.h* #define ID_PRINT_FUNCTION 1 #define ID_READ_FUNCTION 2 #define ID_WRITE_FUNCTION 3 #define ID_PROCESS_FUNCTION 4 I'm looking for a scheme to define the named constants in terms of their location in the array so that when the order of the functions changes, the constants will also change. (Also, I would like the compiler or preprocessor to calculate the indices to avoid human mistakes like typeo's.)

    Read the article

  • quick sort problem

    - by farka
    I use qsort from C libary and I have datatype Element_type **pElement and Element_type is struct typedef element_type {int ,char ....} example, and i call quicksor function with qsort(*pElement,iCountElement,(size_t)sizeof(Element_type),compare); and callback function static int compare(const void *p1, const void *p2) { Element_type *a1 = (Element_type *)p1; Element_type *a2 = (Element_type *)p2; return ( (a2)->iServiceId < (a1)->iServiceId ); } but I always get segmentation fault. Why?

    Read the article

  • Split text files Accross threads

    - by Kevin
    The problem: I have a few text files (10) with numbers in them on every line. I need to have them split across some threads I create using the pthread library. these threads that are created (worker threads) are to find the largest prime number that gets sent to them (and over all the largest prime from all of the text files). My current thoughts on solutions: I am thinking myself to have two arrays and all of the text files in one array and the other array will contain a binary file that I can read say 1000 lines and send the pointer to the index of that binary file in a struct that contains the id, file pointer, and file position and let it crank through that. a little bit of what I am talking about pthread_create(&threads[index],NULL,calc_sqrt,(void *)threadFields[index]);//Pass struct to each worker Struct: typedef struct threadFields{ int *id, *position; FILE *Fin; }tField; If anyone has any insight or a better solution it would be greatly appreciated Thanks

    Read the article

  • memset on array of structures in C++

    - by garry
    I have another memset question. It appears as if the code I am editing may have some issues (or it's not done the same way in different files) A::LRM las[9]; //A and LRM are both structures with BOOLS and INTS memset(&las, 0, sizeof(las)); typedef Sec SecArray[16]; SecArray rad_array; memset(rad_array, 0, sizeof(SecArray)); The second example appears to be correct because rad_array is the same as the first position in the array. Then the sizeof(SecArray)) would make sense. The first one doesn't seem correct to me. All structs are just BOOLs and INTS nothing dynamic in them. My understanding from my other post about memset was that it followed this format. memset("pointer to object", "what to set it to", "size of object") Can anyone tell me what exactly is going on here if I am incorrect with my theory.

    Read the article

  • Can C++ Constructors be templates?

    - by Gokul
    Hi, I have non-template class with a templatized constructor. This code compiles for me. But i remember that somewhere i have referred that constructors cannot be templates. Can someone explain whether this is a valid usage? typedef double Vector; //enum Method {A, B, C, D, E, F}; struct A {}; class Butcher { public: template <class Method> Butcher(Method); private: Vector a, b, c; }; template <> Butcher::Butcher(struct A) : a(2), b(4), c(2) { // a = 0.5, 1; // b = -1, 1, 3, 2; // c = 0, 1; } Thanks, Gokul.

    Read the article

  • how to convert char * to uchar16 in JNI C++

    - by Sagar Hatekar
    Hello, here's what I am trying to do: typedef uint16_t uchar16_t; uchar16_t buf[32]; // buf will contain timezone information like GMT-6, Eastern Daylight Time, etc char * str = "Test"; for (int i = 0; i <= strlen(str); i++) buf[i] = str[i]; I guess that's not correct since uchar16_t would contain 2 bytes and str contains 1 byte. What is it that I am supposed to do ?

    Read the article

  • C++ and preprocessor macro gotcha

    - by aaa
    hello. Appologies for yet another gotcha question. Can you figure out what is wrong with the statement below? gcc error states: "type name declared as function returning array". #define MACRO(a) (a)[1] class index { typedef int index_type[2]; index_type& operator[](int i); }; int k = 0; int i = MACRO(index()[k]); ps: is such questions are deemed too annoying, I am going to stop.

    Read the article

  • JNAerator Unnamed Union Missing in Structure

    - by Nick
    I'm trying to get JNAerator to generate some JNA backed Java code from a C shared library and everything is fine except that it failed to generate an unnamed union nested inside a structure. Example: typedef struct MY_STRUCTURE { union { My_Type1 var1; My_Type2 var2; }; }MY_STRUCTURE; If I change the header to make the union have a name it will work. But for obvious reasons I can't just change the header without breaking the shared library I'm trying to use. Any solutions other than changing the header file and shared library to named union?

    Read the article

  • crc24 from c to python

    - by biiiiiaw
    can someone please translate this code to python? i have tried and tried again, but have not managed it: #define CRC24_INIT 0xB704CEL #define CRC24_POLY 0x1864CFBL typedef long crc24; crc24 crc_octets(unsigned char *octets, size_t len) { crc24 crc = CRC24_INIT; int i; while (len--) { crc ^= (*octets++) << 16; for (i = 0; i < 8; i++) { crc <<= 1; if (crc & 0x1000000) crc ^= CRC24_POLY; } } return crc & 0xFFFFFFL; } i have the rotate left function (ROL24(value,bits_to_rotate_by)), which i know works since i got it from a source code of a reputable programmer, but i dont get the * and ++ on octet. i only sort of understand how ++ works in c++, and i dont know what * is at all

    Read the article

  • pointers in C with a #define

    - by milan
    The function: #define ASSOC(port) (*(volatile bit_field *) (&port)) The function call: #define SCLK ASSOC(PORTC).bit0 bit_field defined as a struct like this: typedef struct { unsigned char bit0 :1, bit1 :1, bit2 :1, bit3 :1, bit4 :1, bit5 :1, bit6 :1, bit7 :1; } bit_field; I don't know where &port is defined. Can someone please explain how the function is read and how it works please? I am not very good with pointers and this example in particular is very confusing with "*" in the front and at the end and the "&" with the port. Thank you

    Read the article

  • Using Boost statechart, how can I transition to a state unconditionally?

    - by nickb
    I have a state A that I would like to transition to its next state B unconditionally, once the constructor of A has completed. Is this possible? I tried posting an event from the constructor, which does not work, even though it compiles. Thanks. Edit: Here is what I've tried so far: struct A : sc::simple_state< A, Active > { public: typedef sc::custom_reaction< EventDoneA > reactions; A() { std::cout << "Inside of A()" << std::endl; post_event( EventDoneA() ); } sc::result react( const EventDoneA & ) { return transit< B >(); } }; This yields the following runtime assertion failure: Assertion failed: get_pointer( pContext_ ) != 0, file /includ e/boost/statechart/simple_state.hpp, line 459

    Read the article

  • Is 'bool' a basic datatype in C++ ?

    - by Naveen
    I got this doubt while writing some code. Is 'bool' a basic datatype defined in the C++ standard or is it some sort of extension provided by the compiler ? I got this doubt because Win32 has 'BOOL' which is nothing but a typedef of long. Also what happens if I do something like this: int i = true; Is it "always" guaranteed that variable i will have value 1 or is it again depends on the compiler I am using ? Further for some Win32 APIs which accept BOOL as the parameter what happens if I pass bool variable?

    Read the article

< Previous Page | 20 21 22 23 24 25 26 27 28 29 30 31  | Next Page >