I am just playing around with std::array. Want to pass an array of integers to a function which can easily be done using
void arrayByValue(array<int, 5> arr);
The above can be invoked like
array<int, 5> aInt = {100,92,-1,122,112};
arrayByValue(aInt);
This works perfectly fine as long as it is in same .cpp file.
I am The issue is how to put this in a .h file, implement the method in .cpp file and then invoke it in another file assume main. I receive the error saying
'array': undeclared identifier
type 'int' expected
Please suggest what could be wrong. Please note this works perfect if I declare the function in the same file from where I am invoking it
You need to add at the top of you .h file:
#include <array>
also use std::array - using namespace std; is a bad habit
Apologies for the inconvenience. I forgot to use
using namespace std;
in the .h file. Using the above line or using std::array
resolved the issue
Related
I'm primarily an R programmer, and I'm using Rcpp to run a hash map implementation by Malte Skarupke called ska::flat_hash_map on Windows 10 through RStudio (Microsoft OpenR). The C++ compiler is g++ run with c11 flags.
With no changes to his .hpp file, I am unable to get it running, as it produces the error
Line 276 no type named 'hash_policy' in 'struct std::hash<char>'
The offending line in flat_hash_map.hpp is
template<typename T>
struct HashPolicySelector<T, void_t<typename T::hash_policy>>
{
typedef typename T::hash_policy type;
};
I've found a few benchmark libraries on github that seem to include the library with no problems, and access it like std::unordered_map, so I don't understand why I am having problems getting it to run.
I've also tried providing different types instead of char, sticking to the ones that std::hash should be able to handle automatically, such as int, and std::string.
My source file is really simple, as I'm literally just trying to get a hash map created, for example, my last run was using this:
#include <Rcpp.h>
#include "flat_hash_map.hpp"
using namespace Rcpp;
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::export]]
void run_test()
{
ska::flat_hash_map<char,char> test_map;
}
I'm hoping someone with more C++ experience than myself could shed some light on the problem, or try running the library themselves if the issue is reproducible.
Thanks for the help!
This is also my first post on StackOverflow, please let me know if there is something I can do to improve my question.
GCC < 5.0 would not trigger substitution failure on unused parameters within an alias template. This case was actually underspecified in the standard, eventually solved by CWG Issue 1558.
As a workaround, you should manually replace line 266:
template<typename...> using void_t = void;
with:
template <typename...>
struct voider { using type = void; };
template <typename... Ts>
using void_t = typename voider<Ts...>::type;
This forces the usage of template parameters of the alias template, allowing the compiler to SFINAE-out types that don't declare hash_policy.
So I wrote the following code first and was getting a compile error. After reading this answer :
static array class variable "multiple definition" C++
I modified my code and moved the static variable definition to a cpp file and it executes fine, but I'm unable to understand that when I have used pre-processor guards, why is it showing multiple definition error ?
#ifndef GRAPH_H
#define GRAPH_H
#include<iostream>
#include<vector>
using namespace std;
struct node{
int element=0;
static vector<bool> check;
node(){
if(check.size()<element+1)
check.resize(element+1);
}
};
vector<bool> node::check;
#endif
So, this is a common mistake of misunderstanding how the header guards work.
Header guards save multiple declarations for one compilation unit, but not from errors during linking. One compilation unit implies a single cpp file.
E.g. apple.cpp includes apple.h and grapes.h, and apple.h in turn includes grapes.h. Then header guards will prevent the inclusion of the file grapes.h again during compilation.
But when the process of compilation is over, and the linker is doing its job of linking the files together, then in that case it sees two memory locations for the same static variables, since the header file was included in a separate translation unit, say apple2.cpp to which its trying to link, thus causing the multiple definition error.
The only way to resolve it is to move the definition of the static variable to a cpp file.
I am working on a project with the library ADOL-C (for automatic differentiation) using gcc. Right now I am trying to recompile the library to use the parallelization features however the make process does not working apparently due to some preprocessor stuff.
I think the problematic line is :
#define ADOLC_OPENMP_THREAD_NUMBER int ADOLC_threadNumber
However I could not find what it means. Does it make a link between two variables? Also ADOLC_threadNumber has not been declared before...
The preprocessor doesn't even know what a variable is. All that #define does is define a short(long?)hand for declaring a variable. I.e., if you type
ADOLC_OPENMP_THREAD_NUMBER;
It becomes
int ADOLC_threadNumber;
It's just a text substitution. Everywhere in code where ADOLC_OPENMP_THREAD_NUMBER appears, it's substituted by int ADOLC_threadNumber.
As far as I see it, the line with the define itself is not problematic, but maybe the subsequent appearance of ADOLC_OPENMP_THREAD_NUMBER. However, to check this, we need to know more about the context.
#define is a directive used often in .h files,
it creates a macro, which is the association of an identifier or parameterized identifier with a token string.
After the macro is defined, the compiler can substitute the token string for each occurrence of the identifier in the source file.
#define may be associated with #ifndef directive to avoid to delare the identifier more than once :
#ifndef ADOLC_OPENMP_THREAD_NUMBER
#define ADOLC_OPENMP_THREAD_NUMBER int ADOLC_threadNumber
#endif
I am trying to use the computeMeanAndCovarianceMatrix function from PCL in ROS. I researched online and saw that the function is defined in a centroid.hpp file, so I put #include <pcl_ros/centroid.h> at the top of my file but I still get a No such file or directory #include <pcl_ros/centroid.h>. Can someone help me with what include statement I have to put up so the function works?
Figured it out. You need to add #include <pcl/common/centroid.h>
I'm using mac os 10.9, I have a C++ program that uses freeglut library. When I try to make the project. It gives an error which I don't know if it's my fault or not. This is the message:
In file included from /usr/X11/include/GL/freeglut.h:18:
/usr/X11/include/GL/freeglut_ext.h:177:27: error: functions that differ only in their return type cannot be overloaded
FGAPI GLUTproc FGAPIENTRY glutGetProcAddress( const char *procName );
More information: I used CMake (version 2.8.12) to generate the Makefile, and installed the latest version of Xcode and XQuartz.
Any help is appreciated. Thank you.
In glut.h and freeglut_ext.h files:
In glut.h:
#if (GLUT_API_VERSION >= 5)
extern void * APIENTRY glutGetProcAddress(const char *procName) OPENGL_DEPRECATED(10_3, 10_9);
#endif
In freeglut_ext.h:
/*
* Extension functions, see freeglut_ext.c
*/
typedef void (*GLUTproc)();
FGAPI GLUTproc FGAPIENTRY glutGetProcAddress( const char *procName );
One of the declarations returns a function type GLUTproc (specifying a function that takes no arguments), and the other declaration returns a pointer (void*). Both functions take the same arguments (a single const char*). What the compiler says is true.
You're only seeing a complaint about "overloading" because it's C++. In C++, if a compiler thinks it's seen two different functions with the same name then each one needs to have different arguments (e.g. a different number of arguments, or distinct types).
In this case, I doubt the functions are meant to be different; they're meant to be the same, and at some point the API evolved and changed the declaration.
You need to find some way to prevent the compiler from seeing both declarations at the same time (perhaps by setting GLUT_API_VERSION). If you have to, you can #include just one of the files and see if you really need the other file (and if you did, you may have to manually declare some things to avoid a 2nd #include).