I have the latest OpenMPI on linux which I compiled with the Intel compiler suite. I am trying to compile an OpenMPI application. My particular application uses RInside and RcppEigen. If I comment out the openMPI parts of my code, the compile string is:
icpc -I/usr/share/R/include -I/usr/lib/R/site-library/Rcpp/include -I/usr/local/lib/R/site-library/RInside/include -O3 -pipe -g -Wall -I/usr/local/lib/R/site-library/RcppEigen/include sjb_simple_smle_with_Rinside.cpp -L/usr/lib/R/lib -lR -lblas -llapack -L/usr/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/lib/R/site-library/Rcpp/lib -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o sjb_simple_smle_with_Rinside
Thus, I tried to compile with mpic++ using:
mpic++ -I/usr/share/R/include -I/usr/lib/R/site-library/Rcpp/include
-I/usr/local/lib/R/site-library/RInside/include -O3 -pipe -g -Wall -I/usr/local/lib/R/site-library/RcppEigen/include sjb_simple_smle_with_Rinside.cpp -L/usr/lib/R/lib -lR -lblas
-llapack -L/usr/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/lib/R/site-library/Rcpp/lib -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o sjb_simple_smle_with_Rinside
If I uncomment
#include "mpi.h"
and try to compile, I get the errors below. Is there any trick to compiling OpenMPI applications with the Intel compilers? The full source is listed below:
stevejb#ursamajor:~/Projects/big_data_sim_mle/simple_smle/R_inside_version$ mpic++ -I/usr/share/R/include -I/usr/lib/R/site-library/Rcpp/include -I/usr/local/lib/R/site-library/RInside/include -O3 -pipe -g -Wall -I/usr/local/lib/R/site-library/RcppEigen/include sjb_simple_smle_with_Rinside.cpp -L/usr/lib/R/lib -lR -lblas -llapack -L/usr/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/lib/R/site-library/Rcpp/lib -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o sjb_simple_smle_with_Rinside -shared-intel -I/usr/local/include -pthread -L/usr/local/lib -lmpi_cxx -lmpi -ldl -lm -Wl,--export-dynamic -lrt -lnsl -lutil
/usr/local/include/openmpi/ompi/mpi/cxx/datatype.h(142): error: expected a type specifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/datatype.h(142): error: expected a ")"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/datatype.h(142): error: expected an identifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/datatype.h(142): error: "virtual" is not allowed
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/datatype.h(142): error: expected a ";"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/op.h(48): error: expected a type specifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/op.h(48): error: expected a ")"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/op.h(48): error: expected an identifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/op.h(48): error: "virtual" is not allowed
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/op.h(48): error: expected a ";"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/request.h(96): error: expected a type specifier
virtual void Free(void);
^
/usr/local/include/openmpi/ompi/mpi/cxx/request.h(96): error: expected a ")"
virtual void Free(void);
^
/usr/local/include/openmpi/ompi/mpi/cxx/request.h(96): error: expected an identifier
virtual void Free(void);
^
/usr/local/include/openmpi/ompi/mpi/cxx/request.h(96): error: "virtual" is not allowed
virtual void Free(void);
^
/usr/local/include/openmpi/ompi/mpi/cxx/request.h(96): error: expected a ";"
virtual void Free(void);
^
/usr/local/include/openmpi/ompi/mpi/cxx/group.h(111): error: expected a type specifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/group.h(111): error: expected a ")"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/group.h(111): error: expected an identifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/group.h(111): error: "virtual" is not allowed
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/group.h(111): error: expected a ";"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/comm.h(264): error: expected a type specifier
virtual void Free(void);
^
/usr/local/include/openmpi/ompi/mpi/cxx/comm.h(264): error: expected a ")"
virtual void Free(void);
^
/usr/local/include/openmpi/ompi/mpi/cxx/comm.h(264): error: expected an identifier
virtual void Free(void);
^
/usr/local/include/openmpi/ompi/mpi/cxx/comm.h(264): error: "virtual" is not allowed
virtual void Free(void);
^
/usr/local/include/openmpi/ompi/mpi/cxx/comm.h(264): error: expected a ";"
virtual void Free(void);
^
/usr/local/include/openmpi/ompi/mpi/cxx/win.h(118): error: expected a type specifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/win.h(118): error: expected a ")"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/win.h(118): error: expected an identifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/win.h(118): error: "virtual" is not allowed
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/win.h(118): error: expected a ";"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/errhandler.h(59): error: expected a type specifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/errhandler.h(59): error: expected a ")"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/errhandler.h(59): error: expected an identifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/errhandler.h(59): error: "virtual" is not allowed
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/errhandler.h(59): error: expected a ";"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/info.h(84): error: expected a type specifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/info.h(84): error: expected a ")"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/info.h(84): error: expected an identifier
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/info.h(84): error: "virtual" is not allowed
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/info.h(84): error: expected a ";"
virtual void Free();
^
/usr/local/include/openmpi/ompi/mpi/cxx/request_inln.h(39): error: expected an identifier
MPI::Request::Free()
^
/usr/local/include/openmpi/ompi/mpi/cxx/request_inln.h(38): error: inline specifier allowed on function declarations only
inline void
^
/usr/local/include/openmpi/ompi/mpi/cxx/request_inln.h(39): error: incomplete type is not allowed
MPI::Request::Free()
^
/usr/local/include/openmpi/ompi/mpi/cxx/request_inln.h(39): error: a nonstatic member reference must be relative to a specific object
MPI::Request::Free()
^
/usr/local/include/openmpi/ompi/mpi/cxx/request_inln.h(39): error: expected an expression
MPI::Request::Free()
^
/usr/local/include/openmpi/ompi/mpi/cxx/request_inln.h(39): error: expected a ")"
MPI::Request::Free()
^
/usr/local/include/openmpi/ompi/mpi/cxx/request_inln.h(40): error: expected a ";"
{
^
/usr/local/include/openmpi/ompi/mpi/cxx/op_inln.h(123): error: identifier "ompi_op_set_cxx_callback" is undefined
ompi_op_set_cxx_callback(mpi_op, (MPI_User_function*) func);
^
/usr/local/include/openmpi/ompi/mpi/cxx/op_inln.h(128): error: expected an identifier
MPI::Op::Free()
^
/usr/local/include/openmpi/ompi/mpi/cxx/op_inln.h(127): error: inline specifier allowed on function declarations only
inline void
^
/usr/local/include/openmpi/ompi/mpi/cxx/op_inln.h(128): error: incomplete type is not allowed
MPI::Op::Free()
^
/usr/local/include/openmpi/ompi/mpi/cxx/op_inln.h(128): error: a nonstatic member reference must be relative to a specific object
MPI::Op::Free()
^
/usr/local/include/openmpi/ompi/mpi/cxx/op_inln.h(128): error: expected an expression
MPI::Op::Free()
^
/usr/local/include/openmpi/ompi/mpi/cxx/op_inln.h(128): error: expected a ")"
MPI::Op::Free()
^
/usr/local/include/openmpi/ompi/mpi/cxx/op_inln.h(129): error: expected a ";"
{
^
sjb_simple_smle_with_Rinside.cpp(35): warning #12: parsing restarts here after previous syntax error
using namespace Rcpp;
^
sjb_simple_smle_with_Rinside.cpp(77): error: identifier "as" is undefined
const Map<MatrixXd> Xmat(as<Map<MatrixXd> >(Xmat_sexp));
^
sjb_simple_smle_with_Rinside.cpp(77): error: type name is not allowed
const Map<MatrixXd> Xmat(as<Map<MatrixXd> >(Xmat_sexp));
^
sjb_simple_smle_with_Rinside.cpp(80): error: type name is not allowed
const Map<MatrixXd> Ymat(as<Map<MatrixXd> >(Ymat_sexp));
^
sjb_simple_smle_with_Rinside.cpp(87): error: type name is not allowed
const Map<MatrixXd> ua(as<Map<MatrixXd> >(ua_sexp));
^
sjb_simple_smle_with_Rinside.cpp(89): error: type name is not allowed
const Map<MatrixXd> ub(as<Map<MatrixXd> >(ub_sexp));
^
sjb_simple_smle_with_Rinside.cpp(98): error: type name is not allowed
const Map<VectorXd> start_vector(as<Map<VectorXd> >(start_sexp));
^
compilation aborted for sjb_simple_smle_with_Rinside.cpp (code 2)
FULL SOURCE CODE:
// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8; -*-
//
// SJB - first shot at RInside and Eigen combined
//
// Copyright (C) 2012 Stephen J. Barr
//
// GPL'ed
#include <iostream>
#include <sstream>
#include <iomanip>
#include <fstream>
#include "mkl.h"
#include "math.h"
#include <vector>
#include <cmath>
#include <string>
#include <cstdlib>
#include <fcntl.h>
#include <sys/stat.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <map>
#include <utility>
#include <RcppEigen.h>
#include <Rcpp.h>
#include <RInside.h> // for the embedded R via RInside
#include "mpi.h"
using namespace Rcpp;
using namespace Eigen;
using namespace std;
int main(int argc, char *argv[]) {
const int N_TRU_PARAMS = 5;
const int n = 1000;
const int t = 100;
const int nr = 500;
RInside R(argc, argv); // create an embedded R instance
stringstream ss;
ss << "n = " << n << "; t = " << t << ";" << " nr = " << nr << ";";
cout << ss.str() << endl;
R.parseEval(ss.str());
VectorXd tru = VectorXd(N_TRU_PARAMS);
tru << 2,1,-2,1,1;
// R.parseEval("n = 100;");
// R.parseEval("t = 100;");
R.parseEval("set.seed(123)");
string cmdstr = "tru = c(2,1,-2,1,1);"
"set.seed(123);"
"a = rnorm(n,tru[1],tru[2]);"
"b = rnorm(n,tru[3],tru[4]);"
// Make some data lists
"x = NULL;"
"y = NULL;"
// Generate some data for each firm
"for(i in 1:n) {"
" x[[i]] = rnorm(t,1,1);"
" y[[i]] = a[i]+b[i]*x[[i]]+ rnorm(t)*tru[5];"
"}";
R.parseEval(cmdstr);
SEXP Xmat_sexp = R.parseEval("Xmat = do.call(rbind, x)");
const Map<MatrixXd> Xmat(as<Map<MatrixXd> >(Xmat_sexp));
SEXP Ymat_sexp = R.parseEval("Ymat = do.call(rbind, y)");
const Map<MatrixXd> Ymat(as<Map<MatrixXd> >(Ymat_sexp));
cout << "X: " << Xmat.rows() << " x " << Xmat.cols() << endl;
cout << "Y: " << Ymat.rows() << " x " << Ymat.cols() << endl;
// Make matrices ua and ub
SEXP ua_sexp = R.parseEval( "ua = matrix(rnorm(nr*n),n,nr)");
const Map<MatrixXd> ua(as<Map<MatrixXd> >(ua_sexp));
SEXP ub_sexp = R.parseEval( "ub = matrix(rnorm(nr*n),n,nr)");
const Map<MatrixXd> ub(as<Map<MatrixXd> >(ub_sexp));
cmdstr = "track = 0;"
"Y = unlist(y);"
"X = unlist(x);"
"start = lm(Y~X)$coeff;"
"start =c(start[1],0,start[2],0);";
R.parseEval(cmdstr);
SEXP start_sexp = R.parseEval("start");
const Map<VectorXd> start_vector(as<Map<VectorXd> >(start_sexp));
cout << "Starting point: " << start_vector.transpose() << endl;
// THERE ARE TWO PAIRS OF FUNCTIONS, smle_init and smle_nll_mpi
// smle_init: distributes, using mpi broadcast, the necessary
// data to all machines.
//
// smle_nll_mpi: assuming smle_init has been successfully completed
// compute the negative log likelihood using MPI
//
// for now, these functions will be written out in the code
// // MPI INITIALIZATION
// int rank, size;
// const int root = 0;
// cout << "Before init" << endl;
// MPI::Init();
// rank = MPI::COMM_WORLD.Get_rank();
// size = MPI::COMM_WORLD.Get_size();
// double * ua_ptr = ua.data();
// double * ub_ptr = ub.data();
cout << "Initializating MPI Broadcast" << endl;
}
The RInside package comes with working examples for MPI:
edd#max:~$ ls -l /usr/local/lib/R/site-library/RInside/examples/mpi/
total 24
drwxr-xr-x 2 edd staff 4096 Jun 27 10:14 cmake
-rw-r--r-- 1 edd staff 1897 Jun 27 10:14 Makefile
-rw-r--r-- 1 edd staff 1224 Jun 27 10:14 rinside_mpi_sample0.cpp
-rw-r--r-- 1 edd staff 2896 Jun 27 10:14 rinside_mpi_sample1.cpp
-rw-r--r-- 1 edd staff 1137 Jun 27 10:14 rinside_mpi_sample2.cpp
-rw-r--r-- 1 edd staff 2836 Jun 27 10:14 rinside_mpi_sample3.cpp
edd#max:~$
with working support for make and CMake. I would start there. Adding support for RcppEigen is probably trivial; I just added another example directory for RcppArmadillo to the SVN of RInside a few days ago.
If the definition of Free() balks then you probably have a header file conflict which you may avoid by reordering include statements. I do not have the Intel compile so I cannot help.
Finally: the recommended way to get help for Rcpp and RInside is to ask on the
rcpp-devel mailing list.
Related
I want to create a project, I have 3 files, a test.cpp, something.h and a something.cpp. here they are:
test.cpp:
#include <bits/stdc++.h>
#define f cin
#define g cout
#include "something.h"
using namespace std;
int main(void)
{
int x;
register(x);
return 0;
}
something.h:
#ifndef __SOMETHING__H_
#define __SOMETHING__H_
#include <bits/stdc++.h>
void register(int x);
#endif
something.cpp:
#include "something.h"
void register(int x)
{
std::cout << x << '\n';
}
And here is the error I get:
In file included from test.cpp:4:0:
something.h:5:15: error: expected unqualified-id before ‘int’
void register(int x);
^~~
something.h:5:15: error: expected ‘)’ before ‘int’
test.cpp: In function ‘int main()’:
test.cpp:10:15: error: ISO C++ forbids declaration of ‘x’ with no type [-fpermissive]
register(x);
^
test.cpp:10:15: error: redeclaration of ‘int x’
test.cpp:9:9: note: ‘int x’ previously declared here
int x;
^
In file included from something.cpp:1:0:
something.h:5:15: error: expected unqualified-id before ‘int’
void register(int x);
^~~
something.h:5:15: error: expected ‘)’ before ‘int’
something.cpp:3:15: error: expected unqualified-id before ‘int’
void register(int x)
^~~
something.cpp:3:15: error: expected ‘)’ before ‘int’
Why does it tell me that I redefine x? When I just want to call register with it's value.
register is a reserved word in C++. Therefore, you have to give another (unreserved) name.
more information: Register keyword in C++ - Stack Overflow
I'm trying to use a static variable in one of my classes. I've used them before, without problem. Now I'm getting this error:
/tmp/ccg26aZi.o: In function 'main': main.cpp:(.text+0x7482):
undefined reference to `Rect::rect_change' collect2: error: ld
returned 1 exit status make: *** [exe] Error 1
any advice? Thanks!
UPDATE:
I was told to check my make file to make sure the rect.cpp was being included in the making of the exe. I switched the folder so it is in the correct place. But now I'm getting a whole new set of errors, I previously didn't get:
I checked my make file and made sure the rect.cpp, and moved it to a different folder that ran *.cpp. However Now I'm getting a brand new set of errors:
includes/Rect.h:16:17: error: ‘SDL_Rect’ does not name a type Rect(const SDL_Rect & r)
includes/Rect.h:16:28: error: ISO C++ forbids declaration of ‘r’ with no type [-fpermissive] Rect( const SDL_Rect & r)
includes/Rect.h:19:2: error: ‘SDL_Rect’ does not name a type
SDL_Rect getSDL_Rect() ^ includes/Rect.h: In constructor
‘Rect::Rect(const int&)’: includes/Rect.h:17:9: error: request for
member ‘x’ in ‘r’, which is of non-class type ‘const int’ : x(r.x),
y(r.y), w(r.w), h(r.h)
includes/Rect.h:17:17: error: request for member ‘y’ in ‘r’, which is
of non-class type ‘const int’ : x(r.x), y(r.y), w(r.w), h(r.h)
includes/Rect.h:17:25: error: request for member ‘w’ in ‘r’, which is
of non-class type ‘const int’ : x(r.x), y(r.y), w(r.w), h(r.h)
includes/Rect.h:17:33: error: request for member ‘h’ in ‘r’, which is
of non-class type ‘const int’ : x(r.x), y(r.y), w(r.w), h(r.h)
Here is the contents of my make file. the rect.cpp is located inside the src folder.
exe: main.cpp
g++ *.cpp src/*.cpp src/*.c `sdl-config --cflags --libs` -lSDL_image -lSDL_mixer -lSDL_ttf -Iincludes
run:
./a.out
r:
./a.out
clean:
rm a.out
c:
rm a.out
// header
#ifndef RECT_H
#define RECT_H
#include <iostream>
class Rect
{
public:
Rect(int x0 = 0, int y0 = 0, int w0 = 0, int h0 = 0)
: x(x0), y(y0), w(w0), h(h0)
{}
Rect( const SDL_Rect & r)
: x(r.x), y(r.y), w(r.w), h(r.h)
{}
SDL_Rect getSDL_Rect()
{
SDL_Rect r = {x, y, w, h};
return r;
}
int x, y, w, h;
static int rect_change;
static int rect_change_max;
};
inline std::ostream & operator<<(std::ostream & cout, const Rect & r)
{
cout << "(" << r.x << "," << r.y << "," << r.w << "," << r.h << ")";
return cout;
}
#endif
rect.cpp
#include "Rect.h"
int Rect::rect_change = 0;
int Rect::rect_change_max = 0;
// main.cpp example
#include "Rect.h"
int main()
{
Rect rect;
rect.rect_change = 5;
return 0;
}
Thanks for all the help, and thank you RichieHindle for the answer.
I needed to #include "SDL_gfxPrimitives.h" to get it to work. Funny that is the case because I've been working on this project for a while, and the compiler didn't complain at all, until I tried to add a static member.
Thanks again!
I need to overload increment for class Timer. Members of my class are minutes and seconds.
#include <iostream>
#include <conio.h>
using namespace std;
class Timer
{
private:
int minutes;
int seconds;
public:
Time(){
minutes = 0;
seconds = 0;
}
Time(int m, int s){
minutes = m;
seconds = s;
}
void displayTime()
{
cout << "M: " << hours << " S:" << minutes <<endl;
}
Time operator++ ()
{
++seconds;
if(seconds >= 60)
{
++minutes;
seconds -= 60;
}
return Time(minutes, seconds);
}
Time operator++( int )
{
Time T(minutes, seconds);
++seconds;
if(seconds >= 60)
{
++minutes;
seconds -= 60;
}
return T;
}
};
int main()
{
Time T1(18, 23), T2(19,12);
++T1;
T1.displayTime();
++T1;
T1.displayTime();
T2++;
T2.displayTime();
T2++;
T2.displayTime();
_getch()
}
When I debug, it says
Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\Makefile.win"
Executing make...
make.exe -f "C:\Dev-Cpp\Makefile.win" all
g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include"
main.cpp:13: error: ISO C++ forbids declaration of `Time' with no type
main.cpp:18: error: ISO C++ forbids declaration of `Time' with no type
main.cpp:29: error: ISO C++ forbids declaration of `Time' with no type
main.cpp:29: error: expected `;' before "operator"
main.cpp:40: error: expected `;' before "Time"
main.cpp:40: error: ISO C++ forbids declaration of `Time' with no type
main.cpp:40: error: expected `;' before "operator"
main.cpp:54: error: expected `;' before '}' token
main.cpp: In member function `void Timer::displayTime()':
main.cpp:26: error: `hours' undeclared (first use this function)
main.cpp:26: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp: At global scope:
main.cpp:56: error: new types may not be defined in a return type
main.cpp:56: error: extraneous `int' ignored
main.cpp:56: error: `main' must return `int'
main.cpp: In function `int main(...)':
main.cpp:57: error: `Time' undeclared (first use this function)
main.cpp:57: error: expected `;' before "T1"
main.cpp:59: error: `T1' undeclared (first use this function)
main.cpp:64: error: `T2' undeclared (first use this function)
main.cpp:69: error: expected `;' before '}' token
make.exe: *** [main.o] Error 1
Execution terminated
Object should be of type Timer not Time.
try to match class name and constructor name.
Hour member is not defined in displayTime method.
void displayTime()
{
cout << "M: " << minutes << " S:" << seconds <<endl;
}
Please refer following code
http://ideone.com/m50w2r
I seem to be getting several errors with
map<string,function<XMLSerializable*()>> mapConstructor;
Notably,
la5.cpp: In function ‘int main(int, char**)’:
la5.cpp:21:13: error: ‘function’ was not declared in this scope
la5.cpp:21:43: error: ‘mapConstructor’ was not declared in this scope
la5.cpp:21:43: error: template argument 2 is invalid
la5.cpp:21:43: error: template argument 4 is invalid
la5.cpp:25:58: warning: lambda expressions only available with -std=c++0x or - std=gnu++0x [enabled by default]
la5.cpp:33:26: error: expected primary-expression before ‘*’ token
la5.cpp:33:28: error: expected primary-expression before ‘)’ token
la5.cpp:33:31: error: ‘pFunc’ was not declared in this scope
make: *** [la5.o] Error 1
Unfortunately, I can't seem to find what I've done wrong, as it seems to deal with that map declaration which was given to the class by my instructor. Below is my .cpp
#include <iostream>
#include <map>
#include <string>
#include <functional>
#include "Armor.h"
#include "Weapon.h"
#include "Item.h"
#include "Creature.h"
using namespace std;
XMLSerializable * constructItem()
{
return new Item;
}
int main(int argc, char * argv[])
{
map<string,function<XMLSerializable*()>> mapConstructor;
mapConstructor["Item"] = constructItem;
mapConstructor["Creature"] = []() {return new Creature; };
cout << "Input the class name, then we'll try to construct it." << endl;
string sLookup = " ";
cin >> sLookup;
function<XMLSerializable*()> pFunc = mapConstructor[sLookup];
if(pFunc() == NULL)
{
cout << "Sorry, the object couldn't be constructed." << endl;
}
else
{
cout << pFunc() << " a non NULL value was returned!" << endl;
}
return 0;
}
Any suggestions? I'm unfamiliar with maps, but I believe this should work, right?
Coding in pico, compiling with a makefile using g++.
It looks like you just are forgetting to add -std=c++11 or -std=c++0x to your compiler flags to enable C++11.
-std=c++0x is deprecated, but on older versions of g++, -std=c++11 is not available.
From http://cppcms.com/wikipp/en/page/cppcms_1x_tut_url_mapping
After rewriting, hello.cpp code is below:
#include <cppcms/application.h>
#include <cppcms/service.h>
#include <cppcms/http_response.h>
#include <cppcms/url_dispatcher.h>
#include <cppcms/url_mapper.h>
#include <cppcms/applications_pool.h>
#include <iostream>
#include <stdlib.h>
hello(cppcms::service &srv) :
cppcms::application(srv)
{
dispatcher().assign("/number/(\\d+)",&hello::number,this,1);
mapper().assign("number","/number/{1}");
dispatcher().assign("/smile",&hello::smile,this);
mapper().assign("smile","/smile");
dispatcher().assign("",&hello::welcome,this);
mapper().assign("");
mapper().root("/hello");
}
void number(std::string num)
{
int no = atoi(num.c_str());
response().out() << "The number is " << no << "<br/>\n";
response().out() << "<a href='" << url("/") << "'>Go back</a>";
}
void smile()
{
response().out() << ":-) <br/>\n";
response().out() << "<a href='" << url("/") << "'>Go back</a>";
}
void welcome()
{
response().out() <<
"<h1> Wellcome To Page with links </h1>\n"
"<a href='" << url("/number",1) << "'>1</a><br>\n"
"<a href='" << url("/number",15) << "'>15</a><br>\n"
"<a href='" << url("/smile") << "' >:-)</a><br>\n";
}
But now, it throw an error on compiling:
hello.cpp:10:27: error: ISO C++ forbids declaration of ‘hello’ with no type [-fpermissive]
hello.cpp: In function ‘int hello(cppcms::service&)’:
hello.cpp:11:5: error: only constructors take member initializers
hello.cpp:13:16: error: ‘dispatcher’ was not declared in this scope
hello.cpp:13:43: error: ‘hello’ is not a class or namespace
hello.cpp:13:57: error: invalid use of ‘this’ in non-member function
hello.cpp:14:12: error: ‘mapper’ was not declared in this scope
hello.cpp:16:35: error: ‘hello’ is not a class or namespace
hello.cpp:16:48: error: invalid use of ‘this’ in non-member function
hello.cpp:19:29: error: ‘hello’ is not a class or namespace
hello.cpp:19:44: error: invalid use of ‘this’ in non-member function
hello.cpp: In function ‘void number(std::string)’:
hello.cpp:29:14: error: ‘response’ was not declared in this scope
hello.cpp:29:14: note: suggested alternative:
/usr/local/include/cppcms/http_response.h:31:19: note: ‘cppcms::http::response’
hello.cpp:30:47: error: ‘url’ was not declared in this scope
hello.cpp: In function ‘void smile()’:
hello.cpp:35:14: error: ‘response’ was not declared in this scope
hello.cpp:35:14: note: suggested alternative:
/usr/local/include/cppcms/http_response.h:31:19: note: ‘cppcms::http::response’
hello.cpp:36:47: error: ‘url’ was not declared in this scope
hello.cpp: In function ‘void welcome()’:
hello.cpp:41:14: error: ‘response’ was not declared in this scope
hello.cpp:41:14: note: suggested alternative:
/usr/local/include/cppcms/http_response.h:31:19: note: ‘cppcms::http::response’
hello.cpp:43:39: error: ‘url’ was not declared in this scope
I think the new hello.cpp codes is incomplete (and no idea the complete code), but because it said "rewrite", I just removed the old code and create new one.
I've tried combine with old Hello World but no luck.
In the linked-to example the constructor is defined inside the class (in the header). If you define it in a .cpp file, you will have to add the class name to the constructor and member functions.
hello::hello(cppcms::service &srv) :
cppcms::application(srv)
{
...
void hello::number(std::string num)
{
Otherwise the compiler believes that you are adding some unrelated free functions.