Sign in to follow this  
Crusable77

String is ambiguous

Recommended Posts

Crusable77    594
I am prototyping something for a big project, but i got a funny error saying that string is ambiguous I have never seen this before, and i have been writing my code this way for forever.

[source lang="cpp"]#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>

using namespace std;

class Creature{

public:
Creature(int strength, int health, int armor, string name); --string error
void getHealth();
int modifyHealth(int modifier, int type); //type = 1(add), or 2(remove)
int attack();
int block();

private:
int m_Strength;
int m_Health;
int m_Damage;
int m_Armor;
string m_Name; --string error
int num;

};
[/source]

Share this post


Link to post
Share on other sites
ultramailman    1720
I think you should take away the
[CODE]using namespace std;[/CODE]

and use
[CODE]std::string[/CODE]

instead of
[CODE]string[/CODE]

But I found that on google's first result. Perhaps you didn't <search engine of choice> hard enough? Edited by ultramailman

Share this post


Link to post
Share on other sites
Cornstalks    7030
[quote name='ultramailman' timestamp='1353382641' post='5002561']
That doesn't work? Then how about removing
[CODE]#include <string>[/CODE]

Because usually iostream also includes string.
[/quote]
Don't do that. You should explicitly include the headers you need. But you're absolutely right about [font=courier new,courier,monospace]using namespace std;[/font]. A global [font=courier new,courier,monospace]using namespace ...;[/font] should [b]never [/b]go in a header.

@OP: You should always Google first. It's a fantastic skill to learn. What are your other headers/sources doing? Is anything defining [font=courier new,courier,monospace]string [/font]before your header? Anything in stdafx.h? [url="http://stackoverflow.com/a/4872531/1287251"]Typically it's caused by something like this example[/url].

Share this post


Link to post
Share on other sites
SiCrane    11839
It looks like you're using MSVC from the stdafx.h in your headers. If that's the case, then you might want to look at the full build output rather than the summary in the task pane. It will probably tell you the exact source of the ambiguous symbols.

Share this post


Link to post
Share on other sites
ultramailman    1720
[quote name='Cornstalks' timestamp='1353383054' post='5002564']
Don't do that. You should explicitly include the headers you need
[/quote]

True, I just thought that might be a last resort that magically fixes OP's problem. It's improbable, but I thought maybe his string header doesn't have include guards.

Share this post


Link to post
Share on other sites
Bacterius    13165
[quote name='ultramailman' timestamp='1353383504' post='5002566']
[quote name='Cornstalks' timestamp='1353383054' post='5002564']
Don't do that. You should explicitly include the headers you need
[/quote]

True, I just thought that might be a last resort that magically fixes OP's problem. It's improbable, but I thought maybe his string header doesn't have include guards.
[/quote]
I would expect the OP to have bigger problems if his standard headers don't have include guards though :)

Share this post


Link to post
Share on other sites
C0lumbo    4411
The problem is that string is defined as std::string, and it must be being defined somewhere else too, but there's not enough information posted to work out where. If you get rid of the 'using namespace std;' line and add the necessary std:: bits, then the error will go away.

It's often considered unwise to use the 'using' keyword in a header file. I'd advise you to get rid of that habit, and keep any 'using' stuff to your .cpp files.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this