1
#include<iostream>
#include<stdlib.h>
#include<string.h>
using namespace std;
class div
{
    int x,y;
public:
    class dividebyzero
    {
    };
    class noerror1
    {
    };
    div(){};
    div(int a,int b)
    {
        x=a;
        y=b;
    }
    void error1()
    {
        if(y==0)
            throw dividebyzero();
        else
            throw noerror1();
    }
    int divide()
    {
        return (x/y);
    }
};
class naming
{
    char name[32];
public:
    class nullexception
    {
    };
    class noerror2
    {
    };
    naming(char a[32])
    {
        strcpy(name,a);
    }
    void error2()
    {
        if(strcmp(name,"")==0)
            throw nullexception();
        else
            throw noerror2();
    }
    void print()
    {
        cout<<"Name-----"<<name<<endl;
    }
};
int main()
{
    div d(12,0);
    try
    {
        d.error1();
    }
    catch(div::dividebyzero)
    {
        cout<<"\nDivision by Zero-------Not Possible\n";
    }
    catch(div::noerror1)
    {
        cout<<"\nResult="<<d.divide()<<endl;
    }
    naming s("Pankaj");
    try
    {
        s.error2();
    }
    catch(naming::nullexception)
    {
        cout<<"\nNull Value in name\n";
    }
    catch(naming::noerror2)
    {
        s.print();
    } 
    return 0;
}

On compiling this program I am getting following error

pllab55.cpp: In function ‘int main()’:
pllab55.cpp:61:6: error: expected ‘;’ before ‘d’
pllab55.cpp:64:3: error: ‘d’ was not declared in this scope
pllab55.cpp:72:22: error: ‘d’ was not declared in this scope
pllab55.cpp:74:20: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

Before declaring the class naming everything was running fine.It is after declaration of naming these error started to occur. I am new to C++. Please explain me in details. Thanks in advance.

3
  • When you say "on running this program", do you actually mean "on compiling this program"? Commented Aug 20, 2012 at 6:57
  • I think class naming has nothing to do with it yo can simply remove that class from your source code Commented Aug 20, 2012 at 7:02
  • See also: Why is 'using namespace std;' considered a bad practice in C++? Commented Aug 20, 2012 at 7:12

5 Answers 5

2

There is already an std::div in the standard namespace and since you use a using namespace directive instead of declaration it imports all the symbols in std namespace to your current scope. So perhaps renaming the div class shall do the trick for you.

I tried renaming it and it does work indeed.

So either rename your class or wrap it in your own namespace so it does not conflict with std::div

Sign up to request clarification or add additional context in comments.

1 Comment

@PallaviKashyap: Clarified a bit more, Hope that helps :)
2

Your class div shares the same name as std::div. When you do #using namespace std, the result is that each class in the std namespace is imported into your current scope, meaning that std::div is now essentially called div. If you see, that means you now have two classes called div in the same scope, your own and the std class.

By the way, you should avoid the using namespace syntax and rather use the full qualifier of the class (e.g. std::cout).

Comments

0

Your div class is conflicting with std::div so either rename yours or put put your div class in a different namespace.

namespace me {
   struct div{};
}

me::div d;

Comments

0

I gave (a slight variant of) your code a try in gcc and I got the following error:

 /usr/include/stdlib.h:780: error: too few arguments to function 'div_t div(int, int)'

You're trying to override a name from a standard library and experience the conflict of a class and a function having the same name, I'm afraid.

Comments

0

as a general rule of thumb, if you encounter such issues, try to reduce your code as much as possible. For instance, I reduced it down to

#include<stdlib.h>

class div {
public:
  div (int a, int b) { }
};

int
main () {
  div d (12, 0);
  return 0;
}

which still shows your error (at least the first one - the others are followup errors). This lets you also reduce possible assumptions about the reason for the error - as you see, your new class "naming" does not have to do anything with the error you see. When I now additionally remove the include, the error does not show up anymore, which lets me suspect some naming clash with some symbol from stdlib.h. After renaming the class "div" to something else (like "CDiv"), it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.