Answer the question
In order to leave comments, you need to log in
What is wrong with the assignment statement?
Please tell me what is wrong
MP3_Format& operator=(const char* c_string){
song = *c_string;
return *this;
}
A1.cpp:79:20: error: conversion from 'const char [11]' to non-scalar type 'MP3_Format' requested
MP3_Format mp3a = "Summertime";
Answer the question
In order to leave comments, you need to log in
What's wrong with standard AR?
$search = 'lorem';
$query = MyModel::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->andFilterWhere(['like', 'name', $search])
return $dataProvider;
Two operations are needed here: "assign" (possibly auto-generated) and a copy constructor.
class MP3_Format {
MP3_Format(const char* c_string);
MP3_Format& operator=(const MP3_Format&); // велика вероятность, что компилятор её сгенерирует автоматически
};
MP3_Format("Summertime")
and then transfer it to our mp3a. Most likely, the optimizer will remove this transfer and initialize mp3a in place, but such is the semantics ...
The string MP3_Format mp3a = "Summertime";
is initialized, not assigned.
The code
MP3_Format mp3a{};
mp3a = "Summertime";
should work fine. const char*
and the assignment operator, as in Mercury13's answer
Konstantin Stepanov
class Audioformat {
public:
//Audioformat(){mp3_counter++;};
virtual string titel() { return ""; }
virtual string info() { return ""; }
virtual ~Audioformat() {};
};
int mp3_counter = 0;
class MP3_Format : public Audioformat {
public:
MP3_Format(){mp3_counter++;};
MP3_Format(MP3_Format &cop){
MP3_Format(cop.song);
mp3_counter++;
}
MP3_Format(const char* c_string) {
song = (const char*) c_string;
mp3_counter++;
}
MP3_Format(string _song): song(_song){
mp3_counter++;
}
MP3_Format& operator=(const MP3_Format&){
cout<< "Im here" ;
return *this ;
}
~MP3_Format(){mp3_counter--;}
string titel() { return song; }
string info() { return "MP3"; }
operator string() const { return song; }
operator string() { return song; }
friend bool operator==(const MP3_Format& lhs, const MP3_Format& rhs)
{
return lhs.song == rhs.song;
}
friend bool operator!=(const MP3_Format& lhs, const MP3_Format& rhs)
{
return !operator==(lhs,rhs);
}
MP3_Format& operator=(const char* c_string){
song = *c_string;
return *this;
}
private:
string song;
};
bool test_A1_e() {
#ifdef A1_e
//MP3_Format mp3a("Summertime");
MP3_Format mp3a = "Summertime";
MP3_Format mp3b;
mp3b = "Summertime";
cout << mp3b.titel() << " mp3b.titel()";
string ta = mp3a.titel();
string tb = mp3b.titel();
return (ta == "Summertime" &&
tb == "Summertime" );
#else
return false;
#endif
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question