PDA

View Full Version : Problem with method call inside a constructor.



robgeek
6th April 2015, 17:15
Good day!

I created a class which, at the moment, i have one method beside the constructor/destructor:

//signature
"QVector<int> insertNeatly(int num, QVector<int> raffle, int idRfl)"

In the constructor i'm trying to call this method but i'm having the following error message, but first, the part of my code:

//...code in the constructor
//...
QVector< int > raffle(idRfl, 0);
for(int i = 0; i < idRfl; i++)
{
//Error here-> raffle = insertNeatly(line.at( i ).toInt( ), raffle, idRfl);//"line" is a stringlist.
}
//...

Error message:

D:\Programming\C-C++\Qt\Windows\historic.cpp:26: error: undefined reference to `Historic::insertNeatly(int, QVector<int>, int)'

I don't know if i'm right but i believe, since this code are in constructor, i cannot call other method of this class in it. Is that right?

If i'm right, what do you think i create another class just to put this method so i can call him in my constructor? Do you have any other idea?

Thanks!

anda_skoa
6th April 2015, 18:19
I don't know if i'm right but i believe, since this code are in constructor, i cannot call other method of this class in it. Is that right?

No, you can call any method of a class inside its constructor.

Your error is also not a compiler error (which would indicate something C++ doesn't allow), it is a linker error.

undefined reference applied on a method usually means that this method is missing its implementation.
I.e. it is declared (e.g. in the header) but not implemented

Cheers,
_

robgeek
6th April 2015, 19:11
As i showed here the signature you can see i forgot the "Class::", now is working fine.

Thank you!

Kryzon
6th April 2015, 23:58
You're passing and returning that QVector by value, so it's being copied each time the loop iterates. Unless that is the desired behaviour, passing it as a pointer or reference should make that loop faster.

"QVector<int>& Historic::insertNeatly(int num, QVector<int>& raffle, int idRfl)"
Then the call to it would remain: raffle = insertNeatly( line.at( i ).toInt( ), raffle, idRfl );

EDIT: QVector actually uses "implicit sharing," so you're copying references to the same data. I think using the same reference will prevent it from being copied-on-write as inside that loop you're using, but I'd wait for the opinion of someone more experienced.
http://doc.qt.io/qt-5/implicit-sharing.html#list-of-classes

anda_skoa
7th April 2015, 08:10
EDIT: QVector actually uses "implicit sharing," so you're copying references to the same data. I think using the same reference will prevent it from being copied-on-write as inside that loop you're using, but I'd wait for the opinion of someone more experienced.
http://doc.qt.io/qt-5/implicit-sharing.html#list-of-classes

The vector detaches (creates a full or deep copy) when it has more than one reference (the case here when passed by value) and any of its non-const methods is called.
So if the function only calls const methods, no data copying will take place.

In Qt methods containers like that are usually passed by const-reference into functions and returned by value.

In this special case, where the input is the result, i.e. the input container is effectively the one being inserted into, it would make most sense to make the argument an input/output parameter (using pointer or reference as suggested) and not have a return value at all.

Cheers,
_