在我用C項目,我得到這些錯誤:錯誤在Visual Studio 2008中C++
error C2371: 'tDatabase' : redefinition; different basic types
db.h(7) : see declaration of 'tDatabase'
error C2223: left of '->numberOfCity' must point to struct/union
error C2198: 'calloc' : too few arguments for call
我初學C.我該如何解決呢?謝謝。
文件:DB.h
typedef struct Database {
int numberOfCity;
tListOfCity* list;
double **distances;
} tDatabase;
tDatabase *LoadDatabase(char* file);
文件DB.c
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "DB.h"
typedef struct Database{
int numberOfCity;
tListOfCity* list;
double** distances;
} tDatabase; //this line error C2371
tDatabase* LoadDatabase(char* file) {
FILE* fileName;
tDatabase *input;
char data[100];
int i, j, number = 0;
input = (tDatabase*) calloc(1, sizeof (tDatabase));
input->list = NULL;
fileName = fopen("file.txt", "r");
fscanf(fileName, "%d", &number);
input->numberOfCity = number;
input->distances = (double**) calloc(data->numberOfCity, sizeof (double*)); //this line Error C2223 and C2198
for (i = 0; i < input->numberOfCity; i++) {
input->distances[i] = (double*) calloc(input->numberOfCity, sizeof (double));
}
for (i = 0; i <= input->numberOfCity; i++) {
fgets(data, sizeof (data), fileName);
input->list = AddOnEnd(input->list, CreateCity(data));
}
for (i = 0; i < input->numberOfCity; i++) {
for (j = 0; j < input->numberOfCity; j++) {
fscanf(fileName, "%f", &input->distances[i][j]);
}
fclose(fileName);
return input;
}
您的編譯器告訴您不要重新定義該結構。你爲什麼這樣做? – Mat
請告訴我們這些你不明白的錯誤信息。 SO的工作方式與您所設想的不同。您的問題標題完全不特定。您以後如何認爲這樣的問題標題可以被搜索以供其他人使用? –
當我有幾個錯誤時,我應該如何命名標題? – user3161483