C++ VS 2013 C+中的错误c2664+;
因此,我有一个名为“music”的类,当我试图编译代码时,它会给我以下错误: 错误13错误C2664:“music::music(const music&”):无法转换 参数2从'const char[5]'到'char'c:\users\andrei bordeianu\documents\visual studio 2013\projects\mediaplus\mediaplus\source.cpp 362 1 mediaplus 14 IntelliSense:构造函数“music::music”的实例不匹配 参数列表 参数类型有:(常量字符[5],常量字符[5],常量字符[4],常量字符[12],int,常量字符[8],常量字符[5], int)c:\Users\Andrei Bordeianu\Documents\Visual Studio 2013\Projects\MediaPlus\MediaPlus\Source.cpp 361 11 MediaPlus 这是一节课:C++ VS 2013 C+中的错误c2664+;,c++,oop,c2664,C++,Oop,C2664,因此,我有一个名为“music”的类,当我试图编译代码时,它会给我以下错误: 错误13错误C2664:“music::music(const music&”):无法转换 参数2从'const char[5]'到'char'c:\users\andrei bordeianu\documents\visual studio 2013\projects\mediaplus\mediaplus\source.cpp 362 1 mediaplus 14 IntelliSense:构造函数“music::
// 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);
};
好的,尝试了您的建议,并更改为字符串,现在我的应用程序如下所示:
#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:\data\music", 38, "Rihanna", "Stay", 2013);
music m3 = m1;
//cout << getMusic(m2) << endl;
//cout << m2;
//cin >> m2;
//cout << m2;
}
#包括
#包括
使用名称空间std;
//属性类。
类属性{
字符串名;
字符串类型;
字符串路径;
浮子大小;
//对于图像。
整数宽度;
内部高度;
//为了音乐。
弦乐艺术家;
字符串标题;
国际年;
公众:
属性(){
cout type=“”;
此->路径=”;
这个->大小=0;
这个->宽度=0;
这个->高度=0;
本->艺术家=”;
此->标题=”;
本->年=0;
}
属性(字符串名称、字符串类型、字符串路径、浮点大小、整数宽度、整数高度、字符串艺术家、字符串标题、整数年){
cout类型=类型;
这->路径=路径;
这个->大小=大小;
这个->宽度=宽度;
这个->高度=高度;
这个->艺术家=艺术家;
这->标题=标题;
本->年=年;
}
char getName(){
返回此->名称;
}
char getType(){
返回此->类型;
}
char getPath(){
返回此->路径;
}
float getSize(){
返回此->大小;
}
int getWidth(){
返回此->宽度;
}
int getHeight(){
返回此->高度;
}
char getArtist(){
返回此->艺术家;
}
char getTitle(){
返回此->标题;
}
整年{
今年返回;
}
void setName(字符串名称){
此->名称=名称;
}
void setPath(字符串路径){
这->路径=路径;
}
void setType(字符串类型){
这个->类型=类型;
}
无效设置大小(浮动大小){
这个->大小=大小;
}
无效设置宽度(整型宽度){
这个->宽度=宽度;
}
无效设置高度(内部高度){
这个->高度=高度;
}
虚空设置艺术家(弦乐艺术家){
这个->艺术家=艺术家;
}
void setTitle(字符串标题){
这->标题=标题;
}
无效设置年(整数年){
本->年=年;
}
void setDataGeneral(字符串名称、字符串类型、字符串路径、浮点大小){
cout title=p.title;
本->年=p年;
}
~properties(){
无法您正在尝试将字符串类型(“ceva”、“mp3”等)传递到char参数。若要解决此问题,请将构造函数参数从char
更改为string
,您应该试试
music(char* songName, char* name, char* type, char* path, float size, char* artist, char* title, int year);
由于char是单个字符,而不是指向C字符串的指针。更好的是,您应该使用现代的字符串类,而不是旧的指针类。在签名中,函数会将char作为name,但您会发送char*。
音乐(字符*歌曲名、字符名、字符类型、字符路径、浮点大小、字符艺术家、字符标题、整数年)
我认为应该是:
音乐(char*songName、char*name、char*type、char*path、float size、char*artist、char*title、int-year)
如果你得到了答案,请使用字符串类型或指针
至于发生这种情况的原因:您将字符作为值传递,这意味着它们将获得自己的新字符数组实例,这与参数的const实例冲突没有导致错误。在你将错误信息添加到一个问题之前,你真的读过错误信息吗?错误会告诉你到底出了什么问题。我是OOP CPPAnd的新手?错误信息不会有什么影响。阅读它并试着解释它所说的内容。如果你从来没有读过错误信息,你永远都不会成为一名优秀的程序员。
music(char* songName, char* name, char* type, char* path, float size, char* artist, char* title, int year);