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;
}
Related
The GNU MP manual specifies the declaration of the function get_mpz_t:
Function: mpz_t mpz_class::get_mpz_t ()
So I was expecting a mpz_t return type. But running the simple code:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main (void) {
mpz_class n;
n = "12345678901234567890123456789012345678901234567890";
mpz_t m;
mpz_init(m);
m = n.get_mpz_t();
gmp_printf("m %Zd\n", m);
}
Compiled with
g++ mpz_test.cc -o mpz_test -lgmpxx -lgmp
Produces the error output at line m = n.get_mpz_t():
mpz_split.cc: In function ‘int main()’:
mpz_split.cc:12:4: error: incompatible types in assignment of ‘mpz_ptr {aka __mpz_struct*}’ to ‘mpz_t {aka __mpz_struct [1]}’
m = n.get_mpz_t();
^
Looking at the gmpxx.h code I find the declarations:
// conversion functions
mpz_srcptr __get_mp() const { return mp; }
mpz_ptr __get_mp() { return mp; }
mpz_srcptr get_mpz_t() const { return mp; }
mpz_ptr get_mpz_t() { return mp; }
And, of course, mpz_ptr is defined in gmp.h
typedef __mpz_struct *mpz_ptr;
So, is the manual inaccurate? Or, what am I doing wrong here?
The problem occurs on this line:
m = n.get_mpz_t();
You cannot assign to mpz_t directly, instead use mpz_set():
mpz_set(m, n.get_mpz_t());
So, your code should look like this:
#include <iostream>
#include <gmpxx.h>
using namespace std;
int main (void) {
mpz_class n;
n = "12345678901234567890123456789012345678901234567890";
mpz_t m;
mpz_init(m);
mpz_set(m, n.get_mpz_t()); // correct assignment
gmp_printf("m %Zd\n", m);
}
I'm trying to define explicit conversion from some class to std::function like this:
#include <functional>
class ExpInt { private:
const int value;
public:
ExpInt(const int v):value(v){}
explicit operator std::function<int (void)> ()
{
return [=](void){ return value; };
}
};
int main(int argc, char **argv)
{
auto e = new ExpInt(44);
auto f = static_cast<std::function<int (void)>>(e);
return 0;
}
But when I compile it I get the following error:
$ g++ main.cpp -o main
main.cpp: In function ‘int main(int, char**)’:
main.cpp:16:51: error: no matching function for call to ‘std::function<int()>::function(ExpInt*&)’
auto f = static_cast<std::function<int (void)>>(e);
^
The compiler tells you what's wrong:
error: no matching function for call to ‘std::function<int()>::function(ExpInt*&)’
auto f = static_cast<std::function<int (void)>>(e);
^
A pointer to ExpInt is not convertible to std::function<int (void)>. ExpInt would be convertible, so if you simply indirect through the pointer, that would work:
auto f = static_cast<std::function<int (void)>>(*e);
P.S. You leak the dynamic allocation. Avoid using owning bare pointers.
I am having trouble overloading class member functions that are marked const, while there is no problem when the functions are not marked const. Also the overload itself works fine in pure C++.
The following fails
#include <vector>
#include <pybind11/pybind11.h>
class Foo
{
public:
Foo(){};
std::vector<double> bar(const std::vector<double> &a) const
{
return a;
}
std::vector<int> bar(const std::vector<int> &a) const
{
return a;
}
};
namespace py = pybind11;
PYBIND11_MODULE(example,m)
{
py::class_<Foo>(m, "Foo")
.def("bar", py::overload_cast<const std::vector<double>&>(&Foo::bar));
}
Compiled using:
clang++ -O3 -shared -std=c++14 `python3-config --cflags --ldflags --libs` example.cpp -o example.so -fPIC
Gives error:
...
no matching function for call to object of type 'const detail::overload_cast_impl<const vector<double, allocator<double> > &>'
.def("bar", py::overload_cast<const std::vector<double>&>(&Foo::bar));
...
Whereas the code works when I remove the const mark of the functions.
How should I perform this overload?
There is a special tag for const overloaded methods.
namespace py = pybind11;
PYBIND11_MODULE(example,m)
{
py::class_<Foo>(m, "Foo")
.def("bar", py::overload_cast<const std::vector<double>&>(&Foo::bar, py::const_));
}
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};
I am getting this error when I try to compile my class. I have made sure there is function prototypes and variables are initilized correctly, hopefully someone can help me figure out the problem
g++ -c main.cc
g++ -c BankControl.cc
g++ -c Bank.cc
g++ -c Account.cc
g++ -c View.cc
g++ -c AcctList.cc
g++ -c Customer.cc
g++ -c CustArray.cc
g++ -c Transaction.cc
Transaction.cc: In function ‘int getTransID()’:
Transaction.cc:18:34: error: ‘transID’ was not declared in this scope
int getTransID() { return transID; }
^
Transaction.cc: In function ‘TransType getTType()’:
Transaction.cc:19:34: error: ‘tType’ was not declared in this scope
TransType getTType() { return tType; }
^
Transaction.cc: In function ‘TransState getTState()’:
Transaction.cc:20:34: error: ‘tState’ was not declared in this scope
TransState getTState() { return tState; }
^
Transaction.cc: In function ‘std::__cxx11::string getDate()’:
Transaction.cc:21:34: error: ‘date’ was not declared in this scope
string getDate() { return date; }
^
Transaction.cc: In function ‘int getTAcctNum()’:
Transaction.cc:22:34: error: ‘tAcctNum’ was not declared in this scope
int getTAcctNum() { return tAcctNum; }
^
Transaction.cc: In function ‘float getTAmount()’:
Transaction.cc:23:34: error: ‘tAmount’ was not declared in this scope
float getTAmount() { return tAmount; }
^
Transaction.cc: In function ‘void setDate(std::__cxx11::string)’:
Transaction.cc:28:3: error: ‘date’ was not declared in this scope
date = d;
^
Makefile:31: recipe for target 'Transaction.o' failed
make: *** [Transaction.o] Error 1
Here is my header file
#ifndef TRANSACTION_H
#define TRANSACTION_H
#include <string>
using namespace std;
#include "defs.h"
class Transaction
{
public:
Transaction(TransType = TTERROR, TransState = TSERROR,int = 0 ,float = 0);
int getTransID();
TransType getTType();
TransState getTState();
string getDate();
int getTAcctNum();
void setDate(string);
float getAmount();
private:
static int nextTransID;
int transID;
TransType tType;
TransState tState;
string date;
int tAcctNum;
float tAmount;
};
#endif
Here is my source file
#include "Transaction.h"
#include "defs.h"
#include <string>
using namespace std;
int Transaction::nextTransID = 2001;
Transaction::Transaction(TransType t, TransState s, int acct, float amount)
{
transID = nextTransID++;
tType = t;
tState = s;
tAcctNum = acct;
tAmount = amount;
}
int getTransID() { return transID; }
TransType getTType() { return tType; }
TransState getTState() { return tState; }
string getDate() { return date; }
int getTAcctNum() { return tAcctNum; }
float getTAmount() { return tAmount; }
void setDate(string d)
{
date = d;
}
I am kinda lost on what is the issue
This:
int getTransID() { return transID; }
has nothing to do with your class, it's a global function.
You meant:
int Transaction::getTransID() { return transID; }
Also, that function (and the other getters) should be made const to signal that they don't modify the object.