Link to home
Start Free TrialLog in
Avatar of vixenmrb
vixenmrb

asked on

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;
}
Avatar of milindsm
milindsm
Flag of India image

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...!!!!
Avatar of phoffric
phoffric

Take a look in your header files (or post them), and see if there is a missing curly brace in one of them.
>>>> 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.
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.
Avatar of vixenmrb

ASKER

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

>>>> #endif }


See at end of figure.h.

Is there a curly bracket ??
ASKER CERTIFIED SOLUTION
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>> 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

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.  
>>>> ... 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.