I have to programm some aspects of the game Skat for class. Knowing the game/the game's rules isn't important for task though. (I don't know it either.)
I haven't finished the programm and just wanted to run a simple test, but am already getting an error, that I can't seem to fix. Basically I create a new object of the class "SkatGame" called "one", and then I want to output all cards in the "deck" of "one" to the console. However that doesn't work and the error seems to indicate that "one" is no object of a class, i don't know why though.
I copied my whole code, but highlighted the important parts i though to be important (for lack of knowledge of a better tool) via the comments at the side. Any help would be appreciated.
main.cpp-File
#include <iostream>
#include <string>
#include <array>
#include <vector>
#include "Header.h"
int main() {
SkatGame one();
one.toString(); //error in this line
}
Header.h-File
#pragma once
#include <string>
#include <array>
enum Suit { KARO = 0, HERZ, PIK, KREUZ };
enum Face { SIEBEN = 0, ACHT, NEUN, ZEHN, BUBE, DAME, KOENIG, ASS };
int const skatSize = 2;
int const deckSize = 32;
int const playerSize = 10;
class Card {
public:
Card();
Card(Suit, Face);
Suit getSuit();
Face getFace();
std::string toString();
private:
Suit suit;
Face face;
};
class SkatGame {
public:
SkatGame(); //!important line
void shuffle();
void deal();
std::string toString();
std::array <Card, skatSize> getSkat();
private:
std::array <Card, deckSize> deck;
std::array <Card, playerSize> player1;
std::array <Card, playerSize> player2;
std::array <Card, playerSize> player3;
std::array <Card, skatSize> skat;
};
detailed class-definitions file (.cpp)
#include "Header.h"
#include <iostream>
#include <string>
#include <array>
//#include <stdlib.h>
Card::Card() {};
Card::Card(Suit gesinnung, Face Zahl)
: suit(gesinnung), face(Zahl)
{};
Suit Card::getSuit() {
return Card::suit;
}
Face Card::getFace() {
return Card::face;
}
std::string Card::toString() { //!important part begin
std::string zurueck;
zurueck = std::to_string(Card::suit) + " " + std::to_string(Card::face);
return zurueck;
} //!important part end
SkatGame::SkatGame()
{
int a, b, e = 0;
for (a = 0; a <= 3; a++) {
for (b = 0; b <= 7; b++) {
SkatGame::deck[e] = Card(static_cast<Suit>(a), static_cast<Face>(b));
e++;
}
}
}
void SkatGame::shuffle() {
int a, card1, card2;
Card hold;
for (a = 0; a < 1000; a++){
card1 = rand() %32;
card2 = rand() %32;
hold = SkatGame::deck[card1];
SkatGame::deck[card1] = SkatGame::deck[card2];
SkatGame::deck[card2] = hold;
}
}
std::string SkatGame::toString() { //!important part begin
int i;
for (i = 0; i <= 31; i++) {
std::cout << (SkatGame::deck[i]).toString() << std::endl;
};
return "justatest";
} //!important part end
EDIT: Removing the brackets behind one during the declaration worked. So "SkatGame one;" instead of "SkatGame one();".