Thread: Error-End Of File?

  1. #1
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114

    Error-End Of File?

    --------------------Configuration: Practice - Win32 Debug--------------------
    Compiling...
    Source.cpp
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\Practice\Source.cpp(32) : fatal error C1004: unexpected end of file found
    Error executing cl.exe.

    Practice.exe - 1 error(s), 0 warning(s)

    I just picked up two new c++ books and i wrote this little thing to practice using classes, as I just learned about them a few hours ago, and im getting that error message. Here is the source:

    Code:
    /*
    THE GOAL HERE IS TO MAKE A CAR OUT OF CLASSES. GIVE IT A FEW METHODS, AND THEN USE THEM!
     */
    
    class Car
    {
    
    public:
    	Car(){cout<<"YOU HAVE A BRAND NEW CAR! THE TANK IS FULL!"<<endl;}
    	~Car(){}
    	int Drive() {cout<<"VROOM YOU DROVE 10 MILES!"<<endl; GasLeft=GasLeft-1; return(0);}
    	void HonkHorn(){cout<<"HONK HONK! GET OUT OF THE WAY!!!"<<endl;
    private:
    	int GasLeft=10;
    
    };
    
    int main()
    {
    
    Car MyCar;
    cin.get();
    system("cls");
    MyCar.HonkHorn();
    cin.get();
    system("cls");
    MyCar.Drive();
    
    return(0);
      
    }
    Last edited by Frantic-; 06-14-2005 at 09:31 PM.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Your braces aren't matching up, there is probably one missing. I'd look at the HonkHorn function.

    BTW, better indentation and code formatting help reduce those kinds of errors.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    This isn't going to work either:

    int GasLeft=10;

    Class member variables are initialized in a constructor.

    Your code is a mess. Right now, it's more important to adopt a neat coding style than getting your program to compile. You are never going to figure out where your errors are with code as messy as that. In addition, if you want people to help you, your code has to be readable.

    If the authors of your books wrote code like you posted, you wouldn't me able to understand any of their sample programs.

    Here is an example of a format you might adopt:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    class Apple
    { 
    private:
    	string color;
    
    public:
    	Apple(string c)  //constructor
    	{
    		color = c;
    	}
    
    	void show()
    	{
    		cout<<"My apple's color is: "<<color<<endl;
    	}
    
    };
    
    int main()
    {
    	Apple myApple("red");
    	myApple.show();
    
    	return 0;
    }
    Last edited by 7stud; 06-15-2005 at 05:00 AM.

  4. #4
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    Code:
    /*
    THE GOAL HERE IS TO MAKE A CAR OUT OF CLASSES. GIVE IT A FEW METHODS, AND THEN USE THEM!
     */
    
    #include <iostream>
    using namespace std;
    
    //=========================================================================
    
    class Car
    {
    
    public:
    	Car(int GasLeft);
    	~Car();
    	int Drive() ;
    	void HonkHorn();
    private:
    	int GasLeft;
    
    };
    
    //==========================================================================
    
    Car::Car(int GasLeft)
    {
    	GasLeft=10; 
    	cout<<"YOU HAVE A BRAND NEW CAR! THE TANK IS FULL!"<<endl;
    }
    
    //==========================================================================
    
    Car::~Car()
    {
    }
    
    //=========================================================================
    
    int Car::Drive()
    {
    	cout<<"VROOM YOU DROVE 10 MILES!"<<endl; 
    	GasLeft=GasLeft-1; 
    	return(0);
    }
    
    //=========================================================================
    
    void Car::HonkHorn()
    {
    	cout<<"HONK HONK! GET OUT OF THE WAY!!!"<<endl;
    }
    
    //=========================================================================
    
    int main()
    {
    
    	Car MyCar;
    	cin.get();
    	system("cls");
    
    	MyCar.HonkHorn();
    	cin.get();
    	system("cls");
    
    	MyCar.Drive();
    
    return(0);
    }
    --------------------Configuration: Practice - Win32 Debug--------------------
    Compiling...
    Source.cpp
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\Practice\Source.cpp(22) : error C2511: 'Car::Car' : overloaded member function 'void (void)' not found in 'Car'
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\Practice\Source.cpp(8) : see declaration of 'Car'
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\Practice\Source.cpp(43) : error C2512: 'Car' : no appropriate default constructor available
    Error executing cl.exe.

    Practice.exe - 2 error(s), 0 warning(s)
    Last edited by Frantic-; 06-15-2005 at 06:06 AM.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    It is because you define your constructor as car(int) and define it as car().
    Woop?

  6. #6
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    ok fixed that.

    --------------------Configuration: Practice - Win32 Debug--------------------
    Compiling...
    Source.cpp
    C:\Documents and Settings\Owner\Desktop\Comp Prog\C++\Sams C++\Lesson 10\Practice\Source.cpp(43) : error C2512: 'Car' : no appropriate default constructor available
    Error executing cl.exe.

    Practice.exe - 1 error(s), 0 warning(s)

  7. #7
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    you need to define a default constructor.
    Code:
    public:
                    Car(){}
    	Car(int GasLeft);

  8. #8
    Programmer Frantic-'s Avatar
    Join Date
    Dec 2004
    Posts
    114
    ahh thanks.

    this code works. How is the coding style now? any better?

    Code:
    /*
    THE GOAL HERE IS TO MAKE A CAR OUT OF CLASSES. GIVE IT A FEW METHODS, AND THEN USE THEM!
     */
    
    #include <iostream>
    using namespace std;
    
    //=========================================================================
    
    class Car
    {
    
    public:
    	Car();
    	~Car();
    	int Drive() ;
    	void HonkHorn();
    	void GasAlert();
    private:
    	int GasLeft;
    
    };
    
    //==========================================================================
    
    Car::Car()
    {
    	GasLeft=10; 
    	cout<<"YOU HAVE A BRAND NEW CAR! THE TANK IS FULL!"<<endl;
    }
    
    //==========================================================================
    
    Car::~Car()
    {
    }
    
    //=========================================================================
    
    int Car::Drive()
    {
    	cout<<"VROOM YOU DROVE 10 MILES!"<<endl; 
    	GasLeft=GasLeft-1; 
    	return(0);
    }
    
    //=========================================================================
    
    void Car::HonkHorn()
    {
    
    	cout<<"HONK HONK! GET OUT OF THE WAY!!!"<<endl;
    
    }
    
    //=========================================================================
    
    void Car::GasAlert()
    {
    
    	cout<<"You Have " << GasLeft <<" gallons left."<<endl;
    
    }
    
    //=========================================================================
    
    int main()
    {
    
    	Car MyCar;
    	MyCar.GasAlert();
    	cin.get();
    	system("cls");
    
    	MyCar.HonkHorn();
    	cin.get();
    	system("cls");
    
    	MyCar.Drive();
    	MyCar.GasAlert();
    
    return(0);
    }

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    100% better!

    You can get rid of this stuff, unless you want to have it:

    //==================

    For experienced C++ programmers, it's unnecessary as far as code readability goes.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    think the next step for you should be default parameters

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM