0

ERROR WHICH I GOT

Multiple defintion of veh

Main.cpp

int main()
  {
    int choice;
    char ans;
   // string filen;
    system ( "cls" ) ;
    std::cout<<"ENTER FILE NAME with Extenstion:";
    std::cin>>filen;
    veh.at(i).vehcileLoad();

car.h

class vehicle
{
    public:
        vehicle();
        virtual ~vehicle();
        void addVehicle();
        void deleteVehicle();
        void printvehicle(vehicle v);
        void show();
        void showleft();
        void vehcileLoad();

    protected:

    private:
    std::string pltno;
    date dt;
    lime arrive;
    lime departure;
};
std::vector<vehicle> veh(100);
std::vector<vehicle> vehleft(100);
std::fstream file;
std::string filen;
#endif // VEHICLE_H

Implementation FILE

The file contains all functions of the vehicle such as add vehcile etc A bit too long to include it but i did not redfine it there

What i Tried

I have tried using Extern But it did not work I tried adding them to the implementation file and main file it did not work either

std::vector<vehicle> veh(100);
std::vector<vehicle> vehleft(100);
std::fstream file;
std::string filen;

These lines above are the ones which are getting the multiple definition of ... issue even though i only declared it in the header file how can i fix this issue please help me im stuck??

talonmies
  • 70,661
  • 34
  • 192
  • 269
  • 1
    Don't *define* variables in header files, only *declare* them. As in `extern std::vector veh;` Do the definition (`std::vector veh(100);`) in a single source file. – Some programmer dude May 04 '22 at 08:06
  • 2
    Even better: Don't use global variables at all! Define locally inside functions, and pass as arguments to the functions that needs them. – Some programmer dude May 04 '22 at 08:07
  • *even though i only declared it in the header file* - `#include` is very simple directive, it copies and pastes file content verbatim where `include` was. So even though you define the variables in one header file, by `include`'ing it in two source files, you create one definition of each variable in each source file, and there should be only one definition of each variable in the whole program. – Yksisarvinen May 04 '22 at 08:11

0 Answers0