我不确定我是否正确地声明了这个类,请查看我的代码(用于纸牌游戏),特别是在我构造类和三个访问器的地方。当我尝试使用主函数时,出现错误,它说Error 1 error C2228: left of '.get_rank_string' must have class/struct/union
用于所有三种访问函数。用户定义的类构造函数和函数不起作用,我构建了我的类是否错误?
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
class Card
{
public:
Card();
int get_rank() const;
string get_suite_string() const;
string get_rank_string() const;
private:
int rank; //The rank represents the value of the card
int suite; //The suite represents the type of card
};
Card::Card() //Default constructor setting rank to a random number between 1 and 14 and suite between 1 and 4
{
rank = rand()%14;
suite = rand()%5;
}
int Card::get_rank() const //returns the rank value of the rank of the card
{
return rank;
}
string Card::get_rank_string() const //returns the name of the rank of the card
{
string x;
if (rank == 1)
{
x = "Ace";
}
if (rank == 2)
{
x = "Two";
}
if (rank == 3)
{
x = "Three";
}
if (rank == 4)
{
x = "Four";
}
if (rank == 5)
{
x = "Five";
}
if (rank == 6)
{
x = "Six";
}
if (rank == 7)
{
x = "Seven";
}
if (rank == 8)
{
x = "Eight";
}
if (rank == 9)
{
x = "Nine";
}
if (rank == 10)
{
x = "Ten";
}
if (rank == 11)
{
x = "Jack";
}
if (rank == 12)
{
x = "Queen";
}
if (rank == 13)
{
x = "King";
}
return x;
}
string Card::get_suite_string() const
{
string x;
if (suite == 1)
{
x = "Clubs";
}
if (suite == 2)
{
x = "Hearts";
}
if (suite == 3)
{
x = "Diamonds";
}
if (suite == 4)
{
x = "Spades";
}
return x;
}
int main()
{
string name;
cout << "What is your name? ";
getline(cin, name);
int money;
cout << "How much money would you like to start with? ";
cin >> money;
cout << endl << name << ", you have $" << money << "." << endl;
int sum;
while (sum > 0)
{
Card x();
Card y();
cout << "You got a " << x.get_rank_string() << " of " << x.get_suite_string() << "and a " << y.get_rank_string() << "of" << y.get_suite_string() << "." << endl;
cout << "How much do you want to be the next card is in between? ";
int bet;
cin >> bet;
Card z();
if ((z.get_rank() > x.get_rank() && z.get_rank() < y.get_rank()) || (z.get_rank() < x.get_rank() && z.get_rank() > y.get_rank()))
{
cout << "YES! " << name << " you win $" << bet << endl;
sum = money + bet;
}
else
{
cout << "TOO BAD! " << name << " you lose $" << bet << endl;
money - bet;
}
cout << name << ", you have $" << sum;
}
system("pause");
return 0;
}
太好了,非常感谢你!第一次在stackoverflow,真的很惊讶你们回答这些有多快,感觉很好,有支持!你们真棒(⌐͡■͜ʖ͡■)。 - William Baires 8分钟前 – BrockObama