Jump to content
  • Advertisement
Sign in to follow this  
jenny_wui

Problem with C++ and visual studio 2010

This topic is 2137 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello every body, the following piece of C++ code runs ok  on visual studio 2008, but I find the following error while compiling on visual studio 2010:

 

/****************************************************************/

c:\users\myg741\documents\visual studio 2010\projects\hash\hash\main.cpp(13): error C2872: 'hash' : ambiguous symbol

1> could be 'c:\users\myg741\documents\visual studio 2010\projects\hash\hash\hash.h(10) : hash'

1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(463) : std::hash'

1> hash.cpp

1>c:\users\myg741\documents\visual studio 2010\projects\hash\hash\hash.cpp(10): error C2872: 'hash' : ambiguous symbol

1> could be 'c:\users\myg741\documents\visual studio 2010\projects\hash\hash\hash.h(10) : hash'

1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(463) : std::hash'

1>c:\users\myg741\documents\visual studio 2010\projects\hash\hash\hash.cpp(27): error C2872: 'hash' : ambiguous symbol

1> could be 'c:\users\myg741\documents\visual studio 2010\projects\hash\hash\hash.h(10) : hash'

1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(463) : std::hash'

1>c:\users\myg741\documents\visual studio 2010\projects\hash\hash\hash.cpp(33): warning C4018: '<' : signed/unsigned mismatch

1> Generating Code...

1>

1>Build FAILED.

1>

 

/**************************************************************************************************************************************/

/******************************************************************************************************************************************************************/

// hash.h

 

#include <cstdlib>

#include <iostream>

#include <string>

 

using namespace std;

 

#ifndef HASH_H

#define HASH_H

 

class hash{

      private:

             static const int tableSize = 10;

             struct item{

                    string name;

                    string drink;

                    item* next;

              };

             item* HashTable[tableSize];

      public:

             hash();

             int Hash(string key);

};

?

 

#endif

 

 

/*******************************************************************************************************************************************************************/

// hash.cpp file

 

 

#include <cstdlib>

#include <iostream>

#include <string>

#include "hash.h"

 

using namespace std;

?

 

hash::hash(){

 

 

           for (int i = 0; i < tableSize; i++){

                HashTable =  new item;

                HashTable->name = "empty";

                HashTable->drink = "empty";

                HashTable->next = NULL;? ?

           }

}

?

 

int hash::Hash(string key){

 

    int hash = 0;

    int index;

 

    for (int i = 0; i < key.length(); i++){

         hash = hash + (int)key;

         cout << "hash = " << hash << endl;

    }

 

    index = hash % tableSize;

 

    return index;

}

 

/**********************************************************************************************************************/

 

//main.cpp

 

#include <cstdlib>

#include <iostream>

#include <string>

#include "hash.h"

 

using namespace std;

?

 

int main(int argc, char** argv){

 

    int index;

    hash hashObj;

 

    index = hashObj.Hash("Paula");

    cout << "index = " << index << endl;

 

    return 0;

}

 

 

 

 

Share this post


Link to post
Share on other sites
Advertisement

using namespace std; in a header.

 

 

Never a good idea.

Edited by Washu

Share this post


Link to post
Share on other sites

Uh-oh, I suspect this post is going to get the whole forum involved. At least the C++ users who have learned their lesson about using and namespaces.  smile.png

 

Basically,what's happening is that the following error is telling you there are two definitions of hash, the one that you declare and one that's included in the VC studio 2010 system files (which apparently is not in 2008).

:\users\myg741\documents\visual studio 2010\projects\hash\hash\main.cpp(13): error C2872: 'hash' : ambiguous symbol
1> could be 'c:\users\myg741\documents\visual studio 2010\projects\hash\hash\hash.h(10) : hash'
1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(463) : std::hash'

The warning is saying the compiler doesn't know which hash to pick.  This is because you have used "using namespace std;".  You can either remove the using clause and update everything in the file that uses that std namespace by prefixing it with 'std::' or you can tell the compiler which hash you want to use.

 

If you want to use your version of hash do this in your hash declaration:

::hash hashObj;

This tells the compiler to use the global namespace rather than the std:: namespace.

Edited by Cosmic314

Share this post


Link to post
Share on other sites

'hash' : ambiguous symbol


There are two classes named "hash": std::hash, a provided by the C++11 standard which may have been included by consequence in cstdlib or string; and hash, defined in your hash.h. The Visual Studio 2010 C++ compiler introduced some support for C++11 (the newest version of C++). This would explain why it would compile for Visual Studio 2008 and not 2010.

To fix, either rename your class, or stop using a "using namespace" in a header and properly qualify cout, string, and etc with "std::".

Share this post


Link to post
Share on other sites

// hash.h
 
//-----------------------
//#include <cstdlib>
//#include <iostream>
//-----------------------
#include <string>
 
//-----------------------
// using namespace std;
//-----------------------
 
#ifndef HASH_H
#define HASH_H
 
class hash{
      private:
             static const int tableSize = 10;
             struct item{
                    std::string name;
                    std::string drink;
                    item* next;
              };
             item* HashTable[tableSize];
      public:
             hash();
             int Hash(std::string key);
};
?
 
#endif
 
 
/*******************************************************************************************************************************************************************/
// hash.cpp file
 

//----------------------- 
//#include <cstdlib>
//-----------------------
#include <iostream>
#include <string>
#include "hash.h"

//----------------------- 
// using namespace std;
//-----------------------
?
 
hash::hash(){
 
 
           for (int i = 0; i < tableSize; i++){
                HashTable[i] =  new item;
                HashTable[i]->name = "empty";
                HashTable[i]->drink = "empty";
                HashTable[i]->next = NULL;? ?
           }
}
?
 
int hash::Hash(std::string key){
 
    int hash = 0;
    int index;
 
    for (int i = 0; i < key.length(); i++){
         hash = hash + (int)key[i];
         std::cout << "hash = " << hash << std::endl;
    }
 
    index = hash % tableSize;
 
    return index;
}
 
/**********************************************************************************************************************/
 
//main.cpp
 
//-----------------------
//#include <cstdlib>
//-----------------------

#include <iostream>

//-----------------------
//#include <string>
//-----------------------

#include "hash.h"
 
//-----------------------
//using namespace std;
//-----------------------
?
 
int main(int argc, char** argv){
 
    int index;
    hash hashObj;
 
    index = hashObj.Hash("Paula");
    std::cout << "index = " << index << std::endl;
 
    return 0;
}
 
 

I believe the above changes should work.  I commented out the include files each compilation unit wasn't using.  I'm not sure why cstdlib was used.  I commented all 'using namespace std'.  Wherever 'std' namespace is used I prefixed it with 'std::'.  In your program these places are 'std::string', 'std::cout', and 'std::endl'.

 

I've made the same programming mistake before.  And then I read the link that fastcall22 posted.  Now I have enough muscle memory to 'std::' prefix everything that needs it.  Perhaps, not a good thing the more I think about it.  The muscle memory part that is.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!