0

Im attempting to implement strcpy or strncpy and both are showing an error no matter which i use.

The error is only under strncpy and strcpy

Item.cpp:

#include "Item.h"
#include <iomanip>
#include <iostream>
#include <ctime>
#include <string>
using namespace std;
#include "Date.h"
#include "POS.h"
#include "PosIO.h"

namespace sict {
    Item::Item(){
         _name = '\0';
         _price = 0;
         _taxed ='0';
         _quantity = '\0';
    }

    Item::Item(const char* sku,  const char * name, double price, bool taxed){

        strNcpy(_sku, sku, MAX_SKU_LEN);

         name = new char[20];
        strcpy(_name, name);

        _quantity = 0;
        price = _price;
        if (price = '\0') {
            _taxed = true;
        }
    }
    void Item::sku(const char* value){
        strncpy(_sku, value);

    }
    void Item::price(double p){
        p = _price;
    }
    void Item::name(const char * n){
        strcpy(_name, n);

    }



}

Any idea how to fix it, ive excluded alot of code from item.cpp thats irrelevant.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Andrew Barsoom
  • 81
  • 1
  • 3
  • 7

3 Answers3

7

Both strcpy and strncpy are declared in the cstring header. You need to include it in order to use the functions:

#include <cstring>
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • Extra note - `#include ` in C++ gets you the `std::string` and related classes, whereas `#include ` (note the `.h`) in C gets you `strcpy` etc. I honestly don't remember if it's standard-compliant to `#include in C++ - it's certainly bad practice, and works because most C++ compilers are also packaged with C compilers as well as (or maybe not) because of standards-provided guarantees. There's a general rule going from C to C++ to chop off the `.h` extension and add a `c` prefix, hence ``, `` etc. –  Nov 24 '15 at 18:37
  • Actually, the top answer for that states that *is* compliant - "The C++ Standard library provides all standard C headers for compatibility reason" etc etc, with a chunk of the standard reproduced including the 25 C headers (with `.h`) that are supported, `` among them. –  Nov 24 '15 at 19:01
  • Doesn't work. Tried it – Andrew Barsoom Nov 24 '15 at 19:26
  • 1
    Define "doesn't work". – Emil Laine Nov 24 '15 at 20:18
  • since the update https://stackoverflow.com/posts/33901011/revisions the question contains `#include `, then the post becomes confusing... – mpromonet Jul 07 '19 at 14:55
-1

Using std::strcpy(); will solve your issue.

Zaed
  • 9
  • 2
-1

There are two ways to solve this problem, either by including the appropriate header which is #include <cstring> or put std:: in front of the function.

Also, I have noticed that you write strNcpy which can lead to a syntax error.

Tor
  • 784
  • 1
  • 13
  • 25
Zai
  • 1