BOOST_PHOENIX_ADAPT_FUNCTION causes invalid template error - c++

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.

Related

'list' was not declared in this scope

I am new to c++, and I am trying to get a basic program to initialize a list of short unsigned integers. I am compiling and running using scygwin and g++.
Below is the code in the .cpp file:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <typeinfo>
using namespace std;
int main (int argc, char* argv[]) {
list<int> events;
return 0;
}
which I run by typing the following command into cygwin terminal:
$ g++ -o test.out test.cpp
However, I get the following compilation errors:
test.cpp: In function ‘int main(int, char**)’: test.cpp:16:1: error:
‘list’ was not declared in this scope list events;
^
test.cpp:16:6: error: expected primary-expression before ‘int’
list events;
^
I am confused about why list is not in the scope, since I am using namespace std? I found a similar question asked about this on a c++ forum, but my problem would be resolved with that. Anyone know what the problem is here?
-Paul
using namespace std; doesn't add any functionality to your code. It just means you don't have to type std:: when referencing things in the std namespace, like std::list.
To actually include the code base for std::list into your program, you need to add:
#include <list>
When in doubt about this kind of thing, doing a google search for cpp reference list will turn up a page like this where you can see: Defined in header <list> at the top.
Here's another question about using namespace std; that may prove useful and why you shouldn't use it. I'll add a little bit to perhaps explain namespaces.
It is common in C++ programs to organize functions into classes and namespaces. Imagine you wrote your own list class to handle certain scenarios. In order to prevent naming conflicts you would put it in a different namespace than std.
namespace MyApp {
class list;
void sort(list&);
}
For the majority of a large code base you might still prefer to use std::list but you need MyApp::list for some things. Using namespaces you can cluster your code and prevent naming conflicts for similar functionality.
Summary
using namespace std; makes it so that if you reference a function or class not in the global namespace it looks for it in the std namespace.
#include <list> actually inserts prototypes (information about how to access the code) in your source file during the preprocessor stage.

Compile error:... was not declared in this scope

I'm a neophyte of C++ programmation. I have to implement a program witch calculates the pseudoinverse of a matrix. As the Eigen tutorial suggests, I have written a code like this:
#include <stdio.h>
#include <stdlib.h>
#include <Core>
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/SVD>
#include <Eigen/Eigen>
using namespace Eigen;
using namespace std;
void pinv(MatrixXf& pinvmat)
{
ei_assert(m_isInitialized && "SVD is not initialized.");
double pinvtoler=1.e-6; // choose tolerance
SingularValuesType m_sigma_inv=m_sigma;
for ( long i=0; i<m_workMatrix.cols(); ++i) {
if ( m_sigma(i) > pinvtoler )
m_sigma_inv(i)=1.0/m_sigma(i);
else m_sigma_inv(i)=0;
}
pinvmat = (m_matV*m_sigma_inv.asDiagonal()*m_matU.transpose());
}
int main()
{
MatrixXf A(3,2);
A<<1,2,3,4,5,6;
pinv(A);
cout << "pinv =" << endl << A << endl;
return 0;
}
If I try to compile it I'll get the errors:
tut_eigen/pinv.cpp: In function ‘void pinv(Eigen::MatrixXf&)’:
tut_eigen/pinv.cpp:18:14: error: ‘m_isInitialized’ was not declared in this scope
tut_eigen/pinv.cpp:18:58: error: ‘ei_assert’ was not declared in this scope
tut_eigen/pinv.cpp:20:4: error: ‘SingularValuesType’ was not declared in this scope
tut_eigen/pinv.cpp:20:23: error: expected ‘;’ before ‘m_sigma_inv’
tut_eigen/pinv.cpp:21:22: error: ‘m_workMatrix’ was not declared in this scope
tut_eigen/pinv.cpp:22:19: error: ‘m_sigma’ was not declared in this scope
tut_eigen/pinv.cpp:23:19: error: ‘m_sigma_inv’ was not declared in this scope
tut_eigen/pinv.cpp:24:22: error: ‘m_sigma_inv’ was not declared in this scope
tut_eigen/pinv.cpp:26:15: error: ‘m_matV’ was not declared in this scope
tut_eigen/pinv.cpp:26:22: error: ‘m_sigma_inv’ was not declared in this scope
tut_eigen/pinv.cpp:26:47: error: ‘m_matU’ was not declared in this scope
Why?? They are not declared in SVD file?
I suspect you talk about this "tutorial" which isn't so much a tutorial but an FAQ assuming you know a bit about the library already (it would be helpful if you link to your sources of information, BTW).
What this says is that you can add the pinv() method to the SVD "from the outside". I assume they mean that you can derive from SVD and provide the pinv() method in your derived class. Just typing the function somewhere doesn't give the compiler the necessary context to determine where referenced names are located.

defining a hash set with a compare function

I am trying to do a very simple task of defining a new type of hash set which has a compare function as well.
using namespace std;
#include <iostream>
#include <ext/hash_set>
#include <hash_set>
#include <functional>
#include <hash_compare>
typedef __gnu_cxx::hash_set<int, hash_compare<int, less<int> > > hashcomp;
int main(int argc, char * const argv[]) {
}
Error: hash_compare is not defined (line 7)
Error: expected unqualified-id before ">" token (line 7)
Error: template argument 2 is invalid. (line 7)
Seeing the error, I guess you haven't included <functional> as you're using std::less<int> in your code. I'm assuming that less<int> in your code is actually std::less<int>.
EDIT:
The error message clearly says
Error: hash_compare is not defined.
What else do you want? Include the header file which defines hash_compare.
Your requirements are contradictory. A hash table has a random order, by definition.

"nice" keyword in c++?

So I was doing some simple C++ exercises and I noticed an interesting feat. Boiled down to bare metal one could try out compiling the following code:
class nice
{
public:
nice() {}
};
int main()
{
nice n;
return 0;
};
The result is a compilation error that goes something like this:
<file>.cpp: In function ‘int main()’:
<file>.cpp:11: error: expected `;' before ‘n’
<file>.cpp:11: warning: statement is a reference, not call, to function ‘nice’
<file>.cpp:11: warning: statement has no effect
And this was using regular g++ on Max OS X, some of my friends have tried in on Ubuntu as well, yielding the same result.
The feat seems to lie in the word "nice", because refactoring it allows us to compile. Now, I can't find the "nice" in the keyword listings for C++ or C, so I was wondering if anyone here had an idea?
Also, putting
class nice n;
instead of
nice n;
fixes the problem.
P.S. I'm a relative C++ newbie, and come from the ActionScript/.NET/Java/Python world.
Update:
Right, my bad, I also had an
#include <iostream>
at the top, which seems to be the root of the problem, because without it everything works just fine.
Maybe the problem is somehow caused by function nice in libc. It is similar to trying to name your class printf.
using namespace std, by any chance?
Edit:
The standard says that standard headers define all their symbols in namespace std (see 17.4.1.2.4).
A footnote, however, also says that the <.h> variants dump their names into the global namespace - but of course no one should be using these ;)
It is a namespace problem but not with namespace std. The header <iostream> is pulling in <unistd.h>
If you try
class nice
{
public:
nice() {};
};
int main(int argc, char *argv[])
{
nice n;
return 0;
}
there is no problem.
Simply add
#include <unistd.h>
and you will get the "expected ‘;’ before ‘n’" error. Namespace std does not enter the picture.
So the solution is the same as before - put class nice in its own namespace and it will not clash with the global ::nice().
Try this version:
#include <iostream>
namespace test
{
class nice
{
public:
nice() {}
};
};
using namespace std;
int main()
{
test::nice n;
cout << "well I think this works." << endl;
return 0;
}
In this case I've defined my own namespace test. Doing so, I can use whatever class names I like, including functions already defined like printf. The only things I can't re-use are reserved words like int or namespace.
Note: if you say:
using namespace test;
As well and refer to nice alone, you'll get this error:
nice.cpp: In function ‘int main()’:
nice.cpp:18: error: reference to ‘nice’ is ambiguous
/usr/include/unistd.h:593: error: candidates are: int nice(int)
nice.cpp:7: error: class test::nice
Which I think explains nicely what's going on - nice now exists in two namespaces and the compiler can't work out which one you mean.
It works fine for me. Did you try the exact code you posted?
extern "C"
{
#include <unistd.h>
}

include file error in 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::