Turboc++ 中的链接器错误:未定义的符号

发布于 2025-01-07 04:18:20 字数 8161 浏览 3 评论 0原文

我正在 TurboC 上尝试以下程序。我收到其中一个函数原型的链接器错误。 错误是:模块 Student.cpp 中未定义的符号 checkbranch(char close*) 其中,student.cpp 是文件名。 代码编译没有错误,我无法在这里检测到错误。任何帮助将不胜感激。

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
#include<fstream.h>
#include<string.h>
#include<ctype.h>
void checkbranch(char string[]);
void checkusn(char string[]);
void checksem(char string[]);
void checkname(char string[]);
fstream file;
class iobuffer
{
    public: char maxbyte[120];
    iobuffer();
};
iobuffer::iobuffer()
{
for(int i=0; i<120; i++)
    maxbyte[i]='\0';
}
class student
{
public: char usn[20], name[25], sem[10], address[20], branch[10];
    student()
    {
        strcpy(name,"");
        strcpy(usn, "");
    strcpy(sem, "");
    strcpy(branch, "");
    strcpy(address, "");
}
void read();
void display();
void rsem();
void rname();
void rusn();
void rbranch();
void raddress();
};
void student::rusn()
{
cout<<"\n\tEnter the new usn: ";
gets(usn);
checkusn(usn);
}
void student::rname()
{
cout<<"\n\tEnter the name: ";
gets(name);
checkname(name);
}
void student::rsem()
{
cout<<"\n\tEnter the sem: ";
gets(sem);
checksem(sem);
}
void checkbranch(char string[]);
void student::rbranch()
{
cout<<"\n\tEnter the branch: ";
gets(branch);
checkbranch(branch);
}
void student::raddress()
{
cout<<"\n\tEnter the address: ";
gets(address);
}
void checkname(char arr[])
{
for(int j=0; arr[j]!='\0'; j++)
     if((!isalpha(arr[j]))&&(arr[j]!=' '))
     {
        cout<<"\n\tWrong input. Re-enter: ";
        gets(arr);
        j=-1;
     }
}
void student::read()
{
cout<<"\n\tUSN: ";
gets(usn);
checkusn(usn);
cout<<"\n\tName: ";
gets(name);
checkname(name);
cout<<"\n\tSem: ";
gets(sem);
checksem(sem);
cout<<"\n\tBranch: ";
gets(branch);
checkbranch(branch);
cout<<"\n\tAddress: ";
gets(address);
}
void checksem(char arr[])
{
int x=0;
if((strcmp(arr,"1")==0)||(strcmp(arr,"2")==0)||(strcmp(arr,"3")==0)||(strcmp(arr,"4")==0)||(strcmp(arr,"5")==0)||(strcmp(arr,"6")==0)||(strcmp(arr,"7")==0)||(strcmp(arr,"8")==0))
    x=1;
if(x==0)
{
    cout<<"\n\tInvalid sem. Re-enter: ";
    gets(arr);
    checksem(arr);
}
}
void checkusn(char string[])
{
    fstream ftemp;
    char temp[20], tempusn[20], usn[20];
    int i;
if((strlen(string)!=10)||(!isdigit(string[0]))||(!isdigit(string[3]))||(!isdigit(string[4]))||(!isdigit(string[7]))||(!isdigit(string[8]))||(!isdigit(string[9]))||(!isalpha(string[1]))||(!isalpha(string[2]))||(!isalpha(string[5]))||(!isalpha(string[6])))
{
    cout<<"\n\tInvalid USN. Re-enter: ";
    gets(string);
    checkusn(string);
}
file.open("studentdb.txt",ios::in|ios::binary);
while(file.read(temp,120))
{
    i=0;
    while((tempusn[i]=temp[i])!='|')
        i++;
    tempusn[i]='\0';
    if(strcmp(tempusn,string)==0)
    {
        cout<<"\n\tUSN already exists. Re-enter: ";
        gets(string);
        checkusn(string);
    }
}
file.close();
}
void student::display()
{
cout<<setiosflags(ios::left);
cout<<setw(15)<<usn<<setw(20)<<name<<setw(10)<<sem<<setw(15)<<branch<<setw(25)<<address;
}
class fixedlength:public iobuffer
{
public: char buffer[120];
void pack(student s);
void unpack(student s);
};
void fixedlength::pack(student s)
{
strcpy(maxbyte, "");
strcpy(maxbyte, s.usn);
strcat(maxbyte,"|");
strcat(maxbyte, s.name);
strcat(maxbyte, "|");
strcat(maxbyte, s.sem);
strcat(maxbyte, "|");
strcat(maxbyte, s.branch);
strcat(maxbyte, "|");
strcat(maxbyte, s.address);
strcat(maxbyte, "#");
}
void fixedlength::unpack(student s)
{
strcpy(buffer, maxbyte);
for(int i=0, j=1, k=0; j<=5; i++, j++)
{
switch(j)
{
    case 1: while(buffer[i]!='|')
            s.usn[k++]=buffer[i++];
        s.usn[k]='\0';
        k=0;
        break;
    case 2: while(buffer[i]!='|')
            s.name[k++]=buffer[i++];
        s.name[k]='\0';
        k=0;
        break;
    case 3: while(buffer[i]!='|')
            s.sem[k++]=buffer[i++];
        s.sem[k]='\0';
        k=0;
        break;
    case 4: while(buffer[i]!='|')
            s.branch[k++]=buffer[i++];
        s.branch[k]='\0';
        k=0;
        break;
    case 5: while(buffer[i]!='\0')
            s.address[k++]=buffer[i++];
        s.address[k]='\0';
        k=0;
    }
}
}
class delim:public fixedlength
{
public: void writerecord(student);
    void readrecord(student);
    void deleterecord(student, char*);
    void searchrecord(student, char*);
    void modifyrecord(student, char*);
};
void delim::writerecord(student s)
{
pack(s);
file.open("studentdb.txt", ios::app|ios::binary);
file.write((char*)&maxbyte, sizeof(maxbyte));
file.close();
}
void delim::readrecord(student s)
{
file.open("studentdb.txt", ios::in|ios::binary);
cout<<"\n\n File Contents\n\n";
while(file.read((char*)&maxbyte, sizeof(maxbyte)))
{
unpack(s);
cout<<endl;
s.display();
}
getch();
file.close();
}
void delim::searchrecord(student s, char *key)
{
int flag=0;
file.open("studentdb.txt",ios::in|ios::binary);
while((file.read((char*)&maxbyte,sizeof(maxbyte)))&&flag==0)
{
unpack(s);
if(!strcmp(s.usn,key))
{
    flag=1;
    s.display();
}
}
if(flag==0)
{
cout<<"\n\tSpecified record doesn't exist";
getch();
file.close();
}
    }
void delim::deleterecord(student s, char *a)
{
int flag=0;
char x;
fstream ftemp;
file.open("studentdb.txt",ios::in|ios::binary);
ftemp.open("temp.txt", ios::out|ios::trunc|ios::binary);
cout<<"\n\tAre you sure you want to delete (y/n)?";
cin>>x;
if(x=='y'||x=='Y')
{
while(file.read((char*)&maxbyte, sizeof(maxbyte)))
{
    unpack(s);
    if(strcmp(s.usn,a)==0)
        flag=1;
    else
        ftemp.write((char*)&maxbyte,sizeof(maxbyte));
}
}
if(flag==1)
{
file.close();
ftemp.close();
remove("studentdb.txt");
rename("temp.txt","studentdb.txt");
}
if(flag==0)
{
cout<<"\n\tThe specified record is not found\n";
file.close();
ftemp.close();
}
}
void delim::modifyrecord(student s, char *a)
{
int flag=0, choice;
file.open("studendb.txt",ios::in|ios::out|ios::binary);
while(file.read((char*)&maxbyte,sizeof(maxbyte)))
{
unpack(s);
if(strcmp(s.usn,a)==0)
{
    char x;
    flag=1;
    cout<<"\n\t Current contents: \n\n\n";
    s.display();
    do
    {
    cout<<"What do you want to change: ";
    cout<<"\n\t1.Name\n\t2. Sem\n\t3. Branch\n\t4. Address\n\t5. Confirm changes\n\t6. Return to main\n";
    cout<<"Enter choic: ";
    cin>>choice;
    switch(choice)
    {
        case 1: s.rname(); break;
        case 2: s.rsem(); break;
        case 3: s.rbranch(); break;
        case 4: s.raddress(); break;
        case 5: break;
        case 6: return;
        default: cout<<"Invalid option\n";
    }
    }while(choice==1||choice==2||choice==3||choice==4);
cout<<"\n\tDo you want to modify (y\n)?";
cin>>x;
if(x=='y'||x=='Y')
{
    pack(s);
    file.seekg(-120,ios::cur);
    file.write((char*)&maxbyte,sizeof(maxbyte));
}
else
        break;
}
}
if(flag==0)
{
cout<<"\n\t Specified record doesn't exist\n";
file.close();
}
}
void main()
{    
clrscr();
iobuffer i;
student s;
fixedlength f;
delim d;
int option;
char no[20], key[20];
for(;;)
    {
cout<<"\n\nMenu:- ";
cout<<"\n\t1. Insert\n\t2. Display\n\t3. Delete\n\t4. Modify\n\t5. Search\n\t6. End\n\t";
cin>>option;
switch(option)
{
    case 1: s.read();
        d.writerecord(s);
        break;
    case 2: d.readrecord(s);
        break;
    case 3: cout<<"Enter the usn to delete\n";
        gets(no);
        d.deleterecord(s, no); break;
    case 5: cout<<"\nEnter usn to be searched\n";
        gets(key);
        d.searchrecord(s,key);
        break;
    case 4: cout<<"\n\tEnter the usn to be modified\n";
        gets(no);
        d.modifyrecord(s,no);
        break;
    case 6: exit(0); break;
}
}
}

我不确定错误是什么,因此我尝试:

  1. 重新排序函数原型语句(即使如此,错误也会 仅用于 checkbranch)

  2. 在其他地方编写 checkbranch 的函数原型(再次会发生错误)

  3. 删除原型本身(然后得到编译错误缺乏 原型)

I am trying the following program on TurboC. I am getting a linker error for one of the function prototypes.
The error is: undefined symbol checkbranch(char near*) in module student.cpp where student.cpp is the name of the file.
The code compiles with no errors and I am not able to detect the error here. Any help would be appreciated.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<iomanip.h>
#include<fstream.h>
#include<string.h>
#include<ctype.h>
void checkbranch(char string[]);
void checkusn(char string[]);
void checksem(char string[]);
void checkname(char string[]);
fstream file;
class iobuffer
{
    public: char maxbyte[120];
    iobuffer();
};
iobuffer::iobuffer()
{
for(int i=0; i<120; i++)
    maxbyte[i]='\0';
}
class student
{
public: char usn[20], name[25], sem[10], address[20], branch[10];
    student()
    {
        strcpy(name,"");
        strcpy(usn, "");
    strcpy(sem, "");
    strcpy(branch, "");
    strcpy(address, "");
}
void read();
void display();
void rsem();
void rname();
void rusn();
void rbranch();
void raddress();
};
void student::rusn()
{
cout<<"\n\tEnter the new usn: ";
gets(usn);
checkusn(usn);
}
void student::rname()
{
cout<<"\n\tEnter the name: ";
gets(name);
checkname(name);
}
void student::rsem()
{
cout<<"\n\tEnter the sem: ";
gets(sem);
checksem(sem);
}
void checkbranch(char string[]);
void student::rbranch()
{
cout<<"\n\tEnter the branch: ";
gets(branch);
checkbranch(branch);
}
void student::raddress()
{
cout<<"\n\tEnter the address: ";
gets(address);
}
void checkname(char arr[])
{
for(int j=0; arr[j]!='\0'; j++)
     if((!isalpha(arr[j]))&&(arr[j]!=' '))
     {
        cout<<"\n\tWrong input. Re-enter: ";
        gets(arr);
        j=-1;
     }
}
void student::read()
{
cout<<"\n\tUSN: ";
gets(usn);
checkusn(usn);
cout<<"\n\tName: ";
gets(name);
checkname(name);
cout<<"\n\tSem: ";
gets(sem);
checksem(sem);
cout<<"\n\tBranch: ";
gets(branch);
checkbranch(branch);
cout<<"\n\tAddress: ";
gets(address);
}
void checksem(char arr[])
{
int x=0;
if((strcmp(arr,"1")==0)||(strcmp(arr,"2")==0)||(strcmp(arr,"3")==0)||(strcmp(arr,"4")==0)||(strcmp(arr,"5")==0)||(strcmp(arr,"6")==0)||(strcmp(arr,"7")==0)||(strcmp(arr,"8")==0))
    x=1;
if(x==0)
{
    cout<<"\n\tInvalid sem. Re-enter: ";
    gets(arr);
    checksem(arr);
}
}
void checkusn(char string[])
{
    fstream ftemp;
    char temp[20], tempusn[20], usn[20];
    int i;
if((strlen(string)!=10)||(!isdigit(string[0]))||(!isdigit(string[3]))||(!isdigit(string[4]))||(!isdigit(string[7]))||(!isdigit(string[8]))||(!isdigit(string[9]))||(!isalpha(string[1]))||(!isalpha(string[2]))||(!isalpha(string[5]))||(!isalpha(string[6])))
{
    cout<<"\n\tInvalid USN. Re-enter: ";
    gets(string);
    checkusn(string);
}
file.open("studentdb.txt",ios::in|ios::binary);
while(file.read(temp,120))
{
    i=0;
    while((tempusn[i]=temp[i])!='|')
        i++;
    tempusn[i]='\0';
    if(strcmp(tempusn,string)==0)
    {
        cout<<"\n\tUSN already exists. Re-enter: ";
        gets(string);
        checkusn(string);
    }
}
file.close();
}
void student::display()
{
cout<<setiosflags(ios::left);
cout<<setw(15)<<usn<<setw(20)<<name<<setw(10)<<sem<<setw(15)<<branch<<setw(25)<<address;
}
class fixedlength:public iobuffer
{
public: char buffer[120];
void pack(student s);
void unpack(student s);
};
void fixedlength::pack(student s)
{
strcpy(maxbyte, "");
strcpy(maxbyte, s.usn);
strcat(maxbyte,"|");
strcat(maxbyte, s.name);
strcat(maxbyte, "|");
strcat(maxbyte, s.sem);
strcat(maxbyte, "|");
strcat(maxbyte, s.branch);
strcat(maxbyte, "|");
strcat(maxbyte, s.address);
strcat(maxbyte, "#");
}
void fixedlength::unpack(student s)
{
strcpy(buffer, maxbyte);
for(int i=0, j=1, k=0; j<=5; i++, j++)
{
switch(j)
{
    case 1: while(buffer[i]!='|')
            s.usn[k++]=buffer[i++];
        s.usn[k]='\0';
        k=0;
        break;
    case 2: while(buffer[i]!='|')
            s.name[k++]=buffer[i++];
        s.name[k]='\0';
        k=0;
        break;
    case 3: while(buffer[i]!='|')
            s.sem[k++]=buffer[i++];
        s.sem[k]='\0';
        k=0;
        break;
    case 4: while(buffer[i]!='|')
            s.branch[k++]=buffer[i++];
        s.branch[k]='\0';
        k=0;
        break;
    case 5: while(buffer[i]!='\0')
            s.address[k++]=buffer[i++];
        s.address[k]='\0';
        k=0;
    }
}
}
class delim:public fixedlength
{
public: void writerecord(student);
    void readrecord(student);
    void deleterecord(student, char*);
    void searchrecord(student, char*);
    void modifyrecord(student, char*);
};
void delim::writerecord(student s)
{
pack(s);
file.open("studentdb.txt", ios::app|ios::binary);
file.write((char*)&maxbyte, sizeof(maxbyte));
file.close();
}
void delim::readrecord(student s)
{
file.open("studentdb.txt", ios::in|ios::binary);
cout<<"\n\n File Contents\n\n";
while(file.read((char*)&maxbyte, sizeof(maxbyte)))
{
unpack(s);
cout<<endl;
s.display();
}
getch();
file.close();
}
void delim::searchrecord(student s, char *key)
{
int flag=0;
file.open("studentdb.txt",ios::in|ios::binary);
while((file.read((char*)&maxbyte,sizeof(maxbyte)))&&flag==0)
{
unpack(s);
if(!strcmp(s.usn,key))
{
    flag=1;
    s.display();
}
}
if(flag==0)
{
cout<<"\n\tSpecified record doesn't exist";
getch();
file.close();
}
    }
void delim::deleterecord(student s, char *a)
{
int flag=0;
char x;
fstream ftemp;
file.open("studentdb.txt",ios::in|ios::binary);
ftemp.open("temp.txt", ios::out|ios::trunc|ios::binary);
cout<<"\n\tAre you sure you want to delete (y/n)?";
cin>>x;
if(x=='y'||x=='Y')
{
while(file.read((char*)&maxbyte, sizeof(maxbyte)))
{
    unpack(s);
    if(strcmp(s.usn,a)==0)
        flag=1;
    else
        ftemp.write((char*)&maxbyte,sizeof(maxbyte));
}
}
if(flag==1)
{
file.close();
ftemp.close();
remove("studentdb.txt");
rename("temp.txt","studentdb.txt");
}
if(flag==0)
{
cout<<"\n\tThe specified record is not found\n";
file.close();
ftemp.close();
}
}
void delim::modifyrecord(student s, char *a)
{
int flag=0, choice;
file.open("studendb.txt",ios::in|ios::out|ios::binary);
while(file.read((char*)&maxbyte,sizeof(maxbyte)))
{
unpack(s);
if(strcmp(s.usn,a)==0)
{
    char x;
    flag=1;
    cout<<"\n\t Current contents: \n\n\n";
    s.display();
    do
    {
    cout<<"What do you want to change: ";
    cout<<"\n\t1.Name\n\t2. Sem\n\t3. Branch\n\t4. Address\n\t5. Confirm changes\n\t6. Return to main\n";
    cout<<"Enter choic: ";
    cin>>choice;
    switch(choice)
    {
        case 1: s.rname(); break;
        case 2: s.rsem(); break;
        case 3: s.rbranch(); break;
        case 4: s.raddress(); break;
        case 5: break;
        case 6: return;
        default: cout<<"Invalid option\n";
    }
    }while(choice==1||choice==2||choice==3||choice==4);
cout<<"\n\tDo you want to modify (y\n)?";
cin>>x;
if(x=='y'||x=='Y')
{
    pack(s);
    file.seekg(-120,ios::cur);
    file.write((char*)&maxbyte,sizeof(maxbyte));
}
else
        break;
}
}
if(flag==0)
{
cout<<"\n\t Specified record doesn't exist\n";
file.close();
}
}
void main()
{    
clrscr();
iobuffer i;
student s;
fixedlength f;
delim d;
int option;
char no[20], key[20];
for(;;)
    {
cout<<"\n\nMenu:- ";
cout<<"\n\t1. Insert\n\t2. Display\n\t3. Delete\n\t4. Modify\n\t5. Search\n\t6. End\n\t";
cin>>option;
switch(option)
{
    case 1: s.read();
        d.writerecord(s);
        break;
    case 2: d.readrecord(s);
        break;
    case 3: cout<<"Enter the usn to delete\n";
        gets(no);
        d.deleterecord(s, no); break;
    case 5: cout<<"\nEnter usn to be searched\n";
        gets(key);
        d.searchrecord(s,key);
        break;
    case 4: cout<<"\n\tEnter the usn to be modified\n";
        gets(no);
        d.modifyrecord(s,no);
        break;
    case 6: exit(0); break;
}
}
}

I wasn't sure what the error would be therefore I tried:

  1. Reordering the function prototypes statements (even then error would
    only be for checkbranch)

  2. Writing the function prototype for checkbranch elsewhere (again error would occur)

  3. Removing the prototype itself (and then get the compilation error for lack of
    prototype)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半城柳色半声笛 2025-01-14 04:18:20

您必须实现 checkbranch 函数,编译器无法为您执行此操作。

You have to implement the checkbranch function, compiler can not do that for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文