I don't understand how the digamma function of boost can be used inside a program. Any example, is appreciated. I included boost
#include <boost/math/special_functions/digamma.hpp>
but the function call digamma(x), where x is a double gives the following error:
error: there are no arguments to ‘digamma’ that depend on a template
parameter, so a declaration of ‘digamma’ must be available
[-fpermissive]
Here's an example:
http://cpp.sh/7bdu
#include <boost/math/special_functions/digamma.hpp>
#include <iostream>
int main() {
std::cout << boost::math::digamma(3.14) << "\n";
}
edit: The question was edited with an error message. The error message means that the compiler didn't find a definition of digamma, because you didn't include the namespace bit boost::math::.
Related
Following https://stackoverflow.com/a/9424211/3368959 I am trying to compare three numbers:
#include <iostream>
int main() {
std::cout << std::min({2,5,1}) << std::endl;
return 0;
}
But the compiler gives me the error:
error: no matching function for call to ‘min(<brace-enclosed initializer list>)’
However, the code compiles just fine when using
std::min(std::min(2,5),1)
But the first way should work with the c++11 standard. What could I be doing wrong?
As #BoBTFish suggested:
In order to use template <class T> T min (initializer_list<T> il) one needs to include <algorithm> as is mentioned here.
I already asked a question about this before but with reference to the following code again
#include <iostream>
using std::cout;
using std::endl;
#include <dlfcn.h>
int rand() throw() {
// get the original rand() function
static auto original_rand = (decltype(&rand)) dlsym(RTLD_NEXT, "rand");
cout << "Call made to rand()" << endl;
return original_rand();
}
Why does compiling this result in the following error
error: exception specification in declaration does not match previous declaration
Why would this happen? How does the linker know that this is just another rand() function I have declared myself but rather an existing version of rand()?
One of your header files (<iostream> in my case) silently includes <stdlib.h>, which contains the rand() function. The latter does not have any exception specification. In your code you try to overload rand(), but the signature is the same as the one from <stdlib.h> except the exception specifier (which is not part of the function type, so cannot overload on it alone), hence you get a compile-time error (not a linker error, as you mentioned in the question).
If you wonder why the function rand() silently included is also visible in the global namespace (not visible only as std::rand as you may expect), that's because often the C library functions are imported into the standard C++ header files via a direct
// <new_cpp_header>
#include <old_c_header.h>
followed by
namespace std
{
using old_function_from_old_c_header;
}
so the function ends up in both global namespace as well as in namespace std;. This is not a very good practice IMO, but that's how most compilers do it nowadays.
For example, the <cstdio> file has:
// <cstdio>
#include <stdio.h>
namespace std
{
using printf;
// ...
}
so both printf and std::printf are visible, even though you include only <cstdio> (and not <stdio.h>).
I am doing everything correctly as far as I can tell and I have gotten the error message:
error: 'unordered_map' does not name a type
error: 'mymap' does not name a type
In my code, I have:
#include <unordered_map>
using namespace std;
//global variable
unordered_map<string,int> mymap;
mymap.reserve(7000);
void main {
return;
}
I don't see what can be missing here....
EDIT: when I update my declaration to
std::tr1::unordered_map<string,int> mymap;
I an able to eliminate the first error, but when I try to reserve, I still get the second error message.
EDIT2: As pointed out below, reserve must go into main and I need to compile with flag
-std=c++0x
However, there still appear to be errors related to unordered_map, namely:
error: 'class std::tr1::unordered_map<std::basic_string<char>, int>' has no member named 'reserve'
Compile with g++ -std=c++11 (my gcc version is gcc 4.7.2) AND
#include <unordered_map>
#include <string>
using namespace std;
//global variable
unordered_map<string,int> mymap;
int main() {
mymap.reserve(7000); // <-- try putting it here
return 0;
}
If you want to support <unordered_map> for versions older than c++11 use
#include<tr1/unordered_map> and declare your maps in the form :- std::tr1::unordered_map<type1, type2> mymap
which will use the technical report 1 extension for backward compatibility.
You can't execute arbitrary expressions at global scope, so you should put
mymap.reserve(7000);
inside main.
This is also true for other STL containers like map and vector.
The Boost Phoenix documentation here indicates that I can create my own (lambda) arguments instead of _1/arg1, _2,arg2 etc. So, starting with code like this:
#include <iostream>
#include <boost/phoenix.hpp>
int main(int argc, char *argv[]) {
std::cout << (boost::phoenix::arg_names::_1)(17) << std::endl;
return 0;
}
...which outputs 17, I'm aiming for a first step of using myarg1. The documentation recommends that I first #include <boost/spirit/home/phoenix/core/argument.hpp>. Doing this (G++ 4.7.2), however, results in a series of compilation errors starting with: error: redefinition of ‘struct boost::phoenix::detail::error_expecting_arguments’.
Without the arguments.hpp file included, I notice that I can declare variables of type boost::phoenix::argument<0>. When I try to declare a variable of the crucial type, boost::phoenix::actor< boost::phoenix::argument<0> >, however, I again receive numerous errors; this time starting with: error: no type named ‘proto_base_expr’. How can I define my own lambda arguments?
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>
}