index(of:_) method for user defined type in swift 3 is not working [duplicate] - swift3

This question already has answers here:
Cannot invoke 'indexOf' with an argument list of type '(ChecklistItem)'
(5 answers)
Closed 5 years ago.
let index = self.placeObjects.index(of:place)
here placeObjects is the array of type PlaceModel which is class created by myself. I am trying to get the index of an element of the same type but, it throws an error that
Cannot invoke 'index' with an argument list of type '(of: PlaceModel)'

To get index(of:) to work, your PlaceModel needs to implement Equatable:
extension PlaceModel: Equatable {
static func==(_ lhs: PlaceModel, _ rhs: PlaceModel) -> Bool {
// determine place equality here
}
}

Related

global scoped variables in c++ not properly initialized [duplicate]

This question already has answers here:
Prevent static initialization order "fiasco", C++
(3 answers)
c++ static initialization order fiasco
(2 answers)
Is the "static initialization order fiasco" a concern for constexpr variables?
(1 answer)
Closed 2 years ago.
I ported my code from stm32f072 to stm32f105. Since then I have an issue with global variables, it seems they don't get initialized. I use Atollic TrueStudio and their compile and build tools. For the new platform I tried converting the existing "eclipse" project and also to create a new one from CubeMx.
I got a Hardfault when accessing a global object. The "workaround" was moving the new statement in the accessing function. The debugger showed that even when having the new statement the global var was 0x00.
I also use an std::queue which has an size() of 0xffffffe7 when not inserted anything yet, which let me believe this also comes from a missing initialization.
I want to solve the issue, not move all init in the beginning of the main function as a workaround.
My code looks something like this:
#include <queue>
std::queue<Telegram> telegram_in_fifo;
Port *port1 = new IoPort(/* some args */);
void some_function() { // tried calling from Interrupt or from main
// port1 is 0x00 here
port1 = new IoPort(/* some args */);
// now port1 has proper address and accessing internal data works without hardfaults
uint64_t size = telegram_in_fifo.size(); // this is 0xffffffe7
if(size <= fifo_max) {
telegram_in_fifo.push(telegram);
}
}

Checking whether (this == nullptr) in a class method [duplicate]

This question already has answers here:
Checking if this is null
(8 answers)
Closed 4 years ago.
I would like to replace code like this:
if ((obj != nullptr) && obj->is_valid())
with
if (obj->is_valid())
where
class Obj {
bool is_valid() {
if (this == nullptr)
return false;
// some more logic ...
}
...
};
Obviously there are 2 conditions:
obj is always accessed via pointer
Obj::is_valid() is never virtual
This is based on the fact, that a non-virtual method accepts this as its 1-st argument, so that
obj->is_valid();
is rewritten as
Obj::is_valid(obj);
While this code does work as expected with gcc-5.4.0, my question is whether this is a legitimate C++ code, that will be interpreted / optimized correctly by other (older / newer) C++ compilers?
Don't.
if (this == nullptr)
It would not provide any security as calling a member function (method) on a null pointer is undefined behavior in the first place. This is bringing a sensation of security without the security, which is worse than nothing.

how to give a string as parameter to be inserted in a QStringListModel [duplicate]

This question already has answers here:
Why not non-const reference to temporary objects? [duplicate]
(4 answers)
Closed 5 years ago.
am learning QT and am trying to populate a QStringList with a couple of elements that later populate a QListView
my first try from the docu was:
// Create model
model = new QStringListModel(this);
// Make data
List << "Java" << "C++" << "C";
// Populate our model
model->setStringList(List);
// Glue model and view together
ui->listView->setModel(model);
so far so good... I can see my List with all the elements I populate...
now in the same class where am doing that am trying to now defined a function that let me add new elements to the list...
so my 1st idea was defining something like
void MainWindow::addNewLanguage(QString& item)
{
List << item;
model->setStringList(List);
}
but(and here comes my question...) I am only able to call my function by doing
QString x( "Php" );
w1.addNewLanguage( x );
I would like to instead dom something more nice like:
w1.addNewLanguage( "Pascal" );
no need to define a new object of the QString...
but doing that breaks the compilation with the msg
C:\Users\xxx\WorspaceQT\untitled4\main.cpp:25: error: invalid
initialization of non-const reference of type 'QString&' from an
rvalue of type 'QString'
w1.addNewLanguage( "x2" );
^
anything I can do to address this??
thanks! :)
The error message already gives you a great hint:
invalid initialization of non-const reference of type 'QString&' from an rvalue of type 'QString'
Therefore, you should define addNewLanguage as:
void MainWindow::addNewLanguage(const QString& item)
or alternatively:
void MainWindow::addNewLanguage(QString item)
Have a look at this post for an explanation why a non-const reference is not allowed to a temporary object.
Note that the second approach is not (much) slower than the first one as QString is implicitly shared.

std::string values become corrupted in Qt 5 [duplicate]

This question already has answers here:
const reference data member bound to a temporary initializing that reference in a constructor
(2 answers)
Closed 6 years ago.
I am trying to use the Presage text-prediction platform in a Qt5 project but keep getting errors due to std::strings being corrupted.
I have this class providing string contexts for the predictive system:
class SomeClass : public ParentClass
{
public:
SomeClass(const std::string& _past_context) : past_context(_past_context) { }
std::string get_past_stream() const {
return past_context;
}
std::string get_future_stream() const { return empty; }
private:
const std::string& past_context;
const std::string empty;
};
This context is called inside the Presage code like this:
std::string ContextTracker::getToken(const int index) const
{
std::stringstream pastStringStream(context_tracker_callback->get_past_stream());
...
}
If I send past_context to std::out inside the get_past_stream method, it shows the correct string. If I send the results of get_past_stream in the getToken method, I get corrupted data.
UPDATE [2016-07-28]:
To clarify the question and remove the duplicate tag: the issue only happens when using Qt5. Compiling with g++ a test case consisting only of SomeClass and the Presage context caller works fine. It is when using the STL in Qt5 that the strings get corrupted after being used as return values.
After narrowing the possibilities to the passing of std::string as return values in Qt5, I found a similar problem here: 'std::string' has no member named front [closed]
It seems Qt5 must be explicitly configured to use C++11 STL. Thus, adding CONFIG += c++11 solved my issue too.

printing value in constructor and destructor [duplicate]

This question already has answers here:
C++ Strange constructor behaviour
(2 answers)
Closed 8 years ago.
I am stucked on the following code for constructors and destructors which I don't understand the result.one of the value should be 7 and how come there are four numbers? help appreciated.
class Package
{
private:
int value;
public:
Package()
{
value=7;
cout<<value<<endl;
}
Package(int v)
{
value=v;
cout<<value<<endl;
}
~Package()
{
cout<<value<<endl;
}
};
int main()
{
Package obj1(4);
Package obj2();
Package obj3(2);
return 0;
}
in console prompt it displays
4
2
2
4
This statement
Package obj2();
is a function declaration that has no parameters and has return type of Package.
You could write instead
Package obj2 {};
if you want that it would be an object definition.
So you defined only two objects
Package obj1(4);
Package obj3(2);
And correspondingly the constructors print
4
2
The destructor is called in the reverse order relative to called constructors (LIFO - last input first output) and outputs
2
4
Package obj2();
exhibits what is called the Most Vexing Parse problem in C++.
Package obj2();
This line was parsed as "declare a function named obj2 that returns a Package object," not as "declare a variable named obj2 of type Package and construct it with no arguments." This is called the most vexing parse.
Fix for any C++:
Package obj2;
More explicit fix with C++11 syntax:
Package obj2{};