Problem fixed! Thanks a lot for the constructive suggestions!
I am unable to figure out what is the mistake in the following code. Is there something wrong with the way I am doing includes?
// This is utils.h
#ifndef UTILS_H
#define UTILS_H
#include <iostream>
#include <fstream>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <string>
#include <algorithm>
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef stack<int> si;
typedef queue<int> qi;
#define tr(c,i) for(typeof((c).begin()) i = (c).begin() ; i!=(c).end() ; ++i )
#define all(c) (c).begin(),(c).end()
#define cpresent(c,x) (find(all(c),x) != (c).end())
#endif
// ==============================================================
// Below is main.cpp
#include "utils.h"
int main() {
vi v;
}
On compiling “g++ main.cpp” I get the following error message:
utils.h:13: error: expected initializer before ‘<’ token
utils.h:14: error: expected initializer before ‘<’ token
utils.h:15: error: expected initializer before ‘<’ token
utils.h:16: error: expected initializer before ‘<’ token
utils.h:17: error: expected initializer before ‘<’ token
utils.h:18: error: expected initializer before ‘<’ token
main1.cpp: In function ‘int main()’:
main1.cpp:4: error: ‘vi’ was not declared in this scope
main1.cpp:4: error: expected `;’ before ‘v’
What is wrong with this code? The utils.h used to work fine some time back when I did not have the #ifndefs in it.
Those types (
pair,stack,queue,vector, etc.) are in thestdnamespace. You either need to addusing namespace std;at the top of your file (generally after all of the standard library includes) or fully qualify the type names by addingstd::in front of them.Generally, it’s better practice to fully qualify the type names than to use
using namespaceto avoid potential collisions between names and to make your code cleaner. You should never useusing namespace stdin header files.(Along the lines of clean code, you should consider using better, longer names for your types;
ii,vii, andvviiare atrocious type names).