Error c2664 in VS 2013 C++
So, I have a class called "music" and when I try to compile the code, it gives me the following errors:
Error 13 error C2664: 'music::music(const music &)' : cannot convert argument 2 from 'const char [5]' to 'char' c:usersandrei bordeianudocumentsvisual studio 2013projectsmediaplusmediaplussource.cpp 362 1 MediaPlus
14 IntelliSense: no instance of constructor "music::music" matches the argument list argument types are: (const char [5], const char [5], const char [4], const char [12], int, const char [8], const char [5], int) c:UsersAndrei BordeianuDocumentsVisual Studio 2013ProjectsMediaPlusMediaPlusSource.cpp 361 11 MediaPlus
This is the class:
// Music class.
class music {
char* song;
properties musicProperties;
public:
music() {
cout << "Apel constructor default muzica." << endl;
this->song = NULL;
}
music(char* songName, char name, char type, char path, float size, char artist, char title, int year) {
cout << "Apel constructor cu parametri muzica." << endl;
if (songName) {
this->song = new char[strlen(songName) + 1];
strcpy(this->song, songName);
musicProperties.setDataMusic(name, type, path, size, artist, title, year);
} else {
songName = NULL;
}
}
music operator=(const music &songName) {
cout << "Supraincarcare operator = pentru muzica." << endl;
this->song = new char[strlen(songName.song) + 1];
strcpy(this->song, songName.song);
}
music(const music &songName) {
cout << "Apel constructor de copiere muzica." << endl;
if (songName.song) {
this->song = new char[strlen(songName.song) + 1];
strcpy(this->song, songName.song);
} else {
song = NULL;
}
}
~music() {
cout << "Apel destructor muzica." << endl;
if (this->song) {
cout << "Apel destructor pentru: " << this->song << endl;
delete[] song;
}
}
friend char* getMusic(const music& m);
friend ostream& operator<<(ostream& out, music& m);
friend istream& operator>>(istream& in, music& m);
};
and this is the main:
void main() {
music m1;
music m2("ceva", "ceva", "mp3", "c:datamusic", 38, "Rihanna", "Stay", 2013);
music m3 = m1;
}
Ok, tried your suggestions, and changed to string, and now my app looks like this:
#include <iostream>
#include <string>
using namespace std;
// Properties class.
class properties{
string name;
string type;
string path;
float size;
// For images.
int width;
int height;
// For music.
string artist;
string title;
int year;
public:
properties() {
cout << "Apel constructor default proprietati." << endl;
this->name = "";
this->type = "";
this->path = "";
this->size = 0;
this->width = 0;
this->height = 0;
this->artist = "";
this->title = "";
this->year = 0;
}
properties(string name, string type, string path, float size, int width, int height, string artist, string title, int year) {
cout << "Apel constructor cu parametrii proprietati." << endl;
this->name = name;
this->type = type;
this->path = path;
this->size = size;
this->width = width;
this->height = height;
this->artist = artist;
this->title = title;
this->year = year;
}
char getName() {
return this->name;
}
char getType() {
return this->type;
}
char getPath() {
return this->path;
}
float getSize() {
return this->size;
}
int getWidth() {
return this->width;
}
int getHeight() {
return this->height;
}
char getArtist() {
return this->artist;
}
char getTitle() {
return this->title;
}
int getYear() {
return this->year;
}
void setName(string name){
this->name = name;
}
void setPath(string path){
this->path = path;
}
void setType(string type){
this->type = type;
}
void setSize(float size){
this->size = size;
}
void setWidth(int width){
this->width = width;
}
void setHeight(int height){
this->height = height;
}
void setArtist(string artist){
this->artist = artist;
}
void setTitle(string title){
this->title = title;
}
void setYear(int year){
this->year = year;
}
void setDataGeneral(string name, string type, string path, float size){
cout << "Apel setter general properties." << endl;
setName(name);
setType(type);
setPath(path);
setSize(size);
}
void setDataMusic(string name, string type, string path, float size, string artist, string title, int year){
cout << "Apel setter music properties." << endl;
setName(name);
setType(type);
setPath(path);
setSize(size);
setArtist(artist);
setTitle(title);
setYear(year);
}
void setDataImages(string name, string type, string path, float size, int width, int height){
cout << "Apel setter image properties." << endl;
setName(name);
setType(type);
setPath(path);
setSize(size);
setWidth(width);
setHeight(height);
}
// Constructor de copiere.
properties(const properties& p){
cout << "Apel constructor copiere properties." << endl;
this->name = p.name;
this->type = p.type;
this->path = p.path;
this->size = p.size;
this->width = p.width;
this->height = p.height;
this->artist = p.artist;
this->title = p.title;
this->year = p.year;
}
~properties(){
cout << "Apel destructor properties." << endl;
};
};
// Music class.
class music {
char* song;
properties musicProperties;
public:
music() {
cout << "Apel constructor default muzica." << endl;
this->song = NULL;
}
music(char* songName, string name, string type, string path, float size, string artist, string title, int year) {
cout << "Apel constructor cu parametri muzica." << endl;
if (songName) {
this->song = new char[strlen(songName) + 1];
strcpy(this->song, songName);
musicProperties.setDataMusic(name, type, path, size, artist, title, year);
} else {
songName = NULL;
}
}
music operator=(const music &songName) {
cout << "Supraincarcare operator = pentru muzica." << endl;
this->song = new char[strlen(songName.song) + 1];
strcpy(this->song, songName.song);
}
music(const music &songName) {
cout << "Apel constructor de copiere muzica." << endl;
if (songName.song) {
this->song = new char[strlen(songName.song) + 1];
strcpy(this->song, songName.song);
} else {
song = NULL;
}
}
~music() {
cout << "Apel destructor muzica." << endl;
if (this->song) {
cout << "Apel destructor pentru: " << this->song << endl;
delete[] song;
}
}
friend char* getMusic( music& m); //const
friend ostream& operator<<(ostream& out, music& m);
friend istream& operator>>(istream& in, music& m);
};
// Movies class.
class movies {
char* movie;
properties moviesProperties;
public:
movies() {
cout << "Apel constructor default filme." << endl;
movie = NULL;
}
movies(char* movieTitle, string name, string type, string path, float size) {
cout << "Apel constructor cu parametri filme." << endl;
if (movieTitle) {
this->movie = new char[strlen(movieTitle) + 1];
strcpy(this->movie, movieTitle);
moviesProperties.setDataGeneral(name, type, path, size);
} else {
movie = NULL;
}
}
movies operator=(const movies &movieTitle) {
cout << "Supraincarcare operator = pentru filme." << endl;
this->movie = new char[strlen(movieTitle.movie) + 1];
strcpy(this->movie, movieTitle.movie);
}
movies(const movies &movieTitle) {
cout << "Apel constructor de copiere filme." << endl;
if (movieTitle.movie) {
this->movie = new char[strlen(movieTitle.movie) + 1];
strcpy(this->movie, movieTitle.movie);
} else {
movie = NULL;
}
}
~movies() {
cout << "Apel destructor filme." << endl;
if (this->movie) {
cout << "Apel destructor pentru: " << this->movie << endl;
delete[] movie;
}
}
friend char* getMovies(const movies& m);
friend ostream& operator<<(ostream& out, movies& m);
friend istream& operator>>(istream& in, movies& m);
};
// Images class.
class images {
char* image;
properties imagesProperties;
public:
images() {
cout << "Apel constructor default imagini." << endl;
image = NULL;
}
images(char* imageName, string name, string type, string path, float size, int width, int height) {
cout << "Apel constructor cu parametri imagini." << endl;
if (imageName) {
this->image = new char[strlen(imageName) + 1];
strcpy(this->image, imageName);
imagesProperties.setDataImages(name, type, path, size, width, height);
} else {
image = NULL;
}
}
images operator=(const images &imageName) {
cout << "Supraincarcare operator = pentru imagini." << endl;
this->image = new char[strlen(imageName.image) + 1];
strcpy(this->image, imageName.image);
}
images(const images &imageName) {
cout << "Apel constructor de copiere muzica." << endl;
if (imageName.image) {
this->image = new char[strlen(imageName.image) + 1];
strcpy(this->image, imageName.image);
}
else {
image = NULL;
}
}
~images() {
cout << "Apel destructor muzica." << endl;
if (this->image) {
cout << "Apel destructor pentru: " << this->image << endl;
delete[] image;
}
}
friend char* getImages(const images& i);
friend ostream& operator<<(ostream& out, images& i);
friend istream& operator>>(istream& in, images& i);
};
char* getMovies(const movies& m) {
if (m.movie)
return m.movie;
else
return NULL;
}
char* getImages(const images& i) {
if (i.image)
return i.image;
else
return NULL;
}
char* getMusic(music& songName) { //const
if (songName.song)
return songName.song;
else
return NULL;
}
ostream& operator<<(ostream& out, music& s) {
out << "Melodia este: " << s.song << endl;
return out;
}
istream& operator>>(istream& in, music& s) {
char buffer[20];
cout << "Melodia: ";
in >> buffer;
s.song = new char[strlen(buffer) + 1];
strcpy(s.song, buffer);
return in;
}
void main() {
music m1;
music m2("ceva", "ceva", "mp3", "c:datamusic", 38, "Rihanna", "Stay", 2013);
music m3 = m1;
//cout << getMusic(m2) << endl;
//cout << m2;
//cin >> m2;
//cout << m2;
}
and it gives me the following error(s):
Error 1 error C2440: 'return' : cannot convert from 'std::string' to 'char' c:usersandrei bordeianudocumentsvisual studio 2013projectsmediaplusmediaplussource.cpp 50 1 MediaPlus
etc...
What can I to to resolve this issue ? Thanks!
You should try
music(char* songName, char* name, char* type, char* path, float size, char* artist, char* title, int year);
since char is a single character not pointer to a C string. Even better you should use a modern string class instead of old pointer stuff.
You're trying to pass string types ("ceva", "mp3", etc) into char parameters. To solve this, change your constructor parameters from char
to string
.
In the signature, the function accpects a char for name, but you send a char*. music(char* songName, char name, char type, char path, float size, char artist, char title, int year)
I think it should be: music(char* songName, char* name, char* type, char* path, float size, char* artist, char* title, int year)
上一篇: 在模板中复制常量会基于类型奇怪地失败