T
T
Tolstohabr2020-02-11 23:33:20
C++ / C#
Tolstohabr, 2020-02-11 23:33:20

Why "identifier not defined"?

here is the whole code

#include <iostream> 
#include <windows.h> 
using namespace std;
class Triangle
{
public:
  int a;
  int b;
  int c;
  int x;
  int y;
  Triangle(int a1, int b1, int c1) { a = a1; b = b1; c = c1; }
  double S() {
    return (sqrt((a + b + c) * (1.0 / 2) * ((a + b + c) * (1.0 / 2) - c) * ((a + b + c) * (1.0 / 2) - a) * ((a + b + c) * (1.0 / 2) - b)));
  }
  void move()
  {
    cout << endl;
    cout << "введи точку икс : ";
    cin >> x;
    cout << "введи точку игрек : ";
    cin >> y;
    cout << "фигура перемещена в точку " << x << " " << y << endl;
  }
};
class Rectangl
{
public:
  int a2;
  int b2;
  int x2;
  int y2;
  double sinx;
  Rectangl(int a1, int b1, double sinx1) { a2 = a1; b2 = b1; sinx = sinx1; }
  double S1() {
    return (0.5 * a2 * b2 * sinx);
  }
  void move1()
  {
    cout << endl;
    cout << "введи точку икс : ";
    cin >> x2;
    cout << "введи точку игрек : ";
    cin >> y2;
    cout << "фигура перемещена в точку " << x2 << " " << y2 << endl;
  }
};
class Work : public Triangle, public Rectangl
{
public:
  Work(int a, int b, int c, int a2, int b2 , double sinx ) :Triangle(a,b,c), Rectangl(a2,b2,sinx) {};
  void compare()
  {
    cout << S() <<endl;
    cout << S1() <<endl;

    if (S1() > S()) {
      cout << "Площадь четырёхугольника больше \n";
    }
    if (S1() < S()) {
      cout << "Площадь треугольника больше \n";
    }
    if (S1() == S()) {
      cout << "Площадь одинакова";
    }
  }
};
int main()
{
  SetConsoleCP(1251);
  SetConsoleOutputCP(1251);
  int a, b, c, a2, b2;
  double sinx;
  cout << "создать объекты?(жми 0)" << endl;
  int flak;
  cin >> flak;
  if (flak == 0) {
    cout << "введи стороны треугольника";
    cin >> a;
    cin >> b;
    cin >> c;
    cout << "введи диагонали и синус угла между ними для чутырёхугольника";
    cin >> a2;
    cin >> b2;
    cin >> sinx;

    Work* p;
    Triangle* p1;
    Rectangl* p2;
    p1 = new Triangle(a, b, c);
    p2 = new Rectangl(a2, b2, sinx);
    p = new Work(a, b, c, a2, b2, sinx);
    p->compare();
  }
  cout << "Что хочешь?" << endl;
  cout << "удалить объекты(жми1)" << endl;
  cout << "переместить объекты(жми2)" << endl;
  cout << "сравнить объекты(жми3)" << endl;

  int flak1;
  cin >> flak1;

  if (flak1 == 1) {
    delete p;
    delete p1;
    delete p2;
  }

  system("PAUSE");
  return 0;
}


interested in three lines at the end with delete

Why is everything fine outside if, but inside the identifier is not defined

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2020-02-11
@Tolstohabr

1. Pull p, p1, p2 out of the if. Read "scope".
2. I would do this…

Work* p = NULL;
if () {
  p = new Work();
}
delete p;

It works, because NULL can be safely destroyed, and nothing will happen.
In C++11, you can also use smart pointers.
std::unique_ptr<Work> p;
if () {
  p = std::make_unique<Work>();  // простите, это Си++14, на 11 чуть не так.
}

J
jcmvbkbc, 2020-02-11
@jcmvbkbc

Because p, p1 and p2 are local to the block if (flak == 0)and don't exist outside of it.
See _

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question