2

I'm trying to use the QVector class from Qt to work (for me :P). What I want to do is to put multiple instances of the object Question in a QVector.

I went on multiple forums, but they're all too complicated for me as I am a beginner. This one post was perfect but I did not find a way to resolve my problem.

So I'm turning to you to help me!

Here's the function that I want to work :

The part that create the bundle/ the vector

/**
 * @brief MenuQuestionnary::assembleQuiz
 * Assemble the bundle of question that will be used in Quiz class
 */
void MenuQuestionnary::assembleQuiz(){
    QVector<Question> vectorQuiz;
    vectorQuiz.reserve(spinBoxNumberOfQuestion->value());
    for(int i = 0; i <= spinBoxNumberOfQuestion->value(); i++){
        vectorQuiz.append(Question((qrand()% maximumNumberOfQuestionAvailable)));
    }
}

Here's my Question constructor :

Question::Question(int id)
{
    this->questionId = id;

    //TODO: Actually get it from DB
    this->questionText = "2+2?";
    this->explanation = "Addition mechanics";
    this->creatorId = 1;

}

What i expect to do here is to put the selected number of the Question object in a vector. After that i can pass it to another class. From there i should be able to extract the text from them(questionText and questionExplanation).

Community
  • 1
  • 1
Chax
  • 1,041
  • 2
  • 14
  • 36
  • You can't radically change your question half-way through. Otherwise you invalidate all the answers that referred to the original instance of the question. – Shoe Feb 02 '14 at 14:19
  • Should i start a new thread instead? All i changed was the int to Question after the declaration of my qvector – Chax Feb 02 '14 at 17:20
  • What's the problem now? – Shoe Feb 02 '14 at 17:22
  • Append does'nt seems to exist in the vector since i declared it was a vector of question. One of my friend told me to use a QList. I'll try when i'll be home. Do you have any suggestion? – Chax Feb 02 '14 at 18:11
  • Can you post the whole code (in the question) with the part where you use the `QVector` and the expected results? – Shoe Feb 02 '14 at 19:52
  • You should post an answer instead of editing your question to add the solution. – Shoe Feb 03 '14 at 19:53
  • Ok thanks, I'm not really used on all the rules here... :S – Chax Feb 03 '14 at 20:01
  • You'll be able to accept it after a couple of days. – Shoe Feb 03 '14 at 20:05

4 Answers4

3

You are trying to push objects of class type Question into a QVector<int>, which obviously is expecting an int instead. You should change it to QVector<Question> to begin with.

What I highly suggest, though, is that you read a good book on C++ before going any further, or your experience with it will just get more and more complicated.

Copy pasting code from forums on the internet is not programming and will get you in troubles soon.

Community
  • 1
  • 1
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • I saw the error i did when posting here. I edited my question by replacing the int by the object name. Plus i do not copy paste code, i tryed what that cageman proposed to me, played with it a bit, but I wasen't able to solve it. – Chax Feb 02 '14 at 14:06
  • 2
    +1 for "Copy pasting code from forums on the internet is not programming" :P – Cool_Coder Feb 02 '14 at 14:53
  • That's why i'm asking for an explanation. Otherwise i try what people suggest me and try to find the explanation by myself. I have read few books on c++, i have a specific problem and i need help. – Chax Feb 02 '14 at 17:26
0

I think what you want is this: QVector vectorQuiz; vectorQuiz.reserve(spinBoxNumberOfQuestion->value()); // reserve correct amount of space in vector for performance (not required). I don't know exactly how you are going to use vectorQuiz, but maybe you should use pointers to the questions i.e. QVector and insert questions using new Question()

/**
 * @brief MenuQuestionnary::assembleQuiz
 * Assemble the bundle of question that will be used in Quiz class
 */
void MenuQuestionnary::assembleQuiz(){
    int iVectorSize = spinBoxNumberOfQuestion->value();
    QVector<Question> vectorQuiz;
    vectorQuiz.reserve(iVectorSize ); 

    for(int i = 0; i <= iVectorSize ; ++i){
        vectorQuiz.append(Question(i));
    }

}
cageman
  • 333
  • 2
  • 10
  • Your code gave me this error : no matching function to call 'Question::Question()' and the error is located in the qvector.h... Plus it says that the qVector does'nt have a function named append... Look like it's doomed :P – Chax Feb 02 '14 at 13:39
  • Seems you didn't define a constructor for Question.. i assume it is a class? – cageman Feb 02 '14 at 13:58
  • The class question is defined. Normally it is working but not when i try to put it in a vector... – Chax Feb 02 '14 at 17:23
0

Your object (vectorQuiz) declared as vector of integers. If you want to add some integer value to it you should write something:

vectorQuiz.append( someIntegerValue );

or

vectorQuiz.push_back( someIntegerValue );

For vector of another type (i.e. Question), write code like this:

QVector<Question> vectorQuiz;
// ...
for(int i = 0; i <= spinBoxNumberOfQuestion->value(); ++i){
    vectorQuiz.append(Question(i)); // without [i] after vector object
}

And possible your should use strict inequality < instead of <= in for-cycle (but I'm not sure).

αλεχολυτ
  • 4,792
  • 1
  • 35
  • 71
0

I solved it by trying few things Here's the explanation of what i did

I've split my function in two. The first one put the element in a QList and the second one shuffles it.

/**
 * @brief MenuQuestionnary::assembleQuiz
 * @param list
 * Asseble a quiz in the Qlist and then ask shuffle to shuffle it...
 */
void MenuQuestionnary::assembleQuiz(QList<Question> &list){
    for(int i = 0; i < spinBoxNumberOfQuestion->value(); ++i){
        int rand = qrand() * maximumNumberOfQuestionAvailable;
        Question newQuestion(rand);
        list.append(newQuestion);
    }
    shuffleQuiz(list);
}

/**
 * Method Shuffle
 * equivalent to shuffling a deck of cards: we take a random one, move it to be the last one,
 * then do it again enough times to have statistically touched every card.
 */
void MenuQuestionnary::shuffleQuiz(QList<Question> &list){
    int iters = list.size() * list.size();
    for (int i = 0; i < iters; ++i){
        int rand = qrand() * list.size();
        list.append(list[rand]);
        list.removeAt(rand);
    }
}

Thanks for the help though.

Chax
  • 1,041
  • 2
  • 14
  • 36