C++ 1075 Error - end of file found before the left brace

Please see code below - getting this error in my .cpp file - can't see where the problem is...
:
#include <iostream>
#include "figure.h"
#include "rectangle.h"
#include "triangle.h"

using std::cout;
using std::endl;
//using namespace std;

int main( )
{
  Triangle tri;
  tri.draw();
  cout <<"\nDerived class Triangle object calling center().\n";
  tri.center(); //Calls draw and center

  Rectangle rect;
  rect.draw();
  cout <<"\nDerived class Rectangle object calling center().\n";
  rect.center();//Calls draw and center
 
  return 0;
}
vixenmrbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

milindsmCommented:
I tried running it without

#include "figure.h"
#include "rectangle.h"
#include "triangle.h"

and all related stuff...and it worked...!!!

I hope figure, triangle and rectangle files (source and header) don't have any syntax error...!!!!
0
phoffricCommented:
Take a look in your header files (or post them), and see if there is a missing curly brace in one of them.
0
itsmeandnobodyelseCommented:
>>>> Take a look in your header

Beside of checking brackets of all kind {[()]} you also may check for further include statements (i. e. cyclic includes) and all preprocessor statements like #ifndef  have a proper #endif.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

itsmeandnobodyelseCommented:
You could place a

#error "Processed until here"

in your cpp to spot which include statement was the pulprit. The (pre)compiler will exit at the #error and all statements before were ok.
0
vixenmrbAuthor Commented:
Here are my class files: -->> The error looks like it is in the triangle.h file but can't understand this because triangle is exactly identical to rectangle.h. I had just copied and pasted and just changed the content.

Error: fatal error C1075: end of file found before the left brace '{' triangle.h(12)' was matched

The assignment was to create a base class Figure. Create rectangle and triangle classes as derived classes of figure. Each class has stubs for member functions erase and draw. Ea of these funcs outputs a msg telling what func has been called & the class it was called from. Stubs only output this msg & nothing else. The member func center calls erase and draw to erase and redraw the figure at the center. Because you have only stubs for erase & draw, center will not do any "centering" but will call the member functions erase and draw. Also add an output msg in the mem func center that announces that center is being called. The member funcs should take no arguments.

FIGURE-----------------------------------------header file (base class)
#ifndef FIGURE_H
#define FIGURE_H
#include <iostream>
using namespace std;

namespace MyFigures
{
class Figure {

public:
      
      virtual void center( )
      {
            cout << "You are now calling the center function:"<<endl;
            erase();
            draw();
      }

      
      virtual void draw() = 0; //pure virtural function
      
      virtual void erase() = 0;//pure virtual function
      
 
 };
#endif }


TRIANGLE---------------------------------------header file
 
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "figure.h"
#include <iostream>

using namespace std;

namespace MyFigures
{
      class Triangle : public Figure
      {
      public:
            void draw ( )//mem func draw declared/defined
            {
                  cout <<"You have called the function draw."<<endl;
                  cout <<"From the class triangle."<<endl;
            }
            void erase ( )//mem func draw declared/defined
            {
              cout <<"You have called the function draw."<<endl;
              cout <<"From the class triangle."<<endl;
            }
      };
#endif }//Myfigures
             
RECTANGLE---------------------------------------------------------------------------header file

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "figure.h"
#include <iostream>

using namespace std;

namespace MyFigures
{
      class Rectangle : public Figure
      {
      public:
            void draw ( )//mem func draw declared/defined
            {
                  cout <<"You have called the function draw."<<endl;
                  cout <<"From the class rectangle."<<endl;
            }
            void erase ( )//mem func draw declared/defined
            {
              cout <<"You have called the function draw."<<endl;
              cout <<"From the class rectangle."<<endl;
            }
      };
#endif }//Myfigures
//#endif//Rectangle

0
itsmeandnobodyelseCommented:
>>>> #endif }


See at end of figure.h.

Is there a curly bracket ??
0
itsmeandnobodyelseCommented:
I see you have the } always after the #endif.

It must be in the line before.

You get the error when a header was read second time. Then because of preprocessing only the closing } was included waht leads to the error.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
itsmeandnobodyelseCommented:
>>>> because triangle is exactly identical to rectangle.h.

That is beause rectangle.h was included only once but figure.h and triangle.h was included also in the headers.
#ifndef FIGURE_H
#define FIGURE_H 
#include <iostream>
using namespace std; 

namespace MyFigures
{
class Figure {

public:
      
      virtual void center( )
      { 
            cout << "You are now calling the center function:"<<endl;
            erase();
            draw();
      } 

      
      virtual void draw() = 0; //pure virtural function 
      
      virtual void erase() = 0;//pure virtual function 
       
 
 };
}
#endif // FIGURES_H


TRIANGLE---------------------------------------header file 
 
#ifndef TRIANGLE_H
#define TRIANGLE_H
#include "figure.h"
#include <iostream>

using namespace std; 

namespace MyFigures
{
      class Triangle : public Figure
      {
      public:
            void draw ( )//mem func draw declared/defined 
            {
                  cout <<"You have called the function draw."<<endl; 
                  cout <<"From the class triangle."<<endl; 
            }
            void erase ( )//mem func draw declared/defined 
            {
              cout <<"You have called the function draw."<<endl;
              cout <<"From the class triangle."<<endl; 
            }
      };
}
#endif // TRIANGLE_H
              
RECTANGLE---------------------------------------------------------------------------header file 

#ifndef RECTANGLE_H
#define RECTANGLE_H
#include "figure.h"
#include <iostream>

using namespace std; 

namespace MyFigures
{
      class Rectangle : public Figure
      {
      public:
            void draw ( )//mem func draw declared/defined 
            {
                  cout <<"You have called the function draw."<<endl; 
                  cout <<"From the class rectangle."<<endl; 
            }
            void erase ( )//mem func draw declared/defined 
            {
              cout <<"You have called the function draw."<<endl;
              cout <<"From the class rectangle."<<endl; 
            }
      };
}
#endif // RECTANGLE_H

Open in new window

0
vixenmrbAuthor Commented:
Thanks - itsmeandnobodyelse.. That helped but when I made the correction it created 15 additional errors.  I then realized I needed to change the name of the namespace in the .cpp file to the new one I created and that solved the issue.  
0
itsmeandnobodyelseCommented:
>>>> ... and that solved the issue

Very fine.

One last tip:

Remove the 'using namespace std;' from the headers. Instead use std::cout and std::endl in the output statements and put them between

#ifdef _DEBUG
                  std::cout <<"You have called the function draw."<< std::endl;
                  std::cout <<"From the class triangle."<< std::endl;
#endif

'using' clauses in header files are not good programming cause you force each cpp which includes your header to the std namespace that way. They couldn't try a different string class that way or were not able to redirect 'cout' to another device.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.