J
J
Julia2016-09-18 00:06:20
MySQL
Julia, 2016-09-18 00:06:20

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

5 answer(s)
M
Maxim Timofeev, 2016-08-23
Reytarovsky @Antonchik

What's wrong with standard AR?

$search = 'lorem';
$query = MyModel::find();
$dataProvider = new ActiveDataProvider([
            'query' => $query,
 ]);
$query->andFilterWhere(['like', 'name', $search])
return $dataProvider;

M
Mercury13, 2016-09-18
@Mercury13

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&);   // велика вероятность, что компилятор её сгенерирует автоматически
};

If you write in C ++ 11, instead of the “assign” operation, you need the “assign from a temporary object” operation. Again, an automatically generated one is fine.
The semantics of the C++ language in this line is as follows. We create a temporary object 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 ...

K
Konstantin Stepanov, 2016-09-18
@koronabora

Need description MP3_Format

A
Ariox41, 2016-09-18
@Ariox41

The string MP3_Format mp3a = "Summertime";is initialized, not assigned.
The code

MP3_Format mp3a{};
mp3a = "Summertime";
should work fine.
But it's better to separately implement the constructor for const char*and the assignment operator, as in Mercury13's answer

J
Julia, 2016-09-18
@Julila

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;
};

Test I can't change
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 question

Ask a Question

731 491 924 answers to any question