Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why getting undefined when it is defined using #define

I have defined vector<int> as vi using #define in my C++ program. But when I am trying to use vi it is showing identifier "v" is undefinedC/C++(20).

I have installed mingw version: g++.exe (MinGW.org GCC-6.3.0-1) 6.3.0
I am using Visual studio code as my code editor.

Here is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#include <bits/stdc++.h>

using namespace std;

#define ll long long;
#define vi vector<int>;
#define all(v) v.begin(), v.end();
#define tle ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

void solution() {
  vi v;  // here I am getting error
}

int main() {
  tle;
  int test_case;
  cin >> test_case;
  while (test_case--) {
    solution();
  }

  return 0;
}

Why I am getting the error? How to solve this?

I am expecting to get solution or how to use #define in my C++ program

>Solution :

The issue you are facing is due to the incorrect usage of the #define macro in your code. When defining macros, you should not include a semicolon (;) at the end of the macro definition. The semicolon should be placed when you use the macro.

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

#define ll long long
#define vi vector<int>
#define all(v) v.begin(), v.end()
#define tle ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

void solution() {
  vi v;  // No error now
}

int main() {
  tle;
  int test_case;
  cin >> test_case;
  while (test_case--) {
    solution();
  }
  return 0;
}

By removing the semicolon after each #define, you have successfully defined the macros ll, vi, and all, and they can now be used correctly in your code. The semicolon should only be used when you use the macro, not when you define it.

With this change, the error "identifier ‘v’ is undefined" should be resolved, and your code should compile without any issues.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading