0

I'm stuck with error message deprecated conversion from string constant to 'char*'

What I tried to do here is to assign "First", "Last" to cfoo1 and make cfoo2 equal to cfoo1. Lastly, display cfoo1 and cfoo2 to standard output.

#include <iostream>
#include <cstring>
#include "cfoo.h"

using namespace std;

CFoo :: CFoo(char first[], char last[]){

    m_first[BUF] = first[BUF];
    m_last[BUF] = last[BUF];
}

void CFoo :: WriteFoo(){

    cout << m_first[BUF] << ", " << m_last[BUF];
}



#ifndef CFOO_HEADER
#define CFOO_HEADER

#include <iostream>
#include <cstring>

using namespace std;

const int   BUF = 256;

class   CFoo{

    public:
        CFoo(char first[], char last[]);

        void WriteFoo();

    private:
        char    m_first[BUF];
        char    m_last[BUF];
};

#endif


#include <iostream>
#include "cfoo.h"

using namespace std;

int main(){

    CFoo    foo1("Jong", "Yoon");
    CFoo    foo2 = foo1;

    cout << "foo1 = ";
    foo1.WriteFoo();
    cout << endl;

    cout << "foo 2 = ";
    foo2.WriteFoo();
    cout << endl;

    return 0;
}
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
pooya13
  • 1
  • 2
  • 1
    You cannot modify string literals. Using a `char *` instead of `const char *` to point to them has no merit and the class might as well use `std::string` in the first place. – chris Sep 02 '14 at 03:15
  • Related: [C++ warning: deprecated conversion from string constant to char*](http://stackoverflow.com/questions/21529194/c-warning-deprecated-conversion-from-string-constant-to-char-wwrite-stri) ... maybe dup. – Shafik Yaghmour Sep 02 '14 at 03:16
  • The assignments and printing also don't do what you seem to think they do. – T.C. Sep 02 '14 at 03:23

1 Answers1

1

There are two issues:

  1. Using string literals (which are of type char const*) to call a function that expects char[].

  2. Trying to assign to char arrays.

Fixes:

  1. Change the constructor to:

    CFoo(char const* first, char const* last);
    
  2. Change its implementation to:

    CFoo(char const* first, char const* last)
    {
      // Make sure to copy at most BUF-1 characters
      // to m_first and m_last.
    
      m_first[0] = '\0'
      strncat(m_first, first, BUF-1);
    
      m_last[0] = '\0'
      strncat(m_last, last, BUF-1);
    }
    

You also need to change the implementation of CFoo::WriteFoo() to use the entire string

void CFoo::WriteFoo()
{
    cout << m_first << ", " << m_last;
}

Also,

Accessing m_first[BUF] or m_last[BUF] is an error since the maximum value of a valid index to access those arrays is BUF-1.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    it'd be wise to check the lengths before doing `strcpy`. – M.M Sep 02 '14 at 06:15
  • Oh other question. In main.cpp, I tried to change CFoo foo2 = foo1; to const CFoo foo2 = foo1; I get error passing 'const CFoo' as 'this' argument of 'void CFoo::WriteFoo()' discards qualifiers. can you explain about this error? – pooya13 Sep 03 '14 at 00:19
  • @pooya13, change `WriteFoo()` to a `const` function. Make that a habit. Any time you write a member function that does not modify the object, make it a `const` member function. – R Sahu Sep 03 '14 at 01:43