include file error in C++ - c++

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 the std namespace. You either need to add using namespace std; at the top of your file (generally after all of the standard library includes) or fully qualify the type names by adding std:: in front of them.
Generally, it's better practice to fully qualify the type names than to use using namespace to avoid potential collisions between names and to make your code cleaner. You should never use using namespace std in header files.
(Along the lines of clean code, you should consider using better, longer names for your types; ii, vii, and vvii are atrocious type names).

vector and the like are contained in the namespace std::. Do not use using namespace std; in a header file. Otherwise everyone that includes it gets all of std:: whether intended or not.
On a side note, if this is a utility header intended to be included in other files, you might wrap up those types and #define's in a namespace. Note #define's don't respect namespaces, so you'd prefix them instead:
namespace utility
{
// ...
typedef std::queue<int> qi;
// most would recommend this be in CAPS
#define utility_tr(c,i) for(typeof((c).begin()) i = (c).begin() ; i!=(c).end() ; ++i )
// ...
}

Before your typedefs, you should have using namespace std;
Also, you may wish to use a less common name than UTILS_H.

you should write
using namespace std;
before line
typedef pair<int,int> ii;
or fully qualify types of stl with std::

Related

c++17 error message: "reference to non-static member function must be called" when using a macro on a vector

I'm learning c++ and was playing around with macros. I tried defining push_back as pub, and it gave me this error:
error: reference to non-static member function must be called
vect.pub(1);
Here's my code:
#include <vector>
using namespace std;
typedef vector<int> vi;
#define pub push_back;
int main(){
vi vect;
vect.pub(1);
}
When I didn't use the #define and just wrote push_back, there was no error messages.
What exactly changed when I used the macro?
#define pub push_back;
//...
vect.pub(1);
This expands to the following, which is invalid syntax due to the extra ;.
vect.push_back;(1);
So drop the ; and #define pub push_back.
You should not put ';' for macro.
#include <vector>
using namespace std;
typedef vector<int> vi;
#define pub push_back
int main(){
vi vect;
vect.pub(1);
}
I'm learning c++ and was playing around with macros.
Stop. push_back is at most 6 extra keystrokes. Code is meant to be read by humans. You can't find pub in documentation, but you can find push_back.
Similarly using namespace std; is a terrible habit. There are loads of names that you don't realise you've just imported into the global namespace there.

BOOST_PHOENIX_ADAPT_FUNCTION causes invalid template error

I am trying to create a lazy function from a template function following the Boost::phoenix documentation. The code looks like this
#include <iostream>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/function.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/statement.hpp>
#include <boost/phoenix/object.hpp>
#include <boost/phoenix/function/adapt_function.hpp>
#include <boost/phoenix/core/argument.hpp>
using namespace boost;
using namespace boost::phoenix;
namespace demo
{
bool func(double a,double b)
{
return bool(a > b);
}
}
BOOST_PHOENIX_ADAPT_FUNCTION( bool , func , demo::func , 2)
int main(int argc,char **argv)
{
namespace pl = boost::phoenix::placeholders;
auto comperator = func(pl::arg1,pl::arg2);
std::cout<<comperator(1.2,12.4)<<std::endl;
std::cout<<comperator(0.5,0.1)<<std::endl;
}
This is virtually one of the examples from the BOOST documentation. Storing this file as mk_lazy1.cpp and try to compile gives
$ g++ -omk_lazy1 mk_lazy1.cpp
mk_lazy1.cpp:26:1: error: template argument 1 is invalid
mk_lazy1.cpp:26:1: error: expected identifier before ‘::’ token
mk_lazy1.cpp:26:1: error: expected initializer before ‘const’
mk_lazy1.cpp: In function ‘int main(int, char**)’:
mk_lazy1.cpp:31:10: error: ‘comperator’ does not name a type
mk_lazy1.cpp:32:35: error: ‘comperator’ was not declared in this scope
I use gcc-4.7 on a Debian testing system. An honestly I am a bit lost as I have absolutely no idea what is wrong here (as I said, this is virtually a word by word copy of one of the examples provided by the Boost documentation).
Does anyone have a good idea?
Remove using namespaces and all will work fine.
Or write using namespaces AFTER adapt macro and all will work fine too.
Or put macro into unnamed namespace.

C++: 'set' and 'vector' "undeclared despite #include statements

I am using Netbeans 7.1 on Ubuntu 11.04.
The following call
set< Triangle > V;
gives the error message
error: ‘set’ was not declared in this scope
and the following call
vector< Triangle > ans;
gives the error message
error: ‘vector’ was not declared in this scope
This despite my having
#include <vector>
#include <set>
#include <map>
at the beginning of the C++ file.
At help resolving this would be greatly appreciated.
Peter.
Vectors Sets and map are part of the c++ Standard Library so you need to call vector/set/map with
std::vector< Triangle > ans;
or add
using namespace std;
after the include statements.
you forgot about namespace std :
std::set< Triangle > V;
std::vector< Triangle > V;
They live in the std namespace. So, either fully quality the types (std::vector) or use a using statement (using namespace std;).
The latter option pollutes the global namespace. Never do that in a header file (otherwise the entire namespace is imported when you include the header) and only do it in your implementation file if you know that it isn't going to cause any collisions.
#include <vector>
int main(...) {
vector v; // no worky
std::vector v; // ok!
}

Compile errors when trying to use list in C++

I'm trying to use list in c++, but I get the following error:
1>error C2143: syntax error : missing ';' before '<'
1>error C4430: missing type specifier int assumed. Note: C++ does not support default-int
1>error C2238: unexpected token(s) preceding ';'
With the following code:
#pragma once
#include "Includes.h"
class Polygon
{
public:
Polygon(void);
~Polygon(void);
void addVertice(hgeVector v);
void renderPolygon();
list<hgeVector> vertices;
};
Includes.h:
#ifndef INCLUDES
#define INCLUDES
#define safe_delete(d) if(d) { delete d; d=0; }
#define PI 3.14159
#include <stdio.h>
#include <list>
#include "\include\hge.h"
#include "\include\hgesprite.h"
#include "\include\hgefont.h"
#include "\include\hgeparticle.h"
#include "\include\hgerect.h"
#include "Car.h"
#include "HelperFunctions.h"
#include "config.h"
#include "Polygon.h"
using namespace std;
#endif
Just some general comments...
#define PI 3.14159
Please use M_PI in math.h, which is 3.141592653589793238462643.
#include "\include\hge.h"
#include "\include\hgesprite.h"
#include "\include\hgefont.h"
#include "\include\hgeparticle.h"
#include "\include\hgerect.h"
You should use forward slashes / here, and remove the leading \ before the include.
using namespace std;
Avoid this in header file. This will pollute all other users' global namespace. (Therefore, you should use std::list<hgeVector> vertices; in Polygon.h.)
The issue could be that the line list<hgeVector> vertices is being processed before using namespace std;, and so your compiler does not know what a list (without the std:: namespace qualifier) is. It's not clear to me in exactly what order these statements get processed since your two files include each other, and I don't know precisely how the non-standard #pragma once will handle this.
In any case, try qualifying list<hgeVector> as std::list<hgeVector>
Edit: Assuming #pragma once works just like include guards, then this problem will occur if some other file inlcudes includes.h, but not if some other file includes Polygon.h. If another file includes includes.h, what happens is that includes.h reaches #include <Polygon.h>, and the compiler starts processing Polygon.h. But then when #include <includes.h> is reached inside Polygon.h, nothing is effectively included since the INCLUDES guard is already defined, so you don't get the using namespace std; line before the compiler continues processing the rest of Polygons.h.
In general, try to avoid circular inclusion, and prefer forward-declarations.
I think you have circular "includes". You are including Includes.h in Polygon.h and including Polygon.h in Includes.h.
class template need a full type declaration to instantiate itself. Make sure you have included the header file where hgeVector is declared.
BTW, you have 'using namespace std‘ in your header - this is not a good practice. It will introduce unnecessary names to the current namespace.
Make sure hgeVector is defined.
You may have redefined list somewhere. Try using std::list.
Try something very simple like std::list<int>.
The answer (as Tyler McHenry pointed out) was circular inclusion!
After sorting out my includes I ended up with a compiled code like this (even without std:: infront of list:
#pragma once
#include <list>
#include "D:\Programmering\haffes\hge181\include\hge.h"
#include "D:\Programmering\haffes\hge181\include\hgevector.h"
using namespace std;
using namespace std;
class MyPolygon
{
public:
MyPolygon(void);
~MyPolygon(void);
void addVertice(hgeVector v);
void renderPolygon();
void setHotSpot(hgeVector v);
void translate(hgeVector v);
private:
list<hgeVector> vertices;
hgeVector hotSpot;
bool hotSpotUndef;
};
Thanks a bunch for all the fast and good answers!

"Expected constructor, destructor, or type conversion before '<' token"

I'm running into a syntax/parsing error, but I can't seem to locate it.
DataReader.h:11: error: expected constructor, destructor, or type conversion before '<' token
Here is DataReader.h:
#include <fstream>
#include <iostream>
#include <vector>
#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif
vector<Data*> DataReader(); // This is line 11, where the error is..
And this is the .cpp file:
#include "DataReader.h"
using namespace std;
vector<Data*> DataReader()
{
.....
}
I skipped the content of DataReader() because I think it's irrelevant, but I can post it if needed.
Thanks for any input/suggestions.
In your header file, you need to explicitly use std::vector rather than just vector.
Also, I'm guessing that "Data.h" contains statements of the form:
#ifndef DATA_H
#define DATA_H
...
#endif
That's fine, but you should not use these include guards across #include "Data.h" as well, only within the file itself.
In your header file you need to use std::vector and not plain vector in the declaration of the function DataReader.
The standard include <vector> causes the vector class template to be defined in the std namespace and the declaration in your header file happens before any using namespace std; or using std::vector;.
I think in your header you probably need to write std::vector<Data*> DataReader(); as the using namespace std; is not in scope.
Use std:vector and not vector before Datareader.