Jannat Arora
Jannat Arora

Reputation: 2989

how to write assignment = operator

In Abc.hpp file the following information is defined:

class Abc: public A
{
 enum Ac { VAR };

   struct L{
      std::string s1;
      ::Class2::ID type;
      unsigned s;
      bool operator==(const L& l) const { return (type==l.type)&&(s==l.s)&&(s==l.s); }
   };

   class SS
   {
      public:
      virtual ~SS();
   };
   class IS {
      public:
      /// Destructor
      virtual ~IS();

   };

   class HashIndexImplementation;
   class HashIndex;

 void func(){}

  Abc& operator=(Abc&) {
    cout << "A::operator=(A&)" << endl;
    return *this;
  } //It gives me the error that the token '{' is not recognized

 Abc(Class2 & part);
};

For the above class aim at assigning with another class the following information for my purpose:

Abc d;
static Abc f;
f=d;

However, the code that I have written above does not work...The error that it throws is:

 no matching function for call to Abc::Abc()

EDIT: I am dealing with an entire hierarchy of classes therefore if I add on another constructor like Abc() then am forced to do changes in as many as 20 more classes...is there no other way which can be used for assignment. Is there some means by which I may incorporate the other constructor.

Upvotes: 0

Views: 1003

Answers (3)

Alok Save
Alok Save

Reputation: 206526

no matching function for call to Abc::Abc()

You need to provide a constructor which takes no arguments if you want to instantiate your class object as:

Abc d;

This is because the compiler does not generate the default no argument constructor if you provide any constructor of your own. You provided your own copy constructor so compiler forces you to provide your own no argument constructor.

Upvotes: 3

John Dibling
John Dibling

Reputation: 101456

After having stripped out everything irrelevant and producing a short self-contained compilable example (please do this yourself in future questions), I've come up with this:

#include <iostream>
#include <iomanip>
#include <map>
#include <string>
using namespace std;
class Abc
{
 enum Ac { VAR };

  Abc& operator=(Abc&) {
    cout << "A::operator=(A&)" << endl;
    return *this;
  } //It gives me the error that the token '{' is not recognized

};

int main()
{
    Abc abc;
}

This compiles, but doesn't really do much. You do nothing with the inbound Abc reference. Technically correct as far as language syntax is concerned, it does no useful work. Typically you will do something along the lines of this:

class Abc
{
  int foo_;
  Abc& operator=(Abc& rhs) {
    cout << "A::operator=(A&)" << endl;

  foo_ = rhs.foo_;

    return *this;
  } //It gives me the error that the token '{' is not recognized
};

Beyond that, the syntax error you had has now gone away. There were a lot of undefined things such as Abc's base class, B and ::class2. Perhaps you need to #include something to bring in these defines.

Upvotes: 0

Ryan
Ryan

Reputation: 1797

You do not have a semi-colon after the closing brace of class Abc. Try adding the semi-colon, it should solve the problem.

Upvotes: 0

Related Questions