Linking problems

This is a discussion on Linking problems within the C++ Programming forums, part of the General Programming Boards category; I wrote a program and header files that compiles and runs correctly (Visual C++), but when I try to write ...

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    14

    Linking problems

    I wrote a program and header files that compiles and runs correctly (Visual C++), but when I try to write implementation files for the headers the program no longer works, it compiles but when I try to run it I get 6 unresolved externals errors eg

    main.obj : error LNK2001: unresolved external symbol "public: class Vehicle __thiscall Vehicle:perator+(class Journey &)" (??HVehicle@@QAE?AV0@AAVJourney@@@Z)
    Debug/main.exe : fatal error LNK1120: 6 unresolved externals



    , I am not sure what I am doing wrong as I am new to this.

    This is the code as it works

    Code:
    =========Header file 1
    
    #include <iostream>
    using namespace std;
    
    class FuelPurchase;
    class Journey;
    
    class Vehicle
    	{
    	
      public:		
    	Vehicle(string makeValue, string modelValue, int yearValue);
    	Vehicle (int yearValue); 
    	Vehicle();
    
    	void Print();
    		
    	Vehicle b(Vehicle);
    
    	int vCost;
    	int vLitre;
    	int vKm;
          
    	Vehicle operator +(FuelPurchase &total);
    	Vehicle operator +(Journey &km);
    
    	private:
    		int year;
    		string make;
    		string model;
    
    	
    	};
    
    =========Header file 2
    
    #include <iostream>
    using namespace std;
    
    class Journey
    	{
    	
      public:
    		
    	Journey(float journeyValue);        
    	Journey();
    	float journey;
    	
    	};
    
    =========Header file 3
    
    #include <iostream>
    using namespace std;
    
    class FuelPurchase
    	{
      public:
    		
    	FuelPurchase(int litreValue, int costValue);
                    int cost;
    	int litre;	
    	};
    
    =============main.cpp
    
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include "Vehicle.h"
    #include "FuelPurchase.h"
    #include "Journey.h"
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	
    Vehicle a("Audi", "A6", 2003);
    Vehicle b("Jaguar","X200", 2003);
    Vehicle c("Mercedes-Benz","CL600",2003);
    
    cout<< "Original statistics:" << endl;
    cout<< "====================" << endl;
    
    a.Print();
    b.Print();
    c.Print();
    
    a = a + FuelPurchase (50, 60);
    a = a + Journey (150);
    a = a + FuelPurchase (12, 15);
    
    b = b + FuelPurchase (50, 60);
    b = b + Journey (250);
    b = b + FuelPurchase (22, 22);
    
    c = c + FuelPurchase (50, 60);
    c = c + Journey (350);
    c = c + FuelPurchase (50, 40);
    
    cout<< "Final statistics:" << endl;
    cout<< "=================" << endl;
    a.Print();
    b.Print();
    c.Print();
    
    
    return(0);
    }
    
    
    Vehicle::Vehicle(string make_, string model_, int year_) 
    					: make(make_), model(model_), year(year_)
    {
    
    	   	vCost = 0;
    		 vLitre = 0;
    		 vKm = 0;
    
    	
    }
    
    
    Vehicle::Vehicle(int year_) 
    					: year(year_)
    {
    }
    
    
    FuelPurchase::FuelPurchase(int cost_,	int litre_)
    					: cost(cost_), litre(litre_)
    {
    }
    
    
    Journey::Journey(float journey_)
    					: journey(journey_)
    {
    }
    
    
    Vehicle Vehicle::operator+(FuelPurchase &total)
    {
    
    	Vehicle Update( *this);
    	Update.vLitre += total.litre;
    	Update.vCost += total.cost;
    
    	return Update;
    	
    }
    
    
    Vehicle Vehicle::operator+(Journey &km)
    {
    
    	Vehicle Update( *this);
    	Update.vKm += km.journey;
    	
    	return Update;
    	
    }
    
    void Vehicle::Print()
    {
    	
    cout<<"Vehicle: "<<make<<" "<<model<< endl;
    cout<<vKm<<" travelled requiring "<<vLitre<<" of fuel at a cost of $"<<vCost<<endl;
    	
    // and so on
    	
    }
    What I did next was to change the relevant procedures into an implementation file, eg:

    Code:
    =============vehicle.cpp
    #include "Vehicle.h"
    #include <iostream>
    #include <string>
    #include <iomanip>
    #include "FuelPurchase.h"
    #include "Journey.h"
    
    using namespace std;
    
    Vehicle::Vehicle() 
    					
    {
    	                vCost = 0;
    		 vLitre = 0;
    		 vKm = 0;
    		 model = "";
    		 make = "";
    		 vKm = 0;
    
    }
    
    Vehicle::Vehicle(string make_, string model_, int year_) 
    					: make(make_), model(model_), year(year_)
    {	   		
    }
    
    Vehicle Vehicle::operator+(FuelPurchase &total)
    {
    	Vehicle Update( *this);
    	Update.vLitre += total.litre;
    	Update.vCost += total.cost;
    	return Update;	
    }
    
    
    Vehicle Vehicle::operator+(Journey &km)
    {
    	Vehicle Update( *this);
    	Update.vKm += km;	
    	return Update;	
    }
    
    void Vehicle::Print()
    {	
    // same as working program
    
    }
    
    =============Journey.cpp
    #include "Journey.h"
    #include <iostream>
    
    using namespace std;
    
    Journey::Journey()
     {	
      km= 0;
    }
    
    Journey::Journey(int km_)
    			: km(km_)
    {		
    }
    
    =============FuelPurchase.cpp
    
    #include <iostream>
    
    using namespace std;
    #include "FuelPurchase.h"
    
    
    FuelPurchase::FuelPurchase()
    {
    }
    
    FuelPurchase::FuelPurchase(int cost_,	int litre_)
    					: cost(cost_), litre(litre_)
    {
    cost = 0;
    litre =0;
    }
    The main code ends now with the return value, but is otherwise the same.

    If anyone can help me, it would be much appreciated!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    Hmmm....what's different?

    Code:
    //old version
    Vehicle Vehicle:perator+(Journey &km)
    {
    	Vehicle Update( *this);
    	Update.vKm += km.journey;
    	return Update;
    }
    
    //new version
    Vehicle Vehicle:perator+(Journey &km)
    {
    	Vehicle Update( *this);
    	Update.vKm += km;	
    	return Update;	
    }
    gg

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    Thanks for that, I made the change to my code, but it still has the same problem (which is a bit of a worry, as it means that there are further problems its not even finding yet!!).

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    zip up the files and post it - that'll get your problems solved the quickest.

    gg

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    Thanks Codeplug, I have posted my non working version, where I have tried to split the file.

    Cheers
    Attached Files Attached Files

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    FuelPurchase.cpp - you have implemented a default constructor but you didn't declare one in the .h file
    Vehichle.cpp - you didn't make the change from my earlier post!!

    gg

  7. #7
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    Thanks for the help gg, I made the changes you suggested, I must have forgotten to save the change last time in my confused, frustrated state! I have fixed those problems, but am still getting the same error messages.

  8. #8
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,645
    When I fixed those two problems, it compiled with 0 errors and 0 warnings in VC++ 6.0

    - Add a default constructor declaration in FuelPurchase.h "FuelPurchase();"
    - In Vehicle.cpp, in operator+(Journey&), you need to have "Update.vKm += km.km;"

    You also need to start understanding these compiler errors what usually causes them - cuz you're going to have the for the rest of your programming life.

    gg

  9. #9
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    I am trying to understand them gg, seems like a lot to learn some days ! The errors are not compile errors, the program compiles, but when I try to run the program I get the linker errors I mentioned, and it wont execute.

    cheers,
    jamjar

  10. #10
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    Try cleaning up some space on your drive.
    The problem may be there is not enough room for the linkage.

    Since you say that all the files were compiled without errors and the errors are external to main

    Be sure to compile your cpp's individually and notice any problems.

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    14
    Thanks for all of your help, I took a break and thought about what you both said, after a good nights sleep, it all made a bit more sense to meand my program is now working!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ OpenGL linking problems
    By KoshiB in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2006, 04:25 PM
  2. Linking problems, class problems
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-22-2005, 12:47 AM
  3. Linking problems trying to compile QT application
    By Maragato in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2005, 09:08 PM
  4. Grrr.... SDL Linking Problem
    By 7EVEN in forum Game Programming
    Replies: 5
    Last Post: 08-12-2005, 08:44 PM
  5. More linker problems
    By Ganoosh in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2005, 10:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21