0

I'm trying to add a struct inside a class inside inside a .h file and that what I came up with until now:

//Rectangle.h
#pragma once
#include <bits/stdc++.h>
using namespace std;
class Rectangle
 {
  public:
    Rectangle() ;
    struct recpoints
    {
    double x1, y1, x2, y2, x3, y3, x4, y4;
    };
};


// Rectangle.cpp
#include "Rectangle.h"

Rectangle::Rectangle() {}
Rectangle::recpoints
    {
     recpoints() { x1 = y1 = x2 = y2 = x3 = y3 = x4 = y4 = 0.0; }
    };

Now that code produces errors

g++ -c main.cpp Rectangle.cpp
Rectangle.cpp:5:5: error: expected unqualified-id before ‘{’ token 

and I have no idea how should I fix it and how to use the struct in the Rectangle.cpp file?

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Basma Ashour
  • 33
  • 1
  • 9
  • Did you mean to write `Rectangle::recpoints::recpoints() { x1 = y1 = x2 = y2 = x3 = y3 = x4 = y4 = 0.0; }` as constructor definition in your `.cpp` file? Check [here](http://coliru.stacked-crooked.com/a/c041f3e9af8bb934) please. – πάντα ῥεῖ Aug 02 '18 at 17:16
  • Remember a `struct` behaves like a `class`. Only difference is default visibility. Declare your constructor for your struct in your header. – drescherjm Aug 02 '18 at 17:16
  • `#include ` is [very bad practice](https://stackoverflow.com/q/31816095/9254539). Who taught you that? – eesiraed Aug 02 '18 at 17:23
  • 1
    Unrelated: [`#include `](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) and [`using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) are both bad practice (Click 'em to find out why). Using them together magnifies some of their worst ill effects, and placing them inside a header is just plain nasty because it infects all files that include Rectangle.h. I highly recommend against doing this. You don't need either of them from the looks of it. – user4581301 Aug 02 '18 at 17:24
  • Thanks all for trying to help. – Basma Ashour Aug 02 '18 at 17:51

1 Answers1

4
  1. You forgot to declare the constructor recpoints() inside of struct recpoints in the header.
  2. The definition of recpoints() should be Rectangle::recpoints::recpoints() { /*your code here*/ } (not enclosed in Rectangle::recpoints{...};).
HolyBlackCat
  • 78,603
  • 9
  • 131
  • 207