Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2013
    Messaggi
    18

    [C] Problemi di compilazione di un progetto

    Salve a tutti, ogni volta che provo a compilare un progetto il compilatore mi da una serie di errori, ma non riesco a capire perchè (a me non sembra ci siano errori di sintassi). Vi posto per esempio il programma che sto facendo e vi sarei grato se riusciste a farmi capire cosa/dove sbaglio. Il programma non è terminato ma in teoria dovrebbe comunque potersi compilare perchè non dipende dalle parti mancanti. Grazie
    codice:
    |*TREE.H*|
    #ifndef _TREE_INCLUDED
    #define _TREE_INCLUDED
    
    #define N 50
    
    typedef tree tree_t;
    void scanFile (FILE*);
    void descendents (char*, char*);
    int brothers (char*, char*);
    #endif
    codice:
    |*TREE.C*|
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "Tree.h"
    
    typedef struct tree {
    	char name [N];
    	char surname [N];
    	char gender;
    	int age;
    	int parents [2];
    };
    
    static tree_t **ar;
    static int n;
    static int desc=0;
    
    int hash (char *, char*);
    
    void scanFile (FILE *fp) {
    	int i, index, ageTmp;
    	char nameTmp [N], surnameTmp [N], genderTmp, fatherN [N], fatherS [N], motherN [N], motherS [N];
    
    	fscanf (fp, "%d", &n);
    	ar= malloc (N*sizeof (tree_t*));
    	if (ar== NULL) {
    		printf ("allocation error \n");
    		exit (-2);
    	}
    	for (i=0; i<n; i++) {
    		ar [i]= malloc (sizeof (tree));
    		if (ar [i]== NULL) {
    			printf ("allocation error \n");
    		    exit (-2);
    		}
    	}
    	for (i=0; i<n; i++) {
    		fscanf (fp, "%s %s %c %d", nameTmp, surnameTmp, &genderTmp, &ageTmp);
    		index=hash (nameTmp, surnameTmp);
    		strcpy (ar [index]->name, nameTmp);
    		strcpy (ar [index]->surname, surnameTmp);
    		ar [index]->gender= genderTmp;
    		ar [index]->age= ageTmp;
    	}
    	while (fscanf (fp, "%s %s %s %s %s %s", nameTmp, surnameTmp, fatherN, fatherS, motherN, motherS)!= NULL) {
    		index= hash (nameTmp, surnameTmp);
    		(*ar) [index]->parents [0]= hash (fatherN, fatherS);
    		(*ar) [index]->parents [1]= hash (motherN, motherS);
    	}
    }
    
    void descendents (char* name, char* surname) {
    	int index=0, i;
    	index= hash (name, surname);
    	for (i=0; i<n; i++) {
    		if ((index== ar [i]->parents [0]) || (index== ar [i]->parents [1])) {
    			desc++;
    			descendents (ar [index]->name, ar [index]->surname);
    		} else return;
    	}
    }
    
    int brothers (char* name, char* surname) {
    	int index, father, mother, brotherN=0, i;
    
    	index= hash (name, surname);
    	father= ar [index]->parents [0];
    	mother= ar [index]->parents [1];
    	for (i=0; i<n; i++) {
    		if ((ar [i]->parents [0]== father) || (ar [i]->parents [1]== mother)) {
    			brotherN++;
    		}
    	}
    }
    
    int hash (char* name, char* surname) {
    	return 1;
    }
    codice:
    |*MAIN.C*|
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "Tree.h"
    
    void filename (char**);
    char *personName (char**);
    char *personSurname (char**);
    
    int main () {
    	char *name, choice, *perName, *perSurname;
    	FILE *fp=NULL;
    
    	filename (&name);
    	fp= fopen (name, "r");
    	if (fp== NULL) {
    		printf ("error opening the file\n");
    		return -1;
    	}
    	scanFile (fp);
    	while (choice!='e') {
    	    printf ("Select an option:\n");
    	    printf ("count descendents (a)?\n");
    	    printf ("count longest  (b)?\n");
    		printf ("count the longest descent (c)?\n");
    		printf ("count the number of brothers and sisters (c)\n?");
    	    printf ("exit (d)?\n");
    		scanf ("%c", choice);
    		switch (choice) {
    		case 'a': 
    			descendents (personName (&perName), personSurname (&perSurname));
    			free (perName);
    			free (perSurname);
    			break;
    		case 'b':
    		case 'c':
    			brothers (personName (&perName), personSurname (&perSurname));
    			free (perName);
    			free (perSurname);
    			break;
    		case 'd': 
    			break;
    		default:
    			printf ("error: unknown option\n");
    			break;
    		}
    
    	}
    	return 0;
    }
    
    void filename (char **name) {
    	char nameTmp [N];
    	printf ("insert file name: ");
    	scanf ("%s", nameTmp);
    	(*name)= malloc (sizeof (nameTmp));
    	if (*name== NULL) {
    		printf ("allocation error\n");
    		exit (-2);
    	}
    	strcpy (*name, nameTmp);
    	return;
    }
    
    char *personName (char **name) {
    	*name= malloc (N*sizeof (char));
    	if (*name== NULL) {
    		printf ("allocation error \n");
    		exit (-2);
    	}
    	printf ("insert name: ");
    	scanf ("%s", *name);
    	return *name;
    }
    
    char *personSurname (char **surname) {
    	*surname= malloc (N*sizeof (char));
    	if (*surname== NULL) {
    		printf ("allocation error \n");
    		exit (-2);
    	}
    	printf ("insert surname: ");
    	scanf ("%s", *surname);
    	return *surname;
    }

  2. #2
    E gli errori di compilazione che ti dà?
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Utente di HTML.it
    Registrato dal
    Feb 2013
    Messaggi
    18
    Oh scusa pensavo poteste compilarlo voi xD..
    Posto quello che mi da codeblocks con compilatore mingw

    |6|error: expected '=', ',', ';', 'asm' or '__attribute__' before 'tree_t'|
    |14|error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token|
    |25|error: 'ar' undeclared (first use in this function)
    |25|error: 'tree_t' undeclared (first use in this function)|
    |25|error: expected expression before ')' token|
    |56|error: 'ar' undeclared (first use in this function)|
    |67|error: 'ar' undeclared (first use in this function)|

    Quello che ad esempio non capisco è come fa "ar" a non essere dichiarato se è una variabile globale e "tree_t" è un tipo fatto con typedef e quindi perchè undeclared...

  4. #4
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Originariamente inviato da Krahos
    Oh scusa pensavo poteste compilarlo voi xD..
    In un forum, è buona norma che tu dica cosa non va e non lasci agli altri l'onere di fare tutte le prove ...

    Quello che ad esempio non capisco
    Una delle prime righe che il compilatore incontra è

    typedef tree tree_t;

    Secondo te cosa comprende da questa prima riga?
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  5. #5
    Utente di HTML.it
    Registrato dal
    Feb 2013
    Messaggi
    18
    Beh l'idea sarebbe quella di un ADT di prima categoria.. In altri programmi mi funziona mentre in questo non va. Comunque per rispondere alla tua domanda quello che secondo me comprende da quella riga è definire il tipo tree_t come tree.
    Dovevo forse fare?
    codice:
    typedef struct tree tree_t
    e dichiarare la struct come
    codice:
    struct tree {
    dati vari
    }
    Eppure dovrebbe comunque funzionare in quel modo

  6. #6
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    Originariamente inviato da Krahos
    In altri programmi mi funziona
    Non l'hai scritto in questo modo ...

    All'inizio del programma tree non è qualcosa di esistente e quindi quella linea diventa sicuramente sbagliata.

    Fra l'altro la typedef è ripetuta nel file tree.c ...

    La dichiarazione della struttura va spostata nell'include e l'unica typedef va scritta con

    codice:
    typedef struct tree {
    	char name [N];
    	char surname [N];
    	char gender;
    	int age;
    	int parents [2];
    } tree_t;
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  7. #7
    Utente di HTML.it
    Registrato dal
    Feb 2013
    Messaggi
    18
    Se però dichiaro il tipo struct all'interno dell'header diventa un quasi ADT e quindi non c'è information hiding. Comunque ho risolto facendo come ho detto sopra e scrivendo
    codice:
    typedef struct tree tree_t
    nel .h
    e dichiarando la struct come
    codice:
    struct tree {
    dati
    };
    nel .c
    E in più avevo messo qualche puntatore di troppo dove non dovevo. Adesso compila senza errori. Mi dispiace averti fatto perdere tempo per una stupidaggine e averne perso a mia volta.. Ti ringrazio comunque tantissimo per avermi dato retta .

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.