initalizing array of vectors in constructor in sfml - c++

I use a SFML library for graphics and other stuff,such as vectors. In my Brain class I try to do something like:
class Brain{
Brain(int size){
Vector2f directions[size];
}
}
But it throws an error saying it must evaluate to a constant. I tried all sorts of things but I can't get it to compile properly. Anyone knows why this happens and how can I fix it?

As suggested in the comments in C++ the size of an array must be known at compile time... if you need dynamic containers you can use std::vector.
class Brain {
public:
Brain(int size) : _directions{size}
{
}
private:
vector<Vector2d> _directions;
};
Don't forget public and private access to your class... By default everything is private on a class so in your snippet the contructor of the class is private!

Related

Initializing a new nested class from outer class function

I'm currently learning nested classes in C++ while building a project and I currently inside setupBLE() I need to pass one of the nested classes but to init that new class I need to pass to its constructor the outer class so it can access its variables and functions but I'm not exactly sure how to pass to the constructor the pointer of the class that's trying to create it.
It's a bit confusing so I hope the code helps with it.
Like in python we have self but in C++ as far as I know we don't have that so I was wondering what should I pass to the constructor.
Code (PillDispenser.h):
class PillDispenser {
public:
explicit PillDispenser(BLEAddress deviceAddress);
private:
BLEAddress _device_address;
BLEAdvertisedDevice _device;
bool _connected;
// Device properties
std::string _device_name;
// Callbacks
static void notifyCallBack();
class AdvertisedDeviceCallBack : public BLEAdvertisedDeviceCallbacks {
PillDispenser &_outer;
explicit AdvertisedDeviceCallBack(PillDispenser &outer) : _outer(outer){};
void onResult(BLEAdvertisedDevice advertisedDevice) override;
};
}
Code (PillDispenser.cpp):
void PillDispenser::setupBLE() {
BLEScan *scanner = BLEDevice::getScan();
scanner->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallBack());
scanner->setInterval(SCAN_INTERVAL);
scanner->setWindow(SCAN_WINDOW);
scanner->setActiveScan(true);
scanner->start(SCAN_DURATION);
}
Issue:
This line is trying to use the default constructor which does not exist
scanner->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallBack());
instead you should use the explicit constructor you defined
scanner->setAdvertisedDeviceCallbacks(new AdvertisedDeviceCallBack(*this));
note that this (in this context) has type PillDispenser* so you have to dereference with * to get a PillDispenser&

Compilation error Friend class unable to access field

I am trying to compile QT5.3
The files in question are qv4executableallocator_p.h and qv4executableallocator.cpp. Relevant code snippet from the header is below
struct Allocation{
Allocation()
: addr(0)
, size(0)
, free(true)
, next(0)
, prev(0)
{}
void *start() const;
void invalidate() { addr = 0; }
bool isValid() const { return addr != 0; }
void deallocate(ExecutableAllocator *allocator);
private:
~Allocation() {}
friend class ExecutableAllocator;
Allocation *split(size_t dividingSize);
bool mergeNext(ExecutableAllocator *allocator);
bool mergePrevious(ExecutableAllocator *allocator);
quintptr addr;
uint size : 31; // More than 2GB of function code? nah :)
uint free : 1;
Allocation *next;
Allocation *prev;
};
In the cpp function ExecutableAllocator::ChunkOfPages::~ChunkOfPages() I get a compilation error when trying to access alloc->next.
QV4::ExecutableAllocator::Allocation* QV4::ExecutableAllocator::Allocation::next’ is private
Code can be seen online at https://qt.gitorious.org/qt/qtdeclarative/source/be6c91acc3ee5ebb8336b9e79df195662ac11788:src/qml/jsruntime
My gcc version is relatively old... 4.1
Is this the issue or is something else wrong in my environment. I would like a way to go forward. I am stuck with this compiler, since it is the one I have to use on my target platform
I'd guess that the QV4::ExecutableAllocator::ChunkOfPages struct is not directly befriended with Allocation, so you can't access Allocation's private data in its destructor in C++ prior to C++11 standard.
Try adding friend struct ExecutableAllocator::ChunkOfPages to the Allocation definition, that should do the trick.
There was a slight change in the way nested classes are handled in C++11 (cited from cppreference.com):
Prior C++11, member declarations and definitions inside the nested class of the friend of class T cannot access the private and protected members of class T, but some compilers accept it even in pre-C++11 mode.
Which could explain why this worked in a new compiler, but not in your old one.

Segfault when trying to access function of member in a library

I have a library that is all tested thoroughly through google test suite. I am trying to keep it "pimpl" clean, but I'm running into a segfault I can't quite figure out.
Relevant Code:
Interface.h:
class Interface{
public:
Interface();
void Function(const int argument);
private:
std::unique_ptr<Implementation> Implement;
std::unique_ptr<DependencyInjection> Injection1, Injection2;
};
Interface.cpp:
Interface::Interface()
: Injection1(new DependencyInjection()),
Injection2(new DependencyInjection()),
Implement(new Implementation(*Injection1, *Injection2)) {}
void Interface::Function(const int argument){ Implement->Function(argument); }
Implementation.h:
class Implementation{
public:
Implementation(AbstractInjection &injection1, AbstractInjection &injection2);
void Function(const int argument);
private:
AbstractInjection Injection1, Injection2;
};
Implementation.cpp
Implementation::Implementation(AbstractInjection &injection1, AbstractInjection &injection2)
: Injection1(injection1),
Injection2(injection2) {}
void Implementation::Function(const int argument){
injection1.Function(argument); } // code from here out is all well tested and works
So when I create the interface and call Interface.Function() the code segfaults when it tries to evaluate Implementation.Function(). I've ran gdb through everything I can think of, all the pointers are non-null.
If I just create a test that looks like
std::unique_ptr<DependencyInjection1> injection1(new DependencyInjection());
std::unique_ptr<DependencyInjection2> injection2(new DependencyInjection());
std::unique_ptr<Implementation> implement(new Implementation(*injection1, *injection2));
implement->Function(0);
The code works fine and does not segfault
But if I create a test like
Interface iface;
iface.Function(0);
it will segfault.
I am new to the whole unique_ptr thing, but I have a suspicion that isn't the larger problem. It may be a red herring, I don't know.
The problem should actually pop as as a warning.
Initializers are done in the order in which they appear in the class definition, not in which they appear in the constructor!
Switch it to:
class Interface{
public:
Interface();
void Function(const int argument);
private:
std::unique_ptr<DependencyInjection> Injection1, Injection2;
std::unique_ptr<Implementation> Implement;
};
From here: C++: Initialization Order of Class Data Members, this is "12.6.2 of the C++ Standard"
You've got a wrong order of member fields, they are initialized in order they are declared in the class. So implement is initialized before both injections. Use -Werror=reorder to get compiler error (for GCC and probably CLang)

QVector<T> operator= and operator[]

I'm writing a program for my friends during my summer time and I occurred really strange problem.
I'm assigning a QVector< T > to other QVector< T > with operator= in my homemade template class and then trying to check whethere this 2nd QVector< T > have any elements. First, I'm checking it's size and it's good, but when I use operator[] and function GetName() which is a member of class T.
Here's some code:
template <template T> class Turniej {
private:
QVector<T>* list_;
QVector<Stol<T> > stoly_;
int round_;
int max_rounds_;
Turniej() {round_=1;}
public:
static Turniej& GetTurniej() {
static Turniej turniej;
return turniej; }
void SetList(QVector<T> *list) {list_=list; }
QVector<T>& GetList() {return *list_;}
//Some other irrelevant methods
};
Then I invoke SetList() function with reference to QVector filled with 2 objects of Gracz class
Turniej<Gracz>::GetTurniej().SetList(&list)
And when I'm finally trying to get to the list_ object in Turniej class by code below, I get this error:
ASSERT failure in QVector::operator[]: "index out of range", file .../QtCore/qvector.h, line 356
qDebug()<<Turniej<Gracz>::GetTurniej().GetList()[0].GetName();
Where GetName() returns some QString and for certain that method exists in Gracz class.
I would really appreciate any help, because I'm stuck with it for 2 days and have completely no idea what's going on. What is strange, I was trying to google that error but I haven't found any topic with error in that particular line of qvector.h.
I'm using Qt 5.0.2 with Qt Creator
Thanks for any help!
when you call SetList, you are transferring the ownership of the list, are you shure you are not deleting it (or it is not getting deleted automatically) afterwards?
Change list_=list; to list_ = new QVector(list). Maybe your vector is modified somewhere outside of your class? You're just keeping reference to vector given to your method, it seems like copying is what you want.
updated
I see your class is a singleton. Always remember to think twice before making a class a singleton - it's not a thing you want to have in your code, and it should be avoided when possible.

Boost::python - filling a C++ vector of pointer on object of a class in python

Here is my problem. I am supposed to make a python code able to fill a C++ vector of pointer on object of a class of the c++ code. I am using Boost::Python.
It's look as follow:
C++ code:
class A_Base {}
class A_derived_1 {}
...
class A_derived_N {}
class B {
...
setAderivediList(vector<A *> &_AderivedList){}
}
What I need to do in Python:
B.setAderivediList(_AderivedList)
I cannot modified the method in the C++ code, I can only add other objects in-between #ifdef python_interface in order not to impact other developpers work.
At first, I tried to write a converter from what I found on the Internet, but I failed to make it works for my code. Then I tried to add a _AderivedList in class B and a setter which fill the vector. Though it compile in C++, it dosen't work in Python.
New B class:
class B {
...
setAderivediList(vector<A *> &_AderivedList){}
#ifdef myPython_code
public:
vector<A *> * _AderivedListPy;
setAListPy(A * &my_Aderived){ //Actually, I tried without the reference as well
this->_AderivedListPy->push_back(my_Aderived);
};
#endif
}
In python it becomes:
myB = mydll.B()
Ai = mydll.A_derived_i()
myB.setAListPy(Ai) #stopping here
myB.setAderivediList(myB._AderivedListPy)
If I use the reference, it will throw a "python argument types did not match C++ signature" error message at myB.setAListPy(Ai), without the reference, it will stop at the same line without throwing any error message.
Any clue of how I could improve this or any other way to do it?
Thank you in advance.
Actually, I found a solution to my issue.
By filling the vector (including calling setAderivediList) directly in setAListPy.
I just pass an int my_A_derived to setAListPy (actually, along with other arg required by all A_derived class constructors) and use a if condition on my_A_derived.