error: undefined reference to `Guessinggame::score(QList<int>, QList<int>)'
-
I have this error in my class,i cant seems to get my program right:Below is the program
class Guessinggame : public QObject { Q_OBJECT public: explicit Guessinggame(QObject *parent = 0); Guessinggame(int n, int fn, int tn); int getNum()const; int getFromNum()const; int getToNum()const; QList<int> generateNumbers(bool unique); static int score(QList<int> input,QList<int> correct); private: int num; int fromNum; int toNum; //signals: //public slots: }; #endif // GUESSINGGAME_H #include "guessinggame.h" Guessinggame::Guessinggame(QObject *parent) : QObject(parent) { } Guessinggame::Guessinggame(int n, int fn, int tn):num(n), fromNum(fn),toNum(tn){ } int Guessinggame::getNum()const { return num; } int Guessinggame::getFromNum()const { return fromNum; } int Guessinggame::getToNum()const { return toNum; } QList<int> Guessinggame::generateNumbers(bool unique){ QList<int> generatedNumbersList; QTime time = QTime::currentTime(); qsrand((uint)time.msec()); bool generateNextNumber = true; do{ int newNumber = qrand() % ((getToNum() + 1) - getFromNum()) + getFromNum(); if(unique){ if(generatedNumbersList.contains(newNumber) == false){ generatedNumbersList.append(newNumber); } } else{ generatedNumbersList.append(newNumber); } if(generatedNumbersList.size() == getNum()){ //set your generateNextNumber to false here; generateNextNumber=false; } }while(generateNextNumber); return generatedNumbersList; } and the main is: #include<QDebug> #include <QInputDialog> #include<QMessageBox> #include <QApplication> #include <QWidget> //#include<QList> #include "guessinggame.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); //constructor initialization Guessinggame g(6,1,39) ; int continuePlaying = 0; QList<int> generatedNumbers = g.generateNumbers(true); QList<int> userNumberList; do{ //clear the list to start with if(!userNumberList.isEmpty()){ userNumberList.clear(); } bool ok; QString userNumbers = QInputDialog::getText(0, "Play Guessing Game","Enter Six Numbers between 1 and 39:", QLineEdit::Normal, "", &ok); if(ok){ //user clicks ok on the dialog box QStringList listOfNumbers = userNumbers.split(" "); qDebug() << "size is "<< listOfNumbers.length(); int numberofDuplicates = listOfNumbers.removeDuplicates(); //check for duplicate number bool allOk(true); //used to check if user only entered integers on input dialog if(numberofDuplicates <= 0 && listOfNumbers.size() == g.getNum()){ for(int x = 0; x < listOfNumbers.size() && allOk; x ++){ QString StringNumber = listOfNumbers.at(x); userNumberList.append(StringNumber.toInt(&allOk,10)); } if(allOk){ //this checks if user entered int correctGuesses = g.score(userNumberList, generatedNumbers); QString response = QString ("You got %1 numbers correct. Do you want to continue") .arg(correctGuesses); continuePlaying = QMessageBox::question(0, "Play again?", response, QMessageBox::Yes | QMessageBox::No); } } if(!allOk || numberofDuplicates > 0 || listOfNumbers.size() != g.getNum()){ //user did something wrong //write use message here QString numberofDuplicates = QInputDialog::getText(0, "Test another sentence", "Enter 1 for yes and 0 for No: "); continuePlaying = QMessageBox::question(0,"", "errorMessage", QMessageBox::Yes | QMessageBox::No); } }else{ //user clicked cancel on dialigi box continuePlaying = QMessageBox::No; } }while(continuePlaying == QMessageBox::Yes); return 0; }
-
Hi and welcome to devnet,
From the looks of it, you did not implement that method.
-
@QTWhiz said in error: undefined reference to `Guessinggame::score(QList<int>, QList<int>)':
is where a call that function on main cpp
@SGaist did not want to know where you call it but if you implemented it. It's at least not in your provided code. And please use the <code> - tags to make your code more readable.
-
#ifndef GUESSINGGAME_H
#define GUESSINGGAME_H
#include "GuessingGame.h"#include <QDialog>
//#include <QWidget>
#include <QTime>
#include <QList>class GuessingGame
{
public:
GuessingGame(int n, int fn, int tn); //constructor
int getNum() const;
int getFromNum() const;
int getToNum() const;
QList<int> generateNumbers(bool unique);
static int score(QList<int> input, QList<int> correct);private:
int num; // stores number of values to be guessed
int fromNum;
int toNum;};
#endif // GUESSINGGAME_H
#include "GuessingGame.h"
GuessingGame::GuessingGame(int n, int fn, int tn):num(n), fromNum(fn),toNum(tn)
{}
int GuessingGame::getNum()const
{
return num;
}
int GuessingGame::getFromNum()const
{
return fromNum;
}
int GuessingGame::getToNum()const
{
return toNum;
}QList<int> GuessingGame::generateNumbers(bool unique)
{
QList<int> generatedNumbersList;
QTime time = QTime::currentTime();
qsrand((uint)time.msec());
bool generateNextNumber = true;
do{
int newNumber = qrand() % ((getToNum() + 1) - getFromNum()) + getFromNum();
if(unique){
if(generatedNumbersList.contains(newNumber) == false){
generatedNumbersList.append(newNumber);
}
}
else{
generatedNumbersList.append(newNumber);
}
if(generatedNumbersList.size() == getNum()){
//set your generateNextNumber to false here;
generateNextNumber=false;
}
}while(generateNextNumber);
return generatedNumbersList;
}int GuessingGame::score(QList<int> input, QList<int> correct)
{int numberOfCorrectNumbers = 0;
if(!input.isEmpty() && !correct.isEmpty()){
for(int index = 0; index < input.size(); index ++){ //loop, use your own syntax if you want
int number = input.at(index); if(correct.contains(number)){ numberOfCorrectNumbers++; }
}
}
return numberOfCorrectNumbers;
}#include <QDebug>
#include <QInputDialog>
#include <QMessageBox>
#include <QApplication>
#include <QWidget>
#include <QList>
#include "GuessingGame.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//constructor initialization
GuessingGame g(6,1,39) ;
int continuePlaying = 0;
QList<int> generatedNumbers = g.generateNumbers(true);
QList<int> userNumberList;
do{
//clear the list to start with
if(!userNumberList.isEmpty()){
userNumberList.clear();
}
bool ok;
QString userNumbers = QInputDialog::getText(0, "Play Guessing Game","Enter Six Numbers between 1 and 39:", QLineEdit::Normal,
"", &ok);
if(ok){ //user clicks ok on the dialog box
QStringList listOfNumbers = userNumbers.split(" ");
qDebug() << "size is "<< listOfNumbers.length();
int numberofDuplicates = listOfNumbers.removeDuplicates(); //check for duplicate number
bool allOk(true); //used to check if user only entered integers on input dialog
if(numberofDuplicates <= 0 && listOfNumbers.size() == g.getNum()){
for(int x = 0; x < listOfNumbers.size() && allOk; x ++){
QString StringNumber = listOfNumbers.at(x);
userNumberList.append(StringNumber.toInt(&allOk,10));
}
if(allOk){ //this checks if user entered
int correctGuesses = g.score(userNumberList, generatedNumbers);
QString response = QString ("You got %1 numbers correct. Do you want to continue")
.arg(correctGuesses);
continuePlaying = QMessageBox::question(0, "Play again?", response, QMessageBox::Yes | QMessageBox::No);
}
}
if(!allOk || numberofDuplicates > 0 || listOfNumbers.size() != g.getNum()){ //user did something wrong
//write use message here
QString numberofDuplicates = QInputDialog::getText(0, "Test another sentence", "Enter 1 for yes and 0 for No: ");
continuePlaying = QMessageBox::question(0,"", "errorMessage", QMessageBox::Yes | QMessageBox::No);
}
}else{ //user clicked cancel on dialigi box
continuePlaying = QMessageBox::No;
}
}while(continuePlaying == QMessageBox::Yes);
return 0;
} -
@D-Mputu : I won't take a look until you fix your post to use the <code> tags to make it readable...