Error *** has no member named *** - c

I don't know what i'm doing wrong ..
Do you have any ideas what i'm doing wrong? Structure was declared in header file sender.h - code below
After trying to compile this program I got this error:
Sender/Sender.c: In function 'SenderCreate':
Sender/Sender.c:50: error: 'Sender' has no member named 'sim_buf'
Sender/Sender.c:51: error: 'Sender' has no member named 'sim_buf_length'
Sender/Sender.c: In function 'SenderExecuteTask':
Sender/Sender.c:75: error: 'sim_buf_length' undeclared (first use in this function)
Sender/Sender.c:75: error: (Each undeclared identifier is reported only once
Sender/Sender.c:75: error: for each function it appears in.)
Sender/Sender.c:77: error: 'sim_buf' undeclared (first use in this function)
make: *** [Sender.o] Error 1
Code of program below:
#include <stdlib.h> // calloc, free
#include <stdio.h>
#include "Sender.h"
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include "../../rdo_module/readout_dev.h"
struct Sender_s {
ET4DataSource *data_source;
unsigned int num_of_images;
unsigned int num_of_el;
ConditionWait *condition_wait;
int sock;
struct sockaddr *PC;
char *sim_buf;
char *sim_buf_length;
};
void SenderSetNumOfEl(Sender *self, unsigned int num_of_el) {
self->num_of_el = num_of_el;
}
Sender *
SenderCreate(ET4DataSource *data_source,
unsigned int num_of_images,
unsigned int num_of_el,
ConditionWait *condition_wait,
int sock,
struct sockaddr *PC,
char *sim_buf, // here I get some problems
int *sim_buf_length) // and here
{
#ifdef DEBUG
printf("Sender.c SenderCreate line 25: Sender *self = calloc(1, sizeof(Sender));\n");
#endif
Sender *self = calloc(1, sizeof(Sender));
#ifdef DEBUG
printf("Sender.c SenderCreate line 25: success\n");
#endif
self->data_source = data_source;
self->num_of_images = num_of_images;
self->num_of_el = num_of_el;
self->condition_wait = condition_wait;
self->sock = sock;
self->PC = PC;
self->sim_buf = sim_buf;
self->sim_buf_length = sim_buf_length;
return self;
}
void
SenderDestroy(Sender *self)
{
free(self);
}
void *
SenderExecuteTask(void *self_)
{
Sender *self = self_;
ET4Buffer *buf = NULL;
int n = 0;
int c_len = sizeof(*(self->PC));
while(1) {
if(*sim_buf_length) {
n=sendto(self->sock, sim_buf, *sim_buf_length, 0, self->PC, c_len);
if(n < 0) {
perror("error in sendto()");
return NULL;
}
}
return NULL;
}
Code of sender.h below:
#ifndef __SENDER_H__
#define __SENDER_H__
#include <sys/socket.h>
#include <sys/select.h>
#include <arpa/inet.h>
#include "../Utils/ConditionWait.h"
#include "../DataSource/ET4DataSource.h"
typedef struct Sender_s Sender;
Sender *
SenderCreate(ET4DataSource *data_source,
unsigned int num_of_images,
unsigned int num_of_el,
ConditionWait *condition_wait,
int sock,
struct sockaddr *PC,
char *sim_buf,
int *sim_buf_length);
void
SenderDestroy(Sender*self);
void *
SenderExecuteTask(void *self_);
void SenderSetNumOfEl(Sender *self, unsigned int num_of_el);
#endif /*__SENDER_MAKER_H__*/

The error messages and the source in the question don't match!
When I take the source given, the compiler tells, what is wrong.
There is no member sim_buf_length, (note it does not complain about sim_buf_length_flag)
I don't get the error message "error: ‘Sender’ has no member named ‘sim_buf’", because the member is clearly present
Furthermore the types char* (Sender_s member) and int* (SenderCreate argument) don't match
The error messages for function SenderExecuteTask are clear, there are no variables declared sim_buf or sim_buf_length(_flag). Probably the function signature should have been
void *SenderExecuteTask(Sender *self);
and then in the definition self->sim_buf and self->sim_buf_length(_flag) used respectively.

Related

Error with struct declare ( error: array type has incomplete element type 'struct entry' )

I have this code and I run it and get his error :
error: array type has incomplete element type 'struct entry'
and I try it to solve it and explore same error but got fail
#ifndef GLOBAL_H
#define GLOBAL_H
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
//list of CONSTANTS
#define BSIZE 128
#define NONE -1
#define EOS '\0'
#define NUM 256
#define DIV 257
#define MOD 258
#define ID 259
#define DONE 260
//list of prototype function
void init();
int lexan();
int lookup(char s[]);
int insert(char s[], int tok);
void emit(int t,int tval);
void parse();
void expr();
void term();
void factor();
void match(int t);
void error(char* m);
//list of variables
FILE *input, *output, *err;
int tokenval;
int lineno;
int lookahead;
extern struct entry symtable[];
struct entry {
char *lexptr;
int token;
};
#endif
and I get error in this line :
extern struct entry symtable[];
this is code for ( global.h ) and other file not get an error
Move the struct entry definition to be placed before it is first used.
struct entry {
char *lexptr;
int token;
};
extern struct entry symtable[];

C: Why do I get the compile error "dereferencing pointer to incomplete type ‘struct xy’"? [duplicate]

This question already has answers here:
C programming: Dereferencing pointer to incomplete type error
(6 answers)
Closed 3 years ago.
I have the problem described in the headline above. These are my files and their code:
run.c:
[...] // I think it's not relevant for the problem
declarations.h:
#ifndef DECLARATIONS_H
#define DECLARATIONS_H
#include <stdint.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <errno.h>
[...]
struct
{
int position;
int currentNumberOfMessages;
int numberOfProcesses;
char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
} mySharedMemory_sct = {0, 0, 0, '0'};
struct mySharedMemory_sct *myShMem_ptr;
[...]
#endif //DECLARATIONS_H
lib.h:
#ifndef LIB_H
#define LIB_H
#include "declarations.h"
#include <stdint.h>
#include <stdio.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <errno.h>
[...]
int init (int *argc, char **argv[])
{
/**
* map the shared memory into the process
*/
if ((myShMem_ptr = mmap(NULL, sizeof(mySharedMemory_sct), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
MAP_FAILED)
{
printf("Error: %s\n", strerror(errno));
return EXIT_FAILURE;
}
/**
* increment the number of running processes called by the 'run'-process
*/
myShMem_ptr->numberOfProcesses += 1; <------- ERROR
[...]
return EXIT_SUCCESS;
}
[...]
#endif //LIB_H
For the line marked 'error' the compiler throws this error message:
"dereferencing pointer to incomplete type ‘struct mySharedMemory_sct’"
and highlights the '->' in "myShMem_ptr->numberOfProcesses += 1;" red as the problem.
I've read the other posts to this error message, but the problem causes were different (i think), so I haven't found a solution yet.
In advance: Thank you for your help!
You need to change
struct
{
int position;
int currentNumberOfMessages;
int numberOfProcesses;
char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
} mySharedMemory_sct = {0, 0, 0, '0'};
to
struct mySharedMemory_sct
{
int position;
int currentNumberOfMessages;
int numberOfProcesses;
char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
} mySharedMemory_sct = {0, 0, 0, '0'};
Let's have a look at a simpler case:
struct a {
int x;
} b;
So what do we have here? We have declared a struct and given it the name a, so this makes it possible to declare instances of that struct with struct a <name>. What about b? Well, that is an example of such an instance.
So what does this mean?
struct a {
int x;
} b = {0};
Well, it does certainly NOT mean that when you create an instance of struct a that the instance will have its x value initialized to 0. It only means that this is true for the very instance b.
You have not posted the complete code, but I suspect that this might do what you want:
struct mySharedMemory_sct {
int position;
int currentNumberOfMessages;
int numberOfProcesses;
char buf[MAX_PAYLOAD_LENGTH * MAX_SLOTS];
} mySharedMemory_sct = {0, 0, 0, '0'};
struct mySharedMemory_sct *myShMem_ptr = &mySharedMemory_sct;
An important thing to remember here is that mySharedMemory_sct and struct mySharedMemory_sct are two completely different things. mySharedMemory_sct is a variable with type struct mySharedMemory_sct. You can change their names independently of each other.

Implicit Declaration of Function sbrk( )

I am trying to create my own malloc function, and I did not finished yet. Here is related part of my code:
mymalloc.h :
#pragma once
#include <stdlib.h>
typedef struct METADATA{
struct METADATA *next;
struct METADATA *prev;
int free;
size_t size;
}METADATA;
METADATA *metadata;
void *mm_malloc(size_t size);
mymalloc.c
#include "mm_alloc.h"
#include "stdlib.h"
#include "stdio.h"
void *mm_malloc(size_t size) {
if(size == 0)
return NULL;
METADATA *tmp;
long address;
if(metadata == NULL){
sbrk(sizeof(tmp));
sbrk(size);
address = sbrk(0);
return (void *)address;
}
}
In the sbrk(sizeof(tmp)); part of mymalloc.c, I got "Implicit declaration of function sbrk()". What I wanted to do ise creating a place for metadata of new block and also a place for the required size. Where am I doing wrong?
Add the line
#include <unistd.h>
At the top of the file 'mymalloc.c' so that the function is declared

C - Pass struct by reference

I am trying to pass a structure by reference in C so that I can modify the values from within the function. This is the code I have so far, but it produces some warnings and one error.
main.c
#include <stdio.h>
#include "myfunctions.h"
#include "structures.h"
int main(int argc, char const *argv[] {
struct MyStruct data;
data.value = 6;
printf("Before change: %d\n", data.value);
changeData(data);
printf("After change: %d\n", data.value);
}
myfunctions.c
#include "structures.h"
void changeData(MyStruct data) {
data.value = 7;
}
myfunctions.h
#ifndef MyStruct
#define MyStruct
void changeData(MyStruct data);
#endif
structures.h
typedef struct {
int value;
} MyStruct;
Errors Produced
In file included from main.c:2:0:
myfunctions.h:4:1: warning: parameter names (without types) in function declaration
void changeData(MyStruct data);
^
In file included from main.c:3:0:
structures.h:5:1: warning: unnamed struct/union that defines no instances
} MyStruct;
^
main.c: In function ‘main’:
main.c:9:5: error: ‘data’ undeclared (first use in this function)
data.value = 6;
^
main.c:9:5: note: each undeclared identifier is reported only once for each function it appears in
That's all caused by
#define MyStruct
With this line, you've defined MyStruct as a macro that expands to nothing. I.e. you've effectively removed all occurrences of MyStruct in the following code, which is why the compiler is so confused about seeing things like
typedef struct {
int value;
} ;
or
void changeData( data);
To fix this, use
#ifndef MYFUNCTIONS_H_
#define MYFUNCTIONS_H_
instead. (This is the reason why we use ALL_UPPERCASE names for macros: To avoid accidental name clashes with normal identifiers.)
applying all my comments and elimination of the unnecessary 'typedef', and placing it all in one file ( Note: there is no problem with extracting the various files), results in the following code:
#ifndef STRUCTURES_H
#define STRUCTURES_H
struct MyStruct
{
int value;
};
#endif // STRUCTURES_H
#ifndef MYFUNCTIONS_H
#define MYFUNCTIONS_H
void changeData( struct MyStruct *data);
#endif // MYFUNCTIONS_H
#include <stdio.h>
//#include "myfunctions.h"
//#include "structures.h"
int main( void )
{
struct MyStruct data;
data.value = 6;
printf("Before change: %d\n", data.value);
changeData(&data);
printf("After change: %d\n", data.value);
} // end function: main
//#include "structures.h"
void changeData( struct MyStruct *data)
{
data->value = 7;
} // end function: changeData
which cleanly compiles and does do the desired operation

incomplete type 'struct' error in C

I have this issue and can't see where the error is so I'm hoping someone can help address it. The error I get from compiling the source is:
client.c:15:54: error: invalid application of ‘sizeof’ to incomplete type ‘struct client’
I have the struct definition inside a header file - client.h:
#ifndef PW_CLIENT
#define PW_CLIENT
#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
struct client {
int id;
struct bufferevent *bev;
struct client *prev;
struct client *next;
};
struct client *client_head = NULL;
struct client* client_connect(struct bufferevent *bev);
#endif
And here is the source of client.c:
#include <event2/listener.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
struct client* client_connect(struct bufferevent *bev) {
// Client info
struct client *c = (struct client*)malloc(sizeof(struct client));
if (c == NULL) {
// error allocating memory
} else {
if (client_head == NULL) {
// initialize list addresses
c->prev = c->next = NULL;
// set connection id
c->id = 0;
} else {
// set list addresses
client_head->next = c;
c->prev = client_head;
c->next = NULL;
client_head = c;
// set connection id
c->id = (c->prev->id + 1);
}
// initialize user vars
c->bev = bev;
}
return c;
}
Thanks!
You have forgotten to #include "client.h", so the definition of struct client is not known in client.c, hence struct client denotes an incomplete type there.
Sorry, but you need to include client.h, the compiler only compiles what he is told to...
I don't see
#include "client.h"
in your .c file

Resources