vendredi 13 septembre 2019

Non-member variable and Exception issues

I am currently studying C++ in my course. I am working on an assignment and is running into some issues.

Prior to me submitting this, I had about 5 or so other issues which I was able to debug, but these ones I am struggling to fix. Apologies if there have been solutions to this already, but I can't seem to find what I am looking for.

I am still trying to understand how to work with non-member variables and exceptions (just learned it recently), as they are somewhat different to Java.

Thanks for any help

Header File

#ifndef ASSIGN2_QWIRKLE_H
#define ASSIGN2_QWIRKLE_H

class qwirkle {
public:

    qwirkle(bool quitProg);
    bool mainMenu(bool quitProg);
    void newGame();
    void loadGame();
    void showInfo();

private:

    bool    quitProg; // To check if the user wants to quit the program
    bool    pickedOption;
    int     userOption;
};

#endif // ASSIGN2_QWIRKLE_H

cpp file

#include "qwirkle.h"
// #include "LinkedList.h"

#include <iostream>
#include <stdexcept>

using std::cout;
using std::cin;
using std::endl;
using std::exception;
using std::cerr;

#define EXIT_SUCCESS       0

qwirkle::qwirkle(bool quitProg) {
   this->quitProg = false;
}

int main (int argc, char** argv) {
   // LinkedList* list = new LinkedList();
   // delete list;

   std::cout << "TODO: Implement Qwirkle!" << std::endl;

   while (!this->quitProg) {
      qwirkle::mainMenu(this->quitProg);
   }

   return EXIT_SUCCESS;
}

bool qwirkle::mainMenu(bool quitProg) {
   pickedOption = false;

   while (!pickedOption) {
      std::cout << "Main Menu" << std::endl;
      std::cout << "---------------------------" << std::endl;
      std::cout << "1. New Game" << std::endl;
      std::cout << "2. Load Game" << std::endl;
      std::cout << "3. Show Student Information" << std::endl;
      std::cout << "4. Quit" << std::endl;
      std::cout << "---------------------------" << std::endl;

      try {
         std::cin >> userOption;
         if ((!userOption => 1) && (userOption <= 4)) {
            throw new outOfRangeException("[Invalid Option!]");
         }else if (userOption == 1) {
            std::cout << "Starting a new game" << std::endl;
            newGame();
         } else if (userOption == 2) {
            std::cout << "Loading an existing game" << std::endl;
            loadGame();
         } else if (userOption == 3) {
            showInfo();
         } else if (userOption == 4) {
            this->quitProg = true;
         }
      } catch (outOfRangeException e) {
         std::cerr << e.what();
      }
   }
   return quitProg;
}

Errors

qwirkle.cpp: In function ‘int main(int, char**)’:
qwirkle.cpp:29:12: error: invalid use of ‘this’ in non-member function
    while (!this->quitProg) {
            ^~~~
qwirkle.cpp:30:25: error: invalid use of ‘this’ in non-member function
       qwirkle::mainMenu(this->quitProg);
                         ^~~~
qwirkle.cpp:51:23: error: expected type-specifier before ‘outOfRangeException’
             throw new outOfRangeException("[Invalid Option!]");
                       ^~~~~~~~~~~~~~~~~~~
qwirkle.cpp:63:16: error: ‘outOfRangeException’ does not name a type
       } catch (outOfRangeException e) {
                ^~~~~~~~~~~~~~~~~~~
qwirkle.cpp:64:23: error: ‘e’ was not declared in this scope
          std::cerr << e.what();

Aucun commentaire:

Enregistrer un commentaire