C:\Users\Kostya\Desktop\c++\MacroWriter\ifexists.cpp|27|error: ambiguous overload for 'operator=' in '((farm*)this)->farm::sendeachbase = 0'|


Ok so I am making a class for my program and I want to initialize some of its variables to 0's. I haven't done anything with classes for a while so I am not good with this stuff.

This is my code:

class someclass
        {
            public:

            string coords;
            int basen;
        string sendtoeach[5];
        someclass() {  // my constructor
        int i = 0;
        while(i<5) {
        sendtoeach[i] = 0;
        i++;
        }
        }
        };

This doesnt work and it gives me an error above. What did I do wrong and how do I initialize some variables of a class to 0 if I need to. And also do you know if code::blocks initializes all the variables in the class to 0's because then it will help a lot and I will not need that stupid constructor.

Recommended Answers

All 3 Replies

So what are you expecting 0 to represent as a string?

Thanks lol, I changed the string to int and it worked.

Note that usually you would initialize variables in the constructors like so :

class Point{
private:
 int x_, y_;
public:
 Point(int x , int y) 
  : x_(x) , y_(y) //called an initializer list
  {
  }

};

There is something you have to remember about the above though. It will initialize data in order
of their declaration not in the order of your initializer list. Just read this sentence a couple of times a day and it will sink in.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.