用户定义的类构造函数和函数不起作用,我是否错误地构造了我的类?

时间:2015-11-19 19:58:00

标签: c++ class

我不确定我是否正确地宣布了课程,请查看我的代码(用于纸牌游戏),特别是在我制作班级构造函数和三个访问者的地方。当我尝试使用main中的函数时出现错误,它为所有三个访问器函数都显示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;
}

4 个答案:

答案 0 :(得分:1)

Card x();

Card y();

实际上并未声明两个名为Cardx的{​​{1}},但声明了两个名为yx的函数,它们不带任何内容并返回{{ 1}}。要修复它,只需将其更改为:

y

答案 1 :(得分:0)

将对象实例化为:

Card x, y, z;
Card x();
Card y();
Card z(); 

不正确

答案 2 :(得分:0)

Card x();更改为Card x;以声明默认的构造变量。

答案 3 :(得分:0)

问题是C ++解析器将Card x()解释为名为x的函数的声明,该函数返回Card并且不接受任何参数。

被解释为一个函数,在它上面调用一个方法是不合法的,这就是你得到这些错误的原因。你正在尝试做某事

void foo();
foo.get_rank();

但是.运算符在其左侧需要类/ union / struct 类型,而在代码中它会找到一个函数。

您必须以不同的方式初始化对象:

Card x;
Card x{}; // C++11 only
Card x = Card();

其他值得注意的事情:如果你返回一个string,你会按值返回,但在你的情况下,你有一个无法改变的字符串,你应该返回一个const string&。此外,不需要使用if / else链,您可以使用switch语句甚至更好的查找数组,例如:

const string& get_suite_rank() {
  static const string suites[] = {"", "Clubs", "Hearts", "Diamonds", "Spades"};
  return suites[suite];
}

请注意,第一个元素为空,因为您从套件1开始,而不是0