//#include<iostream>
class std
{
public:
int i;
};

int main()
{
std A;
//float f=0.0f;
}

error: ‘struct std’ redeclared as different kind of symbol
error: previous declaration of ‘namespace std { }’

on compiling , above error comes.
we are not including any header file.....then how compiler knows about namespace std?
can we make any class having std?

one more question is : can we make ny program that doesn't call main()....?

Recommended Answers

All 14 Replies

std, is a bad name to keep , This is generally because, Most of the header's have thier programs defined in the Namespace std (which means standard i guess, )

So You are redeclaring it, as a struct, Keep a different name and i guess it would work out fine .

but i am not including ny header file then how compiler knows about std?

but i am not including ny header file then how compiler knows about std?

You don't need to put this line:

using namespace std;

The compiler is looking for namespaces and it looks for std:: without you telling it to look for std:: . The following program compiles and runs.

#include<iostream>


int main()
{
    std::cout << "Hello World" << std::endl;
    return 0;
}

Rename your class to something besides std and the error will go away.

friend , i just want to create a class whose name is std......is there ny way to make that class.....i don't want to correct my error . i just want to create a class named std....that's it...

plz think and reply

Maybe Include iostream.h instead of iostream.

The std namespace isn't defined in a header that you need to include, the compiler knows where to find it without you specifying it, much like it knows what an int is, without you implicitly telling it.

So it will even depend on the compiler where exactly the std namespace is defined.

But honoustly, why are you so determined to use a class with the name std? If you ask me you are wasting your time. There are some generally accepted keywords that you should stay away from.

I know that is not the answer you are looking for, but I wouldn't know of a way to do this, and it seems to me it is just time wasted that you could spend on actually writing code. But of course I don't know your exact motivation.

Including iostream.h instead of iostream won't do you any good, the std namespace is defined without including anything. And as these '.h' headers aren't guaranteed to behave as defined by the standard, there isn't really a reason to ever use them.

You don't need to put this line:

using namespace std;

Rename your class to something besides std and the error will go away.

Here's my solution (just a simple example):

#include <iostream>
using namespace std;

class std
{
enum {A = 1, B, C};
public:
    std::std() { cout << A << endl; }
};

int main()
{
    std::std t; // create an instance of class 'std'
    return 0;
}

:P

Here's my solution (just a simple example):

#include <iostream>
using namespace std;

class std
{
enum {A = 1, B, C};
public:
    std::std() { cout << A << endl; }
};

int main()
{
    std::std t; // create an instance of class 'std'
    return 0;
}

:P

Well, you've proved it can be done, so good job there. The bigger question is, as thelamb mentions, is there any purpose in doing so? I do realize it can be fun to just see if something can be done just for its own sake.

@ tux4life......

friend ur program is giving error in g++ compiler.....
error are:
error: ‘struct std’ redeclared as different kind of symbol
<built-in>:0: error: previous declaration of ‘namespace std { }’

@ tux4life......

friend ur program is giving error in g++ compiler.....
error are:
error: ‘struct std’ redeclared as different kind of symbol
<built-in>:0: error: previous declaration of ‘namespace std { }’

Here's my solution (just a simple example):

#include <iostream>
using namespace std;

class std
{
enum {A = 1, B, C};
public:
    std::std() { cout << A << endl; }
};

int main()
{
    std::std t; // create an instance of class 'std'
    return 0;
}

:P

Hmm... Seems to work in Dev C++ just fine, but won't compile using g++ in Linux. Odd. Get rid of the red std:: above and it seems to work in either.

On MinGW on Windows it's at least compiling, Dev-C++ also uses MinGW ...
And yes, remove the std:: as VernonDozier says :)

friend , it still not working. after removing the red std as told by u...it is giving the same error..

i think that g++ is standard compiler and a gud compiler.if it is giving an error then sumthing wrong we r thinking or doing......
or i should say that we can't make class named std in c++.:?

my motive about this question is just understanding the concept of namespace...that's it...
and why u all ppl using namespace std...there is no need to use namespace std...

friend , it still not working. after removing the red std as told by u...it is giving the same error..

i think that g++ is standard compiler and a gud compiler.if it is giving an error then sumthing wrong we r thinking or doing......
or i should say that we can't make class named std in c++.:?

It IS a standard and good compiler. I don't get any errors after removing std:: when using g++. I don't know about anyone else. You can post the exact code you're running if you like, to make sure we're compiling the same thing.

my motive about this question is just understanding the concept of namespace...that's it...
and why u all ppl using namespace std...there is no need to use namespace std...

How are you going to use cout, cin, endl, etc. without using the std namespace? You either put using namespace std at the top or you preface the commands with std:: . Otherwise the compiler has no idea what cout refers to.

commented: Yeah :) +9

error: ‘struct std’ redeclared as different kind of symbol
<built-in>:0: error: previous declaration of ‘namespace std { }’

So pick a different name, one which isn't the name of the STANDARD namespace, and get on with life.

You can never use 'std' as a struct name, even if some current compiler lets you get away with it.

commented: Good clarification in this thread, who the hell comes on the idea to create a class named 'std' :P +9
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.