我运行以下C ++代码并收到警告:控制到达非void函数的结尾 [英] I run the following C++ codes and got a warning: control reaches end of non-void function

查看:70
本文介绍了我运行以下C ++代码并收到警告:控制到达非void函数的结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

enum suits
{
    diamond, club, heart, spade
};

class Card
{
private:
    int rank;
    suits suit;
public:
    Card();
    Card(suits, int);
    int getRank() { return rank; }
    suits getSuit() { return suit; }
    void setRank(int rankvalue) { rank = rankvalue; }
    void setSuit(suits suitvalue) { suit = suitvalue; }
};

ostream & operator<<(ostream &, Card);

Card::Card()
{
    rank = 1;
    suit = spade;
}

Card::Card(suits suitvalue, int rankvalue)
{
    rank = rankvalue;
    suit = suitvalue;
}

ostream & operator<<(ostream & out, Card aCard)
{
    switch (int rank = aCard.getRank())
    {
        case 14: out << "Ace"; break;
        case 11: out << "Jack"; break;
        case 12: out << "Queen"; break;
        case 13: out << "King"; break;
        default: out << rank;
    }
    
    switch (suits suit = aCard.getSuit())
    {
        case diamond: out << " of Diamonds"; break;
        case spade: out << " of Spades"; break;
        case heart: out << " of Hearts"; break;
        case club: out << " of Clubs"; break;
    }
    
    return out;
}

class RandomInteger
{
public:
    RandomInteger();
    unsigned int operator() (unsigned int max);
};

RandomInteger::RandomInteger()
{
    srand(time(0));
}

unsigned int RandomInteger::operator()(unsigned int max)

{
    unsigned int rval = rand();
    return rval % max;
}

RandomInteger randomizer;

class Deck
{
    Card cards[52];
    int topCard;
public:
    Deck();
    void shuffle();
    bool isEmpty() { return topCard <= 0; }
    Card draw();
};

extern RandomInteger randomizer;

Deck::Deck()
{
    topCard = 0;
    for (int i = 1; i <= 13; i++)
    {
        Card c1(diamond, i), c2(spade, i), c3(heart, i), c4(club, i);
        cards[topCard++] = c1;
        cards[topCard++] = c2;
        cards[topCard++] = c3;
        cards[topCard++] = c4;
    }
}

Card Deck::draw()
{
    if (!isEmpty())
        return cards[--topCard];
    else
    {
        Card spadeAce(spade, 1);
        return spadeAce;
    }
}

void Deck::shuffle()
{
    random_shuffle(cards, cards+52, randomizer);
}

class Player
{
public:
    Player();
    void print();
    Card draw(Deck &);
    typedef vector<card> cards;
    vector<cards> column;
};

//ostream & operator<<(ostream &, Player&);

Player::Player()
{
    column.push_back(vector<card>());
    column.push_back(vector<card>());
    column.push_back(vector<card>());
    column.push_back(vector<card>());
}

Card Player::draw(Deck & aDeck)
{
    for (int i = 0; i < 4; i++)
        column[i].push_back(aDeck.draw());
    
}

void Player::print()
{
    cout << "Col 1 \t\t Col 2 \t\t Col 3 \t\t Col 4 \n";
    bool more = true;
    for (int j = 0; more; j++)
    {
        more = false;
        for (int i = 0; i < 4; i++)
            if (j < column[i].size())
            {
                cout << column[i][j] << "\t";
                more = true;
            }
            else
                cout << "\t\t";
        cout << endl;
    }
}

int main()
{
    Deck deck;
    deck.shuffle();
    
    Player player;
    player.draw(deck);
    //while (!deck.isEmpty())
    //{
    cout << "Enter a column number (0 to draw four new cards): " << endl;
    //}
    player.print();
    
    int input;
    int i;
    vector<vector<card> > columns(4);
    while (cin >> input)
        if (input == 0 )
        {
            player.draw(deck);
            player.print();
            columns.push_back(vector<card>());
            columns.push_back(vector<card>());
            columns.push_back(vector<card>());
            columns.push_back(vector<card>());
        }
        else while (cin >> input)
            if (input == 1)
            {
                for ( i = 0; i > 4; i++)
                {
                    columns.push_back(vector<card>());
                }
                for ( i = 0; i > 4; i++)
                {
                    columns[0].back().getSuit();
                    columns[1].back().getSuit();
                    columns[2].back().getSuit();
                    columns[3].back().getSuit();
                }
             
            }
    
}





什么我试过了:



i试图把返回0;在我的int main()的末尾,但我仍然得到相同的警告



What I have tried:

i have tried to put return 0; at the end of my int main() but I still get the same warning

推荐答案

这样的警告应该是你最不担心的:你的代码会产生很多错误,从第133行开始:

Such a warning should be the least of your worries: you code produces many errors, starting from line 133:
Quote:

typedef vector< card>卡;

typedef vector<card> cards;

其中'卡'未在此范围内声明



您应首先修复编译器错误,然后专注于警告。

where "‘card’ was not declared in this scope".

You should first fix compiler errors and then focus on the warnings.


你有两个没有返回任何东西的非空函数。第一个是Card Player :: draw(Deck& aDeck),它应该返回一张Card但不会。第二个是main应该返回一个整数。
You have two non-void functions that are not returning anything. The first is Card Player::draw(Deck & aDeck) which should return a Card but it does not. The second is main which should return an integer.


检查你的编译器输出 - 它应该告诉你错误是哪一行:

例子。 cpp:42警告:控制到达非空函数的结束。



但是当我尝试编译它时,我得到了大约50个错误和警告,所以你可能想要仔细查看已发布的代码和编译器输出。



NB。感谢您使用缩进重新发布代码 - 尽管通常不会收到200多行代码。
Check your compiler output - it should be telling you which line the error is on something like:
example.cpp:42 warning: control reaches end of non void function.

But when I try to compile this, I got about 50 errors and warnings, so you might want to take a close look at both code as posted and your compiler output.

NB. Thanks for re-posting the code with indents - though 200+ lines of code are usually not well received.


这篇关于我运行以下C ++代码并收到警告:控制到达非void函数的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆