strcpy() problem

closed account (ETAkoG1T)
When I compile the code bellow I get this error:

error C4996: 'strcpy': this function or variable may be unsafe consider using strcpy_s instead....

strcpy does not work with char * or so it looks like. What is the problem with my code and what is the solution?

There should be no need to see any other code than the header file, the errors have nothing to do with my main().

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#ifndef CLASSIC_H_
#define CLASSIC_H_
#include <iostream>

// base class
class Cd 
{ // represents a CD disk
private:
	char *performance;
	char *label;
	int selections;
	double playtime;
public:

	// constructors

	Cd(char * s1 = "0", char * s2 = "0", int n = 0, double x = 0)
	{
		performance = new char[strlen(s1) + 1];
		label = new char[strlen(s2) + 1];

		strcpy(performance, s1);
		strcpy(label, s2);

		selections = n;
		playtime = x;
	}

	Cd(const Cd & d)
	{
		performance = new char[strlen(d.performance) + 1];
		label = new char[strlen(d.label) + 1];

		strcpy(performance, d.performance);
		strcpy(label, d.label);

		selections = d.selections;
		playtime = d.playtime;
	}

	virtual ~Cd() { delete [] performance; delete [] label; }

	virtual void Report() const // reports all CD data
	{
		std::cout << "performance: " << performance << std::endl;
		std::cout << "label: " << label << std::endl;
		std::cout << "selections: " << selections << std::endl;
		std::cout << "playtime: " << playtime << std::endl;
	}

	virtual Cd & operator=(const Cd & d)
	{
		if(this == &d)
			return *this;

		delete [] performance;
		delete [] label;

		performance = new char[strlen(d.performance) + 1];
		label = new char[strlen(d.label) + 1];

		strcpy(performance, d.performance);
		strcpy(label, d.label);

		selections = d.selections;
		playtime = d.playtime;

		return *this;
	}
};

class Classic : public Cd
{
private:
	char *mainfeature;
public:

	Classic() { }

	Classic(char * s, char * s1, char * s2, int n, double x) : Cd(s1, s2, n, x)
	{
		mainfeature = new char[strlen(s) + 1];
		strcpy(mainfeature, s);
	}

	~Classic() { delete [] mainfeature; }

	void Report() const
	{
		Cd::Report();
		std::cout << "mainfeature: " << mainfeature << std::endl;
	}
};

#endif 
Main problem is using Microsoft compiler with default settings, swich threats use of almost all standard null-terminated string related functions as errors and trying to force use of it's own non-portable functions.

Best course of action is to turn off said behavior in compiler preferences. And ise strncpy() instead too it is safer and can save you in some cases (iI do not know if MSVC will threat that as eror too)
Topic archived. No new replies allowed.