2 Replies - 248 Views - Last Post: 15 February 2012 - 12:04 PM Rate Topic: -----

#1 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 125
  • Joined: 24-January 12

Using char* in a class, compiler warnings

Posted 15 February 2012 - 11:57 AM

Finally got down to starting with classes and have a question.
The following code does exactly what I expect it to do but it generates 17 warnings, one for each of my char* elements.
C:\working\poker with classes\main.cpp|26|warning: deprecated conversion from string constant to 'char*'|



I'm going to take a guess that char* shouldn't be used the way I am using it and would appreciate any guidance the gurus might offer.

here is teh codez!

#include <iostream>

using namespace std;

class CardClass{ // class declaration

    private: // access specifier, these variables only available in class
        int face;
        int suit;

    public: // access specifier, function can be reached from outside the class
        void showCard(int card_value){
            setFace(card_value);
            setSuit(card_value);
            displayCard();
            }

    private: // access specifier, functions can be reached from inside the class
        void setFace(int card_value){
            face = card_value % 13; // 0 to 52 yields a number from 0 to 12
        }
        void setSuit(int card_value){
            suit = card_value / 13; // 0 to 52 yields a number from 0 to 3
        }
        void displayCard(){
            char* suits[4] = {"Hearts", "Clubs", "Diamonds", "Spades"};
            char* faces[13] = {"Two", "Three", "Four", "Five", "Six", "Seven",
                  "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
            cout << faces[face] << " of " << suits[suit];
        }

};

int main()
{

    int players_hand[5] = {38, 25, 6, 32, 50}; // some valid test data (0 to 51)

    CardClass cardObject; // create an object

    for(int i = 0; i < 5; ++i){ // loop through the hand
        // access the class, passing in the value of the card we want to show
        cardObject.showCard(players_hand[i]);
        cout << endl;
    }

    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Using char* in a class, compiler warnings

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Using char* in a class, compiler warnings

Posted 15 February 2012 - 12:01 PM

Make the array a const char* array. You initialize the array with temporary character arrays therefore you can't modify those arrays. By declaring the array as just 'char*' you're telling the compiler that at some point, your code could change the array.

*NOTE*: Count the number of array elements, 13 + 4, and you get the same number of warnings. Also the key words in the warning is "string constant".

This post has been edited by vividexstance: 15 February 2012 - 12:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 Bryston  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 125
  • Joined: 24-January 12

Re: Using char* in a class, compiler warnings

Posted 15 February 2012 - 12:04 PM

View Postvividexstance, on 15 February 2012 - 12:01 PM, said:

Make the array a const char* array. You initialize the array with temporary character arrays therefore you can't modify those arrays. By declaring the array as just 'char*' you're telling the compiler that at some point, your code could change the array.

*NOTE*: Count the number of array elements, 13 + 4, and you get the same number of warnings. Also the key words in the warning is "string constant".


the awesomeness of D.I.C. never ceases to amaze me, many thanks.

Compiling: main.cpp
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1