0

Trying to access a struct member in class Hand with the following error: error: C2228: left of '.colorBet' must have class/struct/union

hand.h

    #ifndef HAND_H
#define HAND_H

#include"deck.h"
#include <QVector>
#include <QString>

struct street
{
    QString colorBet;
    int colorBetSize;
    int payout;
};

class Hand
{
    QVector<card> cardVector;

public:

    Hand(QVector<card> vCards);

    bool isFlush();
    bool isAllRed();
    bool isAllBlack();

    street street1;
    street street2;
    street street3;

};

#endif // HAND_H

mainwindow.cpp (error near the bottom)

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "hand.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::build_radios(){
    street1BetBlack = new QRadioButton("Black");
    connect(street1BetBlack, SIGNAL(clicked()),
              this, SLOT(street1BetBlackClicked()));
    street2BetBlack = new QRadioButton("Black");
    connect(street2BetBlack, SIGNAL(clicked()),
              this, SLOT(street2BetBlackClicked()));
    street3BetBlack = new QRadioButton("Black");
    connect(street3BetBlack, SIGNAL(clicked()),
              this, SLOT(street3BetBlackClicked()));


    street1BetRed = new QRadioButton("Red");
    connect(street1BetRed, SIGNAL(clicked()),
              this, SLOT(street1BetRedClicked()));
    street2BetRed = new QRadioButton("Red");
    connect(street2BetRed, SIGNAL(clicked()),
              this, SLOT(street2BetRedClicked()));
    street3BetRed = new QRadioButton("Red");
    connect(street3BetRed, SIGNAL(clicked()),
              this, SLOT(street3BetRedClicked()));

    st1bg = new QButtonGroup;
    st2bg = new QButtonGroup;
    st3bg = new QButtonGroup;


    // button groups
    st1bg->addButton(street1BetBlack);
    st1bg->addButton(street1BetRed);
    st1bg->setExclusive(true);

    st2bg->addButton(street2BetBlack);
    st2bg->addButton(street2BetRed);
    st2bg->setExclusive(true);

    st3bg->addButton(street3BetBlack);
    st3bg->addButton(street3BetRed);
    st3bg->setExclusive(true);


}
void MainWindow::street1BetRedClicked()
{
    Hand::street1.colorBet="Red";        //error on every line similar to this
}
void MainWindow::street2BetRedClicked()
{
    Hand::street2.colorBet="Red";
}
void MainWindow::street3BetRedClicked()
{
    Hand::street3.colorBet="Red";
}
void MainWindow::street1BetBlackClicked()
{
    Hand::street1.colorBet="Black";
}
void MainWindow::street2BetBlackClicked()
{
    Hand::street2.colorBet="Black";
}
void MainWindow::street3BetBlackClicked()
{
    Hand::street3.colorBet="Black";
}
chuckieDub
  • 1,767
  • 9
  • 27
  • 46

3 Answers3

2

Hand::street1.colorBet="Red"; & the others are illegal because Hand::street1 isn't a static class member (I'm not saying it should, having static members should be a logic decision, not something you do to make the code compile).

What Hand object are you attempting to modify in, say, street1BetRedClicked? Do you have any Hand objects? Do you need any Hand objects? Do you create them in that method? Outside?

I pointed out the error, but it seems to me that the important issue here isn't the fact that the code doesn't compile, but that:

  1. You don't seem to have a firm grasp on C++, perhaps you should start with something simpler.

  2. You don't have a clear logic set up.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • First of all yes I don't have a firm grasp, I am doing this to learn. I am using hand objects to analyze the entire hand, as in hand.isFlush. Maybe street should be a class and not a struct – chuckieDub Jan 01 '13 at 20:43
  • 1
    @user1846123 I suggest you start off by learning C++, and begin QT afterwards. The best place to do this is by reading an introductory book. – Luchian Grigore Jan 01 '13 at 20:45
  • @user1846123 it doesn't matter if it's class or struct. You speak of `hand`, but you don't have **any** hands in the code. You just defined the class, but didn't create any objects. How, then, do you expect to use hands? – Luchian Grigore Jan 01 '13 at 20:45
  • when the deal is made, the cards that are dealt will be defined as a hand. So 'Hand hand1(QVector);'. then I can run 'hand1.isFlush()' etc. I am now trying to implement the bets, which are bet on each card/street that is dealt. I suppose 'hand1.street1.colorBet' makes sense. I am having difficulty with setting up the logic for this. as you can tell, I am fairly new to programming. – chuckieDub Jan 01 '13 at 20:59
  • 1
    @user1846123 so drop Qt for now and grab a book... Why aren't you taking sound advice? – Luchian Grigore Jan 01 '13 at 21:04
  • Thanks I have c++ primer I will finish going through it – chuckieDub Jan 01 '13 at 21:14
1

When you access street1 like this, street1 should be a static field of Hand.

If every Hand can have its own street1 object, you should access the object's street1 field and not the static field. If you want all instances of Hand to share the same street1, you should make street1 static.

That you don't understand these concepts and from reading your other questions (example), I can tell that you don't know C++ sufficiently to write this program. I would advise you to first read a book on C++ or learn C++ in some other way, before you start working on your own projects.

Example:

Hand hand1 = getRandomHand();
Hand hand2 = getRandomHand();

hand1.street1.colorBet="Red";
hand2.street1.colorBet="Black";
Community
  • 1
  • 1
lesderid
  • 3,388
  • 8
  • 39
  • 65
0

You have to declare an object of type Hand:

Hand hand;
hand.street1;
...

You can also declare the street fields as static.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
brooc
  • 448
  • 2
  • 6
  • 10