2013-03-09 24 views
1

我正在使用Dev C++編譯器。 所以,這裏有什麼問題,我在addRecord模塊中遇到錯誤。在我的程序中Record *和Databse *有什麼區別?

的編譯日誌:

Compiler: Default compiler 
Executing g++.exe... 
g++.exe "C:\SAI,MANI\test_add(09.03.2013).cpp" -o "C:\Users\Ravitheja\Desktop\C and C++\Projects\SAI,MANI\test_add(09.03.2013).exe" -O3 -g3 
-I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2" -I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib" -g3 
C:\SAI,MANI\test_add(09.03.2013).cpp: In function `void addRecord(Record*)': 
C:\SAI,MANI\test_add(09.03.2013).cpp:151: error: using typedef-name `Record' after `struct' 
C:\SAI,MANI\test_add(09.03.2013).cpp:151: error: cannot convert `Record*' to `Database*' in assignment 
C:\SAI,MANI\test_add(09.03.2013).cpp:153: error: expected primary-expression before '*' token 
C:\SAI,MANI\test_add(09.03.2013).cpp:153: error: expected primary-expression before ')' token 
C:\SAI,MANI\test_add(09.03.2013).cpp:153: error: expected `;' before "malloc" 
C:\SAI,MANI\test_add(09.03.2013).cpp:154: error: cannot convert `Record*' to `Database*' in assignment 

C:\SAI,MANI\test_add(09.03.2013).cpp: In function `void viewRecords(Record*)': 
C:\SAI,MANI\test_add(09.03.2013).cpp:180: error: cannot convert `Database*' to `Record*' in assignment 

C:\SAI,MANI\test_add(09.03.2013).cpp: In function `int getDate(date*)': 
C:SAI,MANI\test_add(09.03.2013).cpp:187: error: conversion from `date*' to non-scalar type `date' requested 

Execution terminated 

特別是在這條線,

x->next = (Record*)malloc(sizeof(struct Record));

我得到一個錯誤:無法轉換 '記錄*' 到 '數據庫*' 的任務,但什麼是記錄和數據庫之間的區別,因爲我已經typedef-ed他們。

我還沒有寫出所有的模塊,我只是想測試我的addRecord()和viewRecords(),但它不工作。如何糾正這些錯誤?

#include<iostream> 
#include<conio.h> 
#include<stdlib.h> 
#include<malloc.h> 
using namespace std; 

// global declarations and function prototypes.... 

const int TRUE = 1,FALSE = 0,SUCCESS = 1; 
const int SUBJECTS = 3; 
typedef char String[25]; 

// date structure , a better way to store dates. 

struct date 
{ 
     int day,month,year; 
}; 

// Database structure , the main double linked list which stores all the information. 
typedef struct DataBase 
{ 
     String Name,FatherName,Address,Hometown; 
     float Marks[SUBJECTS],Total,Percentage; 
     date DOB; 
     long int RegNo,PhoneNumber; 
     struct Database *previous;  // the addresses of the next and previous nodes in the dll. 
     struct Database *next; 
}Record; 


int main(); 

int menu();       // for displaying menu. 
void process(int,Record*);   // for processing the menu. 

void addRecord(Record*);    // for adding a record. 
void delRecord(Record*);    // for deleting a record. 
void modRecord(Record*);    // for modifying values of a record. 
void sortRecords(Record*);   // for sorting records. 
void filterRecords(Record*);   // for filtering records. 
void searchRecords(Record*);   // for searching a record. 
void viewRecords(Record*);   // for viewing records (all). 

int getDate(date*);     // for getting input for a date. 
void copyDate(date,date*);   // for copying two date variables. 
int checkDate(date);     // for checking wether a given date is correct. 
char *toString(int);     // for displaying month name given the month number. 
void copyRecord(Record*,Record*);  // for copying contents of one record into another. 

// main function... 

int main() 
{ 
    Record *r; 

    r = (Record*) malloc (sizeof(Record)); 
    r->next  = NULL; 
    r->previous = NULL; 

    while(1) 
    { 
        process(menu(),r); 
    } 
} 

// the menu function. 

int menu() 
{ 
    static int choice; 
    cout<<"\n\t\t\t Menu"; 
    cout<<"\n\t\t\t 1.Add Records "; 
    cout<<"\n\t\t\t 2.Delete Records"; 
    cout<<"\n\t\t\t 3.Modify Records"; 
    cout<<"\n\t\t\t 4.Sort Records"; 
    cout<<"\n\t\t\t 5.Filter Records"; 
    cout<<"\n\t\t\t 6.Search Records"; 
    cout<<"\n\t\t\t 7.View Records"; 
    cout<<"\n\t\t\t 8.Exit"; 
    cout<<"\n\t\t\t YOUR CHOICE : "; 
    cin>>choice; 
    if(choice>=1 && choice<=7) return choice; 
    else if(choice == 8) exit(0); 
    else 
    { 
     cout<<"\n Sorry, that's an invalid choice."; 
     cout<<"\n Please Try Again."; 
     menu(); 
    } 
} 

void process(int choice,Record *r) 
{ 
    switch(choice) 
    { 
        case 1: 
         addRecord(r); 
         break; 
        case 2: 
         delRecord(r); 
         break; 
        case 3: 
         modRecord(r); 
         break; 
        case 4: 
         sortRecords(r); 
         break; 
        case 5: 
         filterRecords(r); 
         break; 
        case 6: 
         searchRecords(r); 
         break; 
        case 7: 
         viewRecords(r); 
         break; 
    } 
} 

void addRecord(Record *x) 
{ 
    date *t; 
    t = (date*) malloc (sizeof(date)); 
    fflush(stdin); 
    Record *temp; temp = (Record*) malloc (sizeof(Record)); 
    cout<<"\n Enter the following details ..... "<<endl; 
    cout<<"\n Name   : "; 
    gets(temp->Name); 
    cout<<"\n Father's Name : "; 
    gets(temp->FatherName); 
    cout<<"\n Address   :"; 
    gets(temp->Address); 
    cout<<"\n Hometown  : "; 
    gets(temp->Hometown); 
    cout<<"\n Register Number : "; 
    cin>>temp->RegNo; 
    temp->Total = 0; 
    for(int i=0;i<SUBJECTS;i++) 
    { 
      cin>>temp->Marks[i]; 
      temp->Total += temp->Marks[i]; 
    } 
    temp->Percentage = temp->Total/SUBJECTS; 
    cout<<"\n Total Marks  : "<<temp->Total; 
    cout<<"\n\n Percentage  : "<<temp->Percentage; 

       if(getDate(t) == SUCCESS) // trick! 
       copyDate(temp->DOB,t); 

    x->next = (Record*)malloc(sizeof(struct Record)); 
    copyRecord(x,temp); 
    temp->previous = (Record*)malloc(struct Database); 
    temp->previous = x; 
    temp->next = NULL; 
    return; 
} 

void viewRecords(Record *x) 
{ 
    if(x->next == NULL && x->previous == NULL) 
    { 
       cout<<"\n There are no records to view."; 
       cout<<"\n Please Add some records and then try again!"; 
       return; 
    } 
    do 
    { 
        cout<<"\n Name   : "<<x->Name; 
        cout<<"\n Father's Name : "<<x->FatherName; 
        cout<<"\n Address   : "<<x->Address; 
        cout<<"\n Hometown  : "<<x->Hometown; 
        cout<<"\n Register Number : "<<x->RegNo; 
        for(int i=0;i<SUBJECTS;i++) 
        cout<<"\n Mark "<<i+1<<" : "<<x->Marks[i]; 
        cout<<"\n Total Marks  : "<<x->Total<<endl; 
        cout<<"\n Percentage  : "<<x->Percentage; 
        cout<<"\n Date Of Birth : "<<x->DOB.day<<"th"<<toString(x->DOB.month)<<" "<<x->DOB.year; 
        if(x->next == NULL) break; 
    }while((x=x->next)!=NULL); 
} 

int getDate(date *t) 
{ 
    cout<<"\nDate of birth (dd:mm:yyyy) : "; 
    scanf("%d:%d:%d",&t->day,&t->month,&t->year); 
    if(checkDate(t) == SUCCESS) 
    return SUCCESS; 
    else 
    { 
     cout<<"\n Sorry, that's not a valid Date."; 
     cout<<"\n Please Try Again,"<<endl; 
     getDate(t); 
    } 
} 
void copyDate(date d1,date *d2) 
{ 
    d1.day = d2->day; 
    d1.month = d2->month; 
    d1.year = d2->year; 
    return; 
} 

int checkDate(date *x) 
{ 
    int leap = (x->year%4==0)?TRUE:FALSE; 
    if((x->day<=0 || x->day > 31 ) || (x->month>12 || x->month<=0) || (x->year<1900 || x->year > 2008)) 
    return FALSE; 
    else if(leap == TRUE && x->month == 2 && (x->day>29)) 
    return FALSE; 
    else if(leap == FALSE && x->month == 2 && (x->day>28)) 
    return FALSE; 
    else if((x->month == 4 || x->month == 6 || x->month == 9 || x->month == 11) && x->month >= 31) 
    return FALSE; 
    else 
    return TRUE; 
} 

char *toString(int m) 
{ 
    char *t = (char*) malloc (sizeof(char)*4); 
    switch(m) 
    { 
       case 1 : t = "Jan"; break; 
       case 2 : t = "Feb"; break; 
       case 3 : t = "Mar"; break; 
       case 4 : t = "Apr"; break; 
       case 5 : t = "May"; break; 
       case 6 : t = "Jun"; break; 
       case 7 : t = "Jul"; break; 
       case 8 : t = "Aug"; break; 
       case 9 : t = "Sep"; break; 
      case 10 : t = "Oct"; break; 
      case 11 : t = "Nov"; break; 
      case 12 : t = "Dec"; break; 
    } 
    return t; 
} 
+0

這是C++不是C!在C中沒有命名空間。 – 2013-03-09 14:45:58

+0

我無法在您的投影中找到感覺.... – 2013-03-09 14:49:33

+0

@Sascha:有一種感覺,因爲程序是C++而不是C. – 2013-03-09 14:50:05

回答

2

我認爲問題的根源在於Recordstruct Database typedef名稱,以及第一個錯誤(使用struct關鍵字與Record)是導致後來的錯誤。

由於這是C++,自卸malloc完全和使用new操作:

x->next = new Record; 
+0

謝謝約翰,這是一個好主意,但我只是想熟悉實施malloc()。 – Ravitheja 2013-03-09 14:57:39

1

你並不需要使用struct關鍵字進行的typedef ..

所以,

x->next = (Record*)malloc(sizeof(struct Record)); 

應該

x->next = (Record*)malloc(sizeof(Record)); 

注:

你最好使用new代替malloc

+0

這不會回答Q,因爲:1.程序是C++而不是C,使用'malloc'是不正確的。結構中有一個「std :: string」成員。 2.如果完全是C,那麼這還不是一個好建議,理想情況是使用'x-> next = malloc(*(x-> next));' – 2013-03-09 14:47:07

+0

@AlokSave我認爲它不是std ::字符串,但我已經給了該typedef字符串[25];所以它實際上並不是一個字符串類,而是char [25]的別名。 – Ravitheja 2013-03-09 14:50:49

+0

@Ravitheja:你在C程序中如何使用'std :: cout'?你不知道你是在編寫一個C程序還是一個C++程序? – 2013-03-09 14:52:08

1

你應該讓你的心,你是否要寫入C或C++,因爲它們是不同的語言。

回答你的主要問題「也就是記錄和數據庫之間的差別」,是

typedef struct Database 
{ 
    // ... 
} Record; 

引入了幾個名字的結構。

在C中會有兩個:struct DatabaseRecord
在C++中,比如你的代碼,有三個:與C相同的兩個,再加上Database

在這兩種語言都不是有一個叫struct Record類型,這就是爲什麼你得到的第一個錯誤:「使用的typedef名‘記錄’‘結構’後」。
你應該總是先修正第一個錯誤。 (我不確定這是否會解決你的問題,因爲你的g ++ lokks有點過時 - 如果它真的是3.4.2版本,它已經差不多10年了,你應該考慮升級)。

相關問題