0

I was writing a code for operator overloading and when i was writing i was getting a constant ambiguity error whenever I used the name of the class outside the class.It said that the name is ambiguous,and i have tried many things but couldnt overcome this error. I even defined all the functions and memebers inside the class only which i rarely do, just so that tha name of class is not used, but still for creating objects name has to be used.

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

class complex
{
    int real, img;

public:
    complex();

    complex(int real, int img)
    {
        this->real = real;
        this->img = img;
    }

    void print(void)
    {
        cout << "The sum is : " << real << "+ i" << img;
    }
    

    complex operator+(complex const &a)
    {
        complex b;

        b.real = real + a.real;
        b.img = img + a.img;

        return b;
    }

    complex operator-(complex const &a)
    {
        complex b;

        b.real = real - a.real;
        b.img = img - a.img;

        return b;
    }

    complex operator*(complex const &a)
    {
        complex b;

        b.real = real * a.real;
        b.img = img * a.img;

        return b;
    }

    complex operator/(complex const &a)
    {
        complex b;

        b.real = real / a.real;
        b.img = img / a.img;

        return b;
    }
};

int main()
{

    int real[2], img[2];

    cout << "Enter the values for real and imaginary parts of first number : ";
    cin >> real[1] >> img[1];

    cout << "Enter the values for real and imaginary parts of second number : ";
    cin >> real[2] >> img[2];

    complex a(real[1], img[1]), b(real[2], img[2]), c;

    c = a + b;
    c.print();

    c = a - b;
    c.print();

    c = a * b;
    c.print();

    c = a / b;
    c.print();

    return 0;
}

I even defined all the functions and memebers inside the class only which i rarely do, just so that tha name of class is not used and so it would not give me any error, but still for creating objects name has to be used and thus the error still remains.

  • 6
    Because `std::complex` exists. Don't do `using namespace std`. – Nelfeal Oct 30 '22 at 11:46
  • 3
    Also don't use `bits/stdc++.h`. What is the source you're learning C++ from, because I doubt your sources quality. – Pepijn Kramer Oct 30 '22 at 11:47
  • @PepijnKramer why cant bits/stdc++.h be used, aint it just a header file for all the basic functions??? – Kartikay Azad Oct 30 '22 at 11:58
  • @Nelfeal but if i wont use using namespace std , wouldnt i have to use std:: for all other things too?? – Kartikay Azad Oct 30 '22 at 11:59
  • @ChrisMM nahh now its showing undefined reference error, it showing undefined refence to complex::complex() – Kartikay Azad Oct 30 '22 at 12:00
  • 1
    @KartikayAzad Yes. Why is that a bad thing? – Nelfeal Oct 30 '22 at 12:00
  • 1
    Actually, it is the answer. You have other errors too, but this question is about the ambiguity, which is caused by `using namespace std` ... The undefined ref is because your default constructor does not have a definition. – ChrisMM Oct 30 '22 at 12:02
  • @Nelfeal i mean its just extra writing, and in schools we all r taught to use namespace std. and even before today not a single person told me not to use it – Kartikay Azad Oct 30 '22 at 12:05
  • 1
    That isn't surprising, a lot of schools don't teach programming properly. IMO schools shouldn't teach C++ at all. It's too complex to be adequately covered in a single course or even a few. As for the "extra writing" bit: it's 4 characters that explicitly indicate you are using the standard library, a very important piece of the language as a whole. You'll get used to it. – Nelfeal Oct 30 '22 at 12:11
  • @KartikayAzad Because bits/stdc++.h is not a standard C++ header file. Its an internal header file not to be used by end-users. Most sources that shows you `using namespace std;` and does an include of `bits/stdc++.h` are "competitive coding" sites and those should NOT be used to learn C++ – Pepijn Kramer Oct 30 '22 at 12:11
  • @Nelfeal To be a bit more kind, most schools also teach datatstructurs "using C++". Which is different from teaching C++ (but that fact to be honest is usually lost on them). AND coding is not about writing the least amount of code, it is about being explicit and self-describing. E.g [naming things is hard - Kate Gregory](https://www.youtube.com/watch?v=MBRoCdtZOYg). And C++ has been kind, it uses a short name for its default namespace : `std::` is only a few characters. – Pepijn Kramer Oct 30 '22 at 12:12
  • In other words, writing software is not something you do to be finished quickly, it is an engineering discipline (note the word DISCIPLINE). Some seemingly extra steps are needed at the beginning to write good software in the end. (lol I feel like a Jedi talking to a padawan right now but it is true) – Pepijn Kramer Oct 30 '22 at 12:17
  • Please make sure you include the full error output from a [mcve] in your question. You can [edit] your question to fix that now still. – Ulrich Eckhardt Oct 30 '22 at 12:26
  • Your `complex` and `std::complex` have a name collision since you have `using namespace std;` and you are using the "include everything" implementation detail private header file of one particular C++ compiler. – Eljay Oct 30 '22 at 12:47
  • @PepijnKramer TBH it's not far from Jedi training: the field is still relatively new (considering FORTRAN is *only* 66 years old) and is extremely abstract, which means there are a lot of "good practices" we follow that we think lead to good software but without any proof, and then we still have to deal with loads of bad software. Some, if not most, of these "good practices" are frankly akin to mysticism. – Nelfeal Oct 30 '22 at 12:51
  • @Nelfeal This is worth a nice philosophical discussion :) But yes tbh too my insights are also moslty from 40+ years of experience in an immature field. I had to develop a best practices view for myself and I am still learning every day. If I had to pick one thing that stands out after all this time its interfaces (and that's interfaces declared by the code that interacts with the outside world, not interfaces forced upon you by the outside world) – Pepijn Kramer Oct 30 '22 at 13:28

0 Answers0