0

I'm writing a code that's supposed to get input for the the size of a 2D array, location of an area inside the array, and the contents of the array. It doesn't do anything with those at this point, but when I tried to compile this code, it gives me errors about pointers. I've never used pointers before, and this code's no exception.

I've actually solved the problem by making the variable names numberless, but I'm really curious why this error occurs.

#include <bits/stdc++.h>
using namespace std;

int n, m, a, b;
int x0, x1, y0, y1;
int main()
{
//  freopen("seri3.gir","r",stdin);
//  freopen("seri3.cik","w",stdout);
    cin >> n >> m;
    cin >> x0 >> x1 >> y0 >> y1;
    x0--, x1--, y0--, y1--;
    int f[n][m],g[n][m];
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++)
        {
            cin >> f[i][j];
            if ((j > y0 || j < y1) && (i > x0 || i < x1)) {
                g[j][i] = f[j][i];
                f[j][i] = 0;
                cout << g[j][i] << endl;
            }
        }


}

This is one of the longest error messages I've ever gotten. It's a compiler error, basically telling that I can't compare pointers and integers and that it couldn't convert doubles over and over again. Note that I haven't used any pointers or doubles in the code. Here's the full error (The compiler is g++, and the IDE is Geany)

alpkaan35
  • 23
  • 5
  • 1
    Please post the error message. And note that arrays with runtime length (such as your `f` and `g` which depend on runtime values `n` and `m`) are a GCC extension and not valid in standard C++. – Angew is no longer proud of SO Sep 06 '19 at 11:53
  • 1
    do you get an error when you run the code or when you try to compile it? Please include at least the first parts of the error message. Even if you dont know how to read it, it holds valuable information and should be included in the question – 463035818_is_not_an_ai Sep 06 '19 at 11:54
  • Also [don't `#include `](https://stackoverflow.com/q/31816095/1782465). – Angew is no longer proud of SO Sep 06 '19 at 11:54
  • 1
    and while we are at it, dont use globals unless you need to and [better avoid `using namespace std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – 463035818_is_not_an_ai Sep 06 '19 at 11:56
  • @Angew The error code is way, way too long. Any way that I can make it so that it shows up only when you click it? Something like [Spoiler, click to show] on other forums. – alpkaan35 Sep 06 '19 at 11:57
  • @formerlyknownas_463035818 That's a compiler error. I'll include it in the question. – alpkaan35 Sep 06 '19 at 11:58
  • I am almost certain that others cannot reproduce your exact error message, so if you need help with it, it is essential that you include it in the question – 463035818_is_not_an_ai Sep 06 '19 at 11:58
  • @alpkaan35 You can copy the entire message into a service such as https://pastebin.com/ but you should also include a shortened version of the message in the question. – eerorika Sep 06 '19 at 11:59
  • @eerorika Pastebin is banned where I live, any good alternatives? – alpkaan35 Sep 06 '19 at 12:00
  • paste the error into the question (or parts of it) ?!? Fwiw I cannot reproduce: https://wandbox.org/permlink/F2KNANqCIY4wK8QU though I had to remove the infamous include – 463035818_is_not_an_ai Sep 06 '19 at 12:01
  • Found an alternative, adding it to the question. – alpkaan35 Sep 06 '19 at 12:04
  • Note that the [error message I get from your code](http://coliru.stacked-crooked.com/a/aed57a46b6703218) starts with a conflict between `y0` in your code and some other `y0` (or possibly `std::y0`) somewhere deep in GCC headers. Which is why `#include ` and `using namespace std;` are both *bad idead* (the first one the worse of the two). – Angew is no longer proud of SO Sep 06 '19 at 12:06
  • I dont understand why you insist so much on not including at least parts of the error in the question. You cannot use pastebin, others might not be able to use the link you posted. Quesitons should be selfcontained. Please include at least parts of the error message in the question – 463035818_is_not_an_ai Sep 06 '19 at 12:06
  • I've actually solved it with by using `xa, xb, ya, yb` instead but i'm really curious to what causes it as i would like to use the numbered versions, I'm only using becaue my teacher told me to. – alpkaan35 Sep 06 '19 at 12:08

3 Answers3

1

There are two errors that I am able to reproduce.

Firstly, POSIX standard specifies functions:

double y0(double x);
double y1(double x);

These functions are in conflict with your variable declarations, and cause the following diagnostic:

‘int y0’ redeclared as different kind of symbol
note: previous declaration ‘double y0(double)’
‘int y1’ redeclared as different kind of symbol
note: previous declaration ‘double y1(double)’

There will also be other errors because you are using these functions as if they were integers. To fix this: Don't declare names that are already in use.

A general solution to avoid name clashes to declare everything inside a namespace of your own. Then you'll just need to come up with one global name that isn't in use. A simple solution in this case is to simply not declare the variables globally at all, but use local variables instead.


Secondly, the program is ill-formed because the array size that you use is not a compile time constant. The resulting diagnostic:

ISO C++ forbids variable length array 'f' [-Wvla]
ISO C++ forbids variable length array 'g' [-Wvla]

To fix this, either use constant size arrays, or allocate them dynamically.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Neither compatibility nor performance isn't what I'm seeking because this is a simple assignment. – alpkaan35 Sep 06 '19 at 12:11
  • 2
    "_To fix this: Don't declare names that are already in use._" Or, alternatively, don't include every header known to man, with `#include `, and don't pull entire contents of those headers into global namespace with `using namespace std;` – Algirdas Preidžius Sep 06 '19 at 12:24
  • @AlgirdasPreidžius Actually, neither the use of ``, nor `using namespace std;` are why declaring global `y0` and `y1` are a problem; The inclusion of `` is just something that causes the problem to have a visible effect. You could be happily using those *taken* names thinking everything is fine, and later include `` only to find that the program stopped working on POSIX systems. `` is a handy tool to put your namespace discipline to the test. But of course, it shouldn't be left in your code after that test :) – eerorika Sep 06 '19 at 12:34
  • Even though it wasn't the cause of this problem, `using namespace std;` should only be used for fitting code to presentation slides. – eerorika Sep 06 '19 at 12:34
0

I've fixed it by using #include <stdio.h> and #include <iostream> instead of #include <bits/stdc++>. Thanks to eerorika for mentioning that y0 and y1 are functions.

alpkaan35
  • 23
  • 5
0

Your code written in c++ rather than in g++ …

#include <iostream>

int main()
{
    using std::cin;
    using std::cout;
    using std::endl;
    int n, m;// , a, b; a and b are never used
    int x0, x1, y0, y1; // Declare them as local variables to avoid clash with ::y0() function
    //  freopen("seri3.gir","r",stdin);
    //  freopen("seri3.cik","w",stdout);
    cin >> n >> m;
    cin >> x0 >> x1 >> y0 >> y1;
    x0--, x1--, y0--, y1--;
    //  int f[n][m], g[n][m]; // Run-time sized arrays are g++ - not c++ !!!
    int f[10][10], g[10][10];
    if (n > 10 || m > 10)
        cout << "Array to big!" << endl;
    else for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> f[i][j];
            if ((j > y0 || j < y1) && (i > x0 || i < x1)) {
                g[j][i] = f[j][i];
                f[j][i] = 0;
                cout << g[j][i] << endl;
            }
        }
    }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • There is no `std::y0`. There is `::y0` that is not specified by C++ standard, but by the POSIX standard. – eerorika Sep 06 '19 at 12:40
  • I... am writing the code in C++. g++ is the compiler, is there a language called G++ that I'm unaware of? – alpkaan35 Sep 06 '19 at 13:01
  • @alpkaan35 GCC uses its own dialect of C++ called gnu++. In fact, all compilers (at least mainstream ones) have their own dialect. You can ask g++ to use standard c++ with options such as `-std=c++17 -pedantic -Wall`. – eerorika Sep 06 '19 at 13:18
  • @eerorika Never knew of that! Added to topics to research. – alpkaan35 Sep 06 '19 at 13:31
  • @alpkaan35 - The use of `bits/stdc++.h` is something that, although new to me, has come up quite a lot in recent days, since I started my activity on Stack Overflow. It's a heavily debated topic! See here: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – Adrian Mole Sep 06 '19 at 14:43
  • @Adrian I've already stopped using `bits/stdc++.h`. I haven't realised it because I had been using it for a while now, but it really screws up the compilation time. Definitely never using it again unless I need every single library for C++ ever. – alpkaan35 Sep 06 '19 at 14:50