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!
Related
I was messing around with c++ classes when I ran into an issue. I am trying to make a player class inherit from the Entity class and mix their constructors. The entity class has a constructor that requires you to provide the x position float and the y position float. I want the Player class to require x and y position too, but I also want the name to be required. How would I do this?
Here is my code:
#include <iostream>
class Entity{
public:
float x, y;
int health = 30;
Entity(float X, float Y){
std::cout << "Entity Constructed!" << std::endl;
x = X;
y = Y;
}
~Entity(){
std::cout << "Entity Destructed!" << std::endl;
}
void Print(){
std::cout << x << ", " << y << std::endl;
}
void Move(float mx, float my){
x+=mx;
y+=my;
}
};
class Player : public Entity{
public:
const char* Name;
int attack_damage = 5;
Player(const char* name){
Name = name;
}
void Attack(Entity& entity){
entity.health-=attack_damage;
}
};
int main(){
Entity e(0.0f, 0.0f);
Player p(0.0f,0.0f);
std::cout << e.health << std::endl;
p.Attack(e);
std::cout << e.health << std::endl;
std::cin.get();
}
and here is the error message I got when I tried to compile the code provided above:
FAILED: CMakeFiles/learning-cpp.dir/src/main.cpp.o
/usr/bin/c++ -Wall -Werror -std=c++14 -g -MD -MT CMakeFiles/learning-cpp.dir/src/main.cpp.o -MF CMakeFiles/learning-cpp.dir/src/main.cpp.o.d -o CMakeFiles/learning-cpp.dir/src/main.cpp.o -c src/main.cpp
src/main.cpp: In constructor ‘Player::Player(const char*)’:
src/main.cpp:33:29: error: no matching function for call to ‘Entity::Entity()’
Player(const char* name){
^
src/main.cpp:8:5: note: candidate: ‘Entity::Entity(float, float)’
Entity(float X, float Y){
^~~~~~
src/main.cpp:8:5: note: candidate expects 2 arguments, 0 provided
src/main.cpp:3:7: note: candidate: ‘constexpr Entity::Entity(const Entity&)’
class Entity{
^~~~~~
src/main.cpp:3:7: note: candidate expects 1 argument, 0 provided
src/main.cpp: In function ‘int main()’:
src/main.cpp:44:23: error: no matching function for call to ‘Player::Player(float, float)’
Player p(0.0f,0.0f);
^
src/main.cpp:33:5: note: candidate: ‘Player::Player(const char*)’
Player(const char* name){
^~~~~~
src/main.cpp:33:5: note: candidate expects 1 argument, 2 provided
src/main.cpp:28:7: note: candidate: ‘constexpr Player::Player(const Player&)’
class Player : public Entity{
^~~~~~
src/main.cpp:28:7: note: candidate expects 1 argument, 2 provided
src/main.cpp:28:7: note: candidate: ‘constexpr Player::Player(Player&&)’
src/main.cpp:28:7: note: candidate expects 1 argument, 2 provided
ninja: build stopped: subcommand failed.
Try this
Player(float x, float y, const char* name)
: Entity(x,y)
{
Name = name;
}
I'm getting a compiler error when I'm trying to initialize my array with function pointers. Without using a class I'm able to run the code fine, but when I incorporate the code in a class I'm getting the error. I suppose this is more of a problem with my understanding of class usage, the scope resolution operator, etc. Any help to get this resolved would be much appreciated.
#include <iostream>
#include <cassert>
using namespace std;
#define F1 0
#define F2 1
#define F3 2
class A
{
private:
bool Func1();
bool Func2();
bool Func3();
public:
bool do_it(int op);
typedef bool (A::*fn)(void);
static fn funcs[3];
protected:
};
A::fn A::funcs[3] = {Func1, Func2, Func3};
int main()
{
A Obj;
cout << "Func1 returns " << Obj.do_it(F1) << endl;
cout << "Func2 returns " << Obj.do_it(F2) << endl;
cout << "Func3 returns " << Obj.do_it(F3) << endl;
return 0;
}
bool A::do_it(int op)
{
assert(op < 3 && op >= 0);
return (this->*(funcs[op]))();
}
bool A::Func1() { return false; }
bool A::Func2() { return true; }
bool A::Func3() { return false; }
The compiler spits out:
15:35:31 **** Build of configuration Debug for project JT ****
make all
make: Warning: File 'objects.mk' has modification time 7.3 s in the future
Building file: ../src/JT.cpp
Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/JT.d" -MT"src/JT.o" -o "src/JT.o" "../src/JT.cpp"
../src/JT.cpp:141:41: error: cannot convert ‘A::Func1’ from type ‘bool (A::)()’ to type ‘A::fn {aka bool (A::*)()}’
A::fn A::funcs[3] = {Func1, Func2, Func3};
^
../src/JT.cpp:141:41: error: cannot convert ‘A::Func2’ from type ‘bool (A::)()’ to type ‘A::fn {aka bool (A::*)()}’
../src/JT.cpp:141:41: error: cannot convert ‘A::Func3’ from type ‘bool (A::)()’ to type ‘A::fn {aka bool (A::*)()}’
src/subdir.mk:18: recipe for target 'src/JT.o' failed
make: *** [src/JT.o] Error 1
15:35:32 Build Finished (took 1s.64ms)
Use A::fn A::funcs[3] = {&A::Func1, &A::Func2, &A::Func3};
In the following code, I have a big vector with 10 members that can be divided into 4 subvectors:
data[10]
P=data[0 to 2]
Q=data[3 to 5]
R=data[6 to 6]
S=data[7 to 9]
Since, vector R only has one member, I prefer to return an rvalue directly from data. I want to do this operation on this value:
x.R()=1983.2+x.R();
like what I can do with Q. But, I receive this error:
g++ -g -c main.cpp -std=c++11 -larmadillo -DNDEBUG
main.cpp: In member function ‘Super_vector::R_type&& Super_vector::R()’:
main.cpp:38:24: error: cannot bind ‘double’ lvalue to ‘Super_vector::R_type&& {aka double&&}’
return data(6,6);
^
main.cpp: In function ‘int main()’:
main.cpp:52:7: error: using xvalue (rvalue reference) as lvalue
x.R()=1983.2+x.R();
^
make: *** [all] Error 1
main.cpp
#include <armadillo>
#include <iostream>
using namespace std;
typedef arma::vec::fixed<10> x_vec;
class Super_vector
{
public:
x_vec data;
typedef decltype(std::declval<x_vec>().subvec(0, 2)) P_type;
typedef decltype(std::declval<x_vec>().subvec(3, 5)) Q_type;
typedef double R_type;
typedef decltype(std::declval<x_vec>().subvec(7, 9)) S_type;
Super_vector(x_vec data_) :
data(data_)
{
}
inline P_type P()
{
return data.subvec(0,2);
}
inline Q_type Q()
{
return data.subvec(3,5);
}
inline R_type&& R()
{
return data(6,6);
}
inline S_type S()
{
return data.subvec(7,9);
}
};
int main()
{
Super_vector x({2,3,5,7,11,13,17,19,23,29});
x.Q()=-x.Q();
x.R()=1983.2+x.R();
x.data.print();
return 0;
}
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.
I'm a new code monkey in training, and I'm currently having issues working with arrays and structs.
Currently I have a main file where I have a an Array of Records declared. I pass that array to an external function, where a quick sort is performed on the with of the fields in the record. Mainly the first name. I'm having an issues where I copy elements in the array of records, to a temporary array for the sorting algorith. I know that c++ does have a qsort function built in, but for what I'm working on right now, I need to have the algoritm written out the way it is. I was able to get this to work using only any array.
I'm getting the following error when trying to compile with the make file.
make
g++ -c -o main2.o main2.cpp
g++ -c externArray2.cpp -o externArray2.o
externArray2.cpp: In function ‘void copytemp(EmployeeRecord*, EmployeeRecord*, int, int)’:
externArray2.cpp:52: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:53: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:54: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:55: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:56: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:57: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:58: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:59: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
externArray2.cpp:60: error: cannot convert ‘float’ to ‘void*’ for argument ‘1’ to ‘void* memcpy(void*, const void*, size_t)’
make: *** [externArray2.o] Error 1
Make File
test1: main.o ExternArray.o
g++ main.o ExternArray.o -o test1
externArray.o: ExternArray.cpp
g++ -c ExternArray.cpp -o ExternArray.o
main.o: main.cpp
g++ -c main.cpp -o main.o
Header.h
#ifndef _INCL_GUARD
#define _INCL_GUARD
const int maxEmployee =10;
const int NAMES = 5;
const int LENGTH = 15;
typedef struct EmployeeRecord
{
char first[10];
char last[10];
float reghours;
float ovrhours;
float pay;
float gross;
float defer;
float state;
float fed;
float ssi;
float net;
} EmployeeRecord;
#endif
main.cpp
#include <iostream>
#include <cstring>
using namespace std;
#include "./Header2.h"
void showArray(EmployeeRecord employees[], int, const char*); //Function Prototype 3.1
extern void qsortArray(EmployeeRecord employees[], int, int); //Funvtion Prototype 3.2
int main(void)
{
EmployeeRecord myEmployee[maxEmployee];
strcpy(myEmployee[0].first,"John");
strcpy(myEmployee[0].last,"Doe");
strcpy(myEmployee[1].first,"Ed");
strcpy(myEmployee[1].last, "Whittle");
strcpy(myEmployee[2].first, "Louise");
strcpy(myEmployee[2].last, "Marion");
strcpy(myEmployee[3].first,"Paula");
strcpy(myEmployee[3].last, "Prentiss");
strcpy(myEmployee[4].first, "Carl");
strcpy(myEmployee[4].last, "Davidson");
showArray(myEmployee, NAMES, "Before Sort");
qsortArray(myEmployee, 0, 4 );
showArray(myEmployee, NAMES, "After Sort");
return 0;
}
void showArray(EmployeeRecord employees[], int emp, const char *message)
{
cout << message << endl;
for (int test = 0; test < emp; test++)
{
cout << "First Name: " << employees[test].first << endl;
cout << "Last Name: " << employees[test].last << endl;
}
}
ExternArray.cpp
#include <cstring>
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
#include "./Header2.h"
void qsortArray(EmployeeRecord employees[], int, int); //Funvtion Prototype 3.2
void copytemp(EmployeeRecord tmpEmp[], EmployeeRecord emp[], int, int);
void qsortArray(EmployeeRecord employees[], int start, int finish)
{
int left=start,
right=finish;
char pivot[15];
strcpy(pivot, employees[(start+finish)/2].first);
while (left < right) {
cout << pivot << " pivot " << endl;
cout << "outer loop" << endl;
// find left candidate
while (strcmp(employees[left].first,pivot) <0) left++;
// find right candidate
cout << "First Inner Loop" << endl;
while (strcmp(employees[right].first,pivot) > 0 )right--;
cout << "Inner Loop" << endl;
if (left <= right)
{
EmployeeRecord tmpEmployee[1];
cout << "Create new struct" << endl;
copytemp(tmpEmployee, employees, 0, left);
cout << "copy to temp" << endl;
copytemp(tmpEmployee, employees, 1, right);
copytemp(employees, tmpEmployee, left, 1);
copytemp(employees, tmpEmployee, right, 0);
left++;
right--;
cout << "All copy done" <<endl;
}
} // while left < right
cout << "Back out of outer Loop" << endl;
if (start < right) qsortArray(employees,start,right);
if (left < finish) qsortArray(employees,left,finish);
}
void copytemp(EmployeeRecord tmpEmp[], EmployeeRecord emp[], int first, int secound)
{
memcpy(tmpEmp[first].first, emp[secound].first, sizeof(emp[secound].first));
memcpy(tmpEmp[first].last, emp[secound].last, sizeof(emp[secound].last));
memcpy(tmpEmp[first].reghours, emp[secound].reghours, sizeof(emp[secound].reghours));
memcpy(tmpEmp[first].ovrhours, emp[secound].ovrhours, sizeof(emp[secound].ovrhours));
memcpy(tmpEmp[first].pay, emp[secound].pay, sizeof(emp[secound].pay));
memcpy(tmpEmp[first].gross, emp[secound].gross, sizeof(emp[secound].gross));
memcpy(tmpEmp[first].defer, emp[secound].defer, sizeof(emp[secound].defer));
memcpy(tmpEmp[first].state, emp[secound].state, sizeof(emp[secound].state));
memcpy(tmpEmp[first].fed, emp[secound].fed, sizeof(emp[secound].fed));
memcpy(tmpEmp[first].ssi, emp[secound].ssi, sizeof(emp[secound].ssi));
memcpy(tmpEmp[first].net, emp[secound].net, sizeof(emp[secound].net));
}
In C you can copy an entire struct with a plain assignment
tmpElm[first] = emp[secound];
This is only problematic if the struct contains members which are pointers, which is not your case.
Try:
memcpy(&(tmpElm[first].first, &(emp[second].first), sizeof(emp[second].first));
The difference is that you need to pass an address of a float rather than a float value.
You may find that with C++ you need to cast the float pointer that the address of operator (&) gives you to a void, since C++ has stronger typing rules than C.
memcpy((void *)&(tmpElm[first].first, (void *)&(emp[second].first), sizeof(emp[second].first));
There are a few other ways to get the address of something when arrays are involved, but this is the simplest for a new programmer.