我在我的C ++程序中遇到链接错误,我无法编译我的代码

时间:2018-05-26 22:46:00

标签: c++ visual-studio visual-c++

Error   1   error LNK2005: "class bus bs" (?bs@@3Vbus@@A) already defined in bus.obj    c:\Users\tahir\documents\visual studio 2013\Projects\Project17\Project17\main.obj   Project17
Error   2   error LNK2005: "class ticket tick" (?tick@@3Vticket@@A) already defined in bus.obj  c:\Users\tahir\documents\visual studio 2013\Projects\Project17\Project17\main.obj   Project17
Error   3   error LNK2005: "class ticket tick" (?tick@@3Vticket@@A) already defined in bus.obj  c:\Users\tahir\documents\visual studio 2013\Projects\Project17\Project17\ticket.obj Project17
Error   4   error LNK2005: "class bus bs" (?bs@@3Vbus@@A) already defined in bus.obj    c:\Users\tahir\documents\visual studio 2013\Projects\Project17\Project17\ticket.obj Project17
Error   5   error LNK1169: one or more multiply defined symbols found   c:\users\tahir\documents\visual studio 2013\Projects\Project17\Debug\Project17.exe  1   1   Project17

bus.h

#pragma once

class bus
{
    int busno;
    int noofkidsseats;
    int noofwomenseats;
    int noofmenseats;
    int noofspecialseats;
    int noofvvip;
    char *busname;
    char *startpoint;
    char *destination;
public:
    bus();
    void input();
    int rbusno();
    int rnoofkidsseats();
    int rnoofwomenseats();
    int rnoofmenseats();
    int rnoofspecialseats();
    int rnoofvvip();
    void display();
}bs;

bus.cpp

#include <iostream>
#include <fstream>
#include "bus.h"
#include "ticket.h"
#include "windows.h"
using namespace std;

int length(char arr[])
{
    int i = 0;
    for (; arr[i] != '\0'; i++);
    return i;
}
void gotoxy(int x, int y)
{
    static HANDLE h = NULL;
    if (!h)
        h = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD c = { x, y };
    SetConsoleCursorPosition(h, c);
}
bus::bus()
{
    busno = 0;
    noofkidsseats = 0;
    noofwomenseats = 0;
    noofmenseats = 0;
    noofspecialseats = 0;
    noofvvip = 0;
    busname = '\0';
    startpoint = '\0';
    destination = '\0';
}
int bus::rbusno()
{
    return busno;
}
int bus::rnoofkidsseats()
{
    return noofkidsseats;
}
int bus::rnoofwomenseats()
{
    return noofwomenseats;
}
int bus::rnoofmenseats()
{
    return noofmenseats;
}
int bus::rnoofspecialseats()
{
    return noofspecialseats;
}
int bus::rnoofvvip()
{
    return noofvvip;
}
void bus::input()
{
    cout << "ENTER THE BUS NUMBER: ";
    cin >> busno;
    cout << "ENTER THE NUMBER OF SEATS FOR KIDS: ";
    cin >> noofkidsseats;
    cout << "ENTER THE NUMBER OF SEATS FOR WOMEN: ";
    cin >> noofwomenseats;
    cout << "ENTER THE NUMBER OF SEATS FOR MEN: ";
    cin >> noofmenseats;
    cout << "ENTER THE NUMBER OF SEATS SPECIAL PERSONS: ";
    cin >> noofspecialseats;
    cout << "ENTER THE NUMBER OF SEATS FOR VVIP PASSENGERS: ";
    cin >> noofvvip;
    cout << "ENTER THE BUS NAME: ";
    char name[20];
    cin >> name;
    int l = length(name);
    busname = new char[l];
    int i;
    for (i = 0; i < l; i++)
        busname[i] = name[i];
    busname[i] = '\0';
    cout << "ENTER THE STARTING POINT: ";
    char start[20];
    cin >> start;
    int L = length(start);
    startpoint = new char[L];
    int j;
    for (j = 0; j < L; j++)
        startpoint[j] = start[j];
    startpoint[j] = '\0';
    cout << "ENTER THE DESTINATION: ";
    char end[20];
    cin >> end;
    int s = length(end);
    destination = new char[s];
    int k;
    for (k = 0; k < s; k++)
        destination[k] = end[k];
    destination[k] = '\0';
}
void bus::display()
{

    cout << "           ***************************************" << endl;
    cout << "           *                                     *" << endl;
    cout << "           *            BUS DEATAILS             *" << endl;
    cout << "           *                                     *" << endl;
    cout << "           ***************************************" << endl;
    cout << "                     THE BUS NUMBER: ";
    cout << busno << endl;
    cout << "                     THE BUS NAME: ";
    cout << busname << endl;
    cout << "                     STARTING POINT: ";
    cout << startpoint << endl;
    cout << "                     DESTINATION: ";
    cout << destination << endl;
    cout << "                     THE NUMBER OF SEATS FOR KIDS: ";
    cout << noofkidsseats << endl;
    cout << "                     THE NUMBER OF SEATS FOR WOMEN: ";
    cout << noofwomenseats << endl;
    cout << "                     THE NUMBER OF SEATS FOR MEN: ";
    cout << noofmenseats << endl;
    cout << "                     THE NUMBER OF SEATS SPECIAL PERSONS: ";
    cout << noofspecialseats << endl;
    cout << "                     THE NUMBER OF SEATS FOR VVIP PASSENGERS: ";
    cout << noofvvip << endl;
}

ticket.h

#pragma once

class ticket
{
    int resno;
    int age;
    int tokids;
    int towomen;
    int tomen;
    int tospecial;
    int tovvip;
    int noofkidsseats;
    int noofwomenseats;
    int noofmenseats;
    int noofspecialseats;
    int noofvvip;
    char *pname;
    char *status;
public:
    ticket();
    void reservation();
    int returnresno();
    void cancellation();
    void print();
}tick;

ticket.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "ticket.h"
#include "bus.h"
using namespace std;


ticket::ticket()
{
    resno = 0;
    age = 0;
    tokids = 0;
    towomen = 0;
    tomen = 0;
    tospecial = 0;
    tovvip = 0;
    pname = '\0';
    status = '\0';
}
int ticket::returnresno()
{
    return resno;
}
void ticket::print()
{
    int f = 0;
    system("cls");
    ifstream fn("Ticket1.dat", ios::out); fn.seekg(0);
    if (!fn)
    {
        cout << "ERROR IN THE FILE ";
    }
X:
    cout << "ENTER THE RESERVATION NO ";
    int n;
    cin >> n;
    while (!fn.eof())
    {
        fn.read((char*)&tick, sizeof(tick));
        if (n == resno)
        {
            f = 1;
            system("cls");
            cout << "NAME: ";
            cout << pname;
            cout << "AGE: ";
            cout << age;
            cout << "PRESENT STATUS: ";
            cout << status;
            cout << "RESERVATION NUMBER: ";
            cout << resno;
            cout << "PRESS ANY KEY TO CONTINUE ";
            system("pause");
        }
    }
    if (f == 0)
    {
        system("cls");
        cout << "UNRECOGINIZED RESERVATION NO !!! WANNA RETRY ? (Y / N) ";
        char a;
        cin >> a;
        if (a == 'y' || a == 'Y')
        {
            system("cls");
            goto X;
        }
        else
        {
            cout << "PRESS ANY KEY TO CONTINUE";
            system("pause");
        }
    }
    fn.close();
}
void ticket::reservation()
{
    system("cls");
    cout << "RESERVATION ";
    cout << "ENTER THE BUS NO: ";
    int bno, f = 0; cin >> bno; ofstream file;
    ifstream fin("bus1.dat", ios::out); fin.seekg(0);
    if (!fin)
    {
        system("cls");
        cout << "ERROR IN THE FILE ";
        system("cls");
        while (!fin.eof())
        {
            fin.read((char*)&bs, sizeof(bs)); int z;
            z = bs.rbusno();
            if (bno == z)
            {
                f = 1;
                noofkidsseats = bs.rnoofkidsseats();
                noofwomenseats = bs.rnoofwomenseats();
                noofmenseats = bs.rnoofmenseats();
                noofspecialseats = bs.rnoofspecialseats();
                noofvvip = bs.rnoofvvip();
            }
        }
        if (f == 1)
        {
            file.open("Ticket1.dat", ios::app);
        S:
            system("cls");
            cout << "NAME:";
            cin >> pname;
            cout << "AGE:";
            cin >> age;
            system("cls");
            cout << "SELECT THE CATEGORY WHICH YOU WISH TO TRAVEL";
            cout << "1.KIDS CATEGORY: ";
            cout << "2.WOMEN CATEGORY: ";
            cout << "3.MEN CATEGORY: ";
            cout << "4.SPECIAL CATEGORY: ";
            cout << "5.SECOND CLASS SLEEPER: ";
            cout << "ENTER YOUR CHOICE ";
            int c;
            cin >> c;
            switch (c)
            {
            case 1:
                tokids++;
                resno = rand();
                if ((noofkidsseats - tokids)>0)
                {
                    status = "confirmed";
                    cout << "STATUS";
                    puts(status);
                    cout << "RESERVATION NO: ";
                    cout << resno;
                    system("pause");
                    file.write((char*)&tick, sizeof(tick)); break;

                    status = "pending";
                    cout << "STATUS";
                    puts(status);
                    cout << "RESERVATION NO";
                    cout << resno;
                    system("pause");
                    file.write((char*)&tick, sizeof(tick)); break;
                }

            case 2:
                towomen++;
                resno = rand();
                if ((noofwomenseats - towomen)>0)
                {
                    status = "confirmed";
                    cout << "STATUS";

                    puts(status);
                    cout << "RESERVATION NO: ";
                    cout << resno;
                    system("pause");
                    file.write((char*)&tick, sizeof(tick)); break;

                    status = "pending";
                    cout << "STATUS";
                    puts(status);
                    cout << "RESERVATION NO:";
                    cout << resno;
                    system("pause");
                    file.write((char*)&tick, sizeof(tick)); break;
                }

            case 3:
                tomen++;
                resno = rand();
                if ((noofmenseats - tomen)>0)
                {
                    status = "confirmed";
                    cout << "STATUS";
                    puts(status);
                    cout << "RESERVATION NO: ";
                    cout << resno;
                    system("pause");
                    file.write((char*)&tick, sizeof(tick)); break;
                }
                else
                {
                    status = "pending";
                    cout << "STATUS";
                    puts(status);
                    cout << "RESERVATION NO: ";
                    cout << resno;
                    system("pause");
                    file.write((char*)&tick, sizeof(tick)); break;
                }

            case 4:
                tospecial++;
                resno = rand();
                if ((noofspecialseats - tospecial)>0)
                {
                    status = "confirmed";
                    cout << "STATUS";
                    puts(status);
                    cout << "RESERVATION NO";
                    cout << resno;
                    system("pause");
                    file.write((char*)&tick, sizeof(tick)); break;
                }
                else
                {
                    status = "pending";
                    cout << "STATUS";
                    puts(status);
                    cout << "RESERVATION NO";
                    cout << resno;
                    system("pause");
                    file.write((char*)&tick, sizeof(tick)); break;
                }

            case 5:
                tovvip++;
                resno = rand();
                if ((noofvvip - tovvip)>0)
                {
                    status = "confirmed";
                    cout << "STATUS";
                    puts(status);
                    cout << "RESERVATION NO";
                    cout << resno;
                    system("pause");
                    file.write((char*)&tick, sizeof(tick)); break;
                }
                else
                {
                    status = "pending";
                    cout << "STATUS";
                    puts(status);
                    cout << "RESERVATION NO";
                    cout << resno;
                    system("pause");
                    file.write((char*)&tick, sizeof(tick)); break;
                }
            }
            cout << "DO YOU WISH TO CONTINUE BOOKING TICKETS (Y/N) ? ";
            char n;
            cin >> n;
            if (n == 'y' || n == 'Y')
            {
                goto S;
            }
        }
    }
    if (f == 0)
    {
        system("cls");
        cout << "ERROR IN THE BUS NUMBER ENTERED !!!";
        system("pause");
    }
    file.close();
}
void ticket::cancellation()
{
    system("cls");
    ifstream fin;
    fin.open("Ticket1.dat", ios::out);

    ofstream file;
    file.open("Temp1.dat", ios::app);
    fin.seekg(0);
    cout << "ENTER THE RESERVATION NO: ";
    int r, f = 0;
    cin >> r;
    if (!fin)
    {
        cout << "ERROR IN THE FILE !!!";
    }
    while (!fin.eof())
    {
        fin.read((char*)&tick, sizeof(tick)); int z;
        z = returnresno();
        if (z != r)
        {
            file.write((char*)&tick, sizeof(tick));
        }
        if (z == r)
        {
            f = 1;
        }
    }
    file.close(); fin.close();
    remove("Ticket1.dat");
    rename("Temp1.dat", "Ticket1.dat");
    if (f == 0)
    {
        cout << "NO SUCH RESERVATION IS MADE !!! PLEASE RETRY   ";
        system("pause");
    }
    else
    {
        cout << "RESERVATION CANCELLED";
        system("pause");
    }
}

的main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "bus.h"
#include "ticket.h"
using namespace std;

int main()
{
    ticket obj;
    bus obj1;
    int ch, r = 1000, j;
    cout << "WELCOME";
Z:
    cout << "BUS TICKET RESERVATION";
    cout << "==========================";
    cout << "1.BUS DETAILS";
    cout << "2.UPDATE BUS DETAILS ";
    cout << "3.RESERVING A TICKET ";
    cout << "4.CANCELLING A TICKET";
    cout << "5.DISPLAY THE PRESENT TICKET STATUS ";
    cout << "6.EXIT";
    cout << "ENTER YOUR CHOICE: ";
    cin >> ch;
    char n;
    switch (ch)
    {
        case 1:
        {
                  ifstream fin("bus1.dat", ios::out);
                  fin.seekg(0);
                  if (!fin)
                  {
                      cout << "ERROR IN THE FILE !!!";
                  }
                  else
                  {
                      while (!fin.eof())
                      {
                          fin.read((char*)&obj1, sizeof(obj1));
                          bs.display();
                      }
                  }
                  fin.close();
                  goto Z;
        }
        case 2:
        {
                  cout << "ENTER THE PASSWORD ";
                  cin >> j;
                  cout << "CHECKING PLEASE WAIT ";
                  system("pause");
              Y:
                  ofstream fout("bus1.dat", ios::app); 
                  bs.input();
                  fout.write((char*)&obj1, sizeof(obj1));
                  fout.close();
                  cout << "DO YOU WISH TO CONTINUE UPDATING ?(Y/N)";
                  cin >> n;
                  if (n == 'y' || n == 'Y')
                  {
                      goto Y;
                      goto Z;
                  }
                  else
                  {
                      goto Z;
                  }
        }
        case 3:
        {
                  obj.reservation();
                  goto Z;
        }
        case 4:
        {
                  obj.cancellation();
                  goto Z;
        }
        case 5:
        {
                  obj.print();
                  goto Z;
        }
        case 6:
        {
                  exit(0);
        }
    }
    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:3)

class bus
{
   // members
}bs;

不仅声明类型bus,还定义变量bstick也一样。

当您在多个cpp文件中包含这些标头时,您会获得这些变量的几个定义。

我确定你读过的C ++书籍都没有告诉你在头文件中定义这样的变量,所以不要这样做。在标题中声明 types ,在cpp文件中声明需要它们的变量