2015-11-19 62 views
1

我不确定我是否正确地声明了这个类,请查看我的代码(用于纸牌游戏),特别是在我构造类和三个访问器的地方。当我尝试使用主函数时,出现错误,它说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; 
} 
+0

太好了,非常感谢你!第一次在stackoverflow,真的很惊讶你们回答这些有多快,感觉很好,有支持!你们真棒(⌐͡■͜ʖ͡■)。 - William Baires 8分钟前 – BrockObama

回答

0

变化Card x();Card x;声明一个默认的构造变化。

+0

真棒,你知道最新的其他三项功能,但他们都显示为错误,当我尝试使用它们... – BrockObama

+0

编辑你的问题,包括错误。 –

+0

在x,y和z之后删除括号后,我能够得到一个干净的编译。 –

0

你实例化对象为:

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

是不正确

1
Card x(); 

Card y(); 

实际上并没有声明一个名为x和秒,但声明了一个名为xy该带什么,并返回一个Card两种功能。要修复它只是将其更改为:

Card x, y; 
0

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

被解释为一个函数,调用它的方法是不合法的,这就是为什么你得到这些错误。您正在尝试做一些事情

void foo(); 
foo.get_rank(); 

.运营商期望在其留下了类/联合/结构类型,而在代码中发现的功能。

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

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

+0

仅限于if语句到目前为止,目前介绍的过程,但请详细说明返回字符串,不知道我是否做了correclty。 – BrockObama

相关问题