Compile Error

This code compiles fine in Windows using strcpy_s however in Linux/Unix I get errors. What can I do to fix this and make it go through?

Song.cpp
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
#include "Song.h"


Song::Song()
{
}


Song::~Song()
{
}


Song::Song(char* title, char* artist, char* duration, char* album)
{
	strcpy_s(this->title,50,title);
	strcpy_s(this->artist, 50, artist);
	strcpy_s(this->duration, 50, duration);
	strcpy_s(this->album, 50, album);
}

void Song::setTitle(char* title)
{
	this->title= title;
}
void Song::setArtist(char* artist)
{
	this->artist = artist;
}
void Song::setDuration(char* duration)
{
	this->duration= duration;
}
void Song::setAlbum(char* album)
{
	this->album= album;
}

char* Song::getTitle()
{
	return this->title;
}
char* Song::getArtist()
{
	return this->artist;
}
char* Song::getDuration()
{
	return this->duration;
}
char* Song::getAlbum()
{
	return this->album;
}


Song.h
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cctype>

using namespace std;

class Song
{
private:
	char* title;
	char* artist;
	char* duration;
	char* album;

public:
	Song();
	~Song();

	Song(char* title, char* artist, char* duration, char* album);

	void setTitle(char* title);
	void setArtist(char* artist);
	void setDuration(char* duration);
	void setAlbum(char* album);

	char* getTitle();
	char* getArtist();
	char* getDuration();
	char* getAlbum();
	

};



Here is what I am getting for errors..

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
g++  -Wall -g -std=c++11  -c -o Song.o Song.cpp
Song.cpp: In constructor ‘Song::Song(char*, char*, char*, char*)’:
Song.cpp:17:29: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermiss       ive]
  strcpy(this->title,50,title);
                             ^
Song.cpp:17:29: error: too many arguments to function ‘char* strcpy(char*, const        char*)’
In file included from /usr/local/include/c++/5.2.0/cstring:42:0,
                 from Song.h:4,
                 from Song.cpp:1:
/usr/include/string.h:125:14: note: declared here
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^
Song.cpp:18:33: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermiss       ive]
  strcpy(this->artist, 50, artist);
                                 ^
Song.cpp:18:33: error: too many arguments to function ‘char* strcpy(char*, const        char*)’
In file included from /usr/local/include/c++/5.2.0/cstring:42:0,
                 from Song.h:4,
                 from Song.cpp:1:
/usr/include/string.h:125:14: note: declared here
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^
Song.cpp:19:37: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermiss       ive]
  strcpy(this->duration, 50, duration);
                                     ^
Song.cpp:19:37: error: too many arguments to function ‘char* strcpy(char*, const        char*)’
In file included from /usr/local/include/c++/5.2.0/cstring:42:0,
                 from Song.h:4,
                 from Song.cpp:1:
/usr/include/string.h:125:14: note: declared here
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^
Song.cpp:20:31: error: invalid conversion from ‘int’ to ‘const char*’ [-fpermiss       ive]
  strcpy(this->album, 50, album);
                               ^
Song.cpp:20:31: error: too many arguments to function ‘char* strcpy(char*, const        char*)’
In file included from /usr/local/include/c++/5.2.0/cstring:42:0,
                 from Song.h:4,
                 from Song.cpp:1:
/usr/include/string.h:125:14: note: declared here
 extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
              ^
make: *** [Song.o] Error 1
This code compiles fine in Windows using strcpy_s however in Linux/Unix I get errors.


It may compile fine, but it's just as wrong. In order to write something to a location where a pointer points, there must be memory we own at that location. Why don't you use std::string?
This is for an assignment and I am not allowed to use string at all, I can only use cstring.
strcpy_s is not std c++, you can use strncpy replace it
@z406361...
What that is closest to strcpy_s is strcpy, not strncpy.
What that is closest to strcpy_s is strcpy, not strncpy.


Both will do the job, but strncpy is a bit safer.
Anyway the most important thing is to allocate the memory in the constructor and delete it in the destructor.
Topic archived. No new replies allowed.