I would like to implement a hash table example.
So for this aim, I have created one header, one hash.cpp and main.cpp files.
in my hash.cpp , I tried to run a dummy hash function which takes key value and turns into an index value. however, it throws an error(reference to 'hash' is ambiguous) whenever I try to create an object according to that hash class.
this is my main.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, const char * argv[]) {
hash hash_object;
int index;
index=hash_object.hash("patrickkluivert");
cout<<"index="<<index<<endl;
return 0;
}
this is my hash.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int hash(string key){
int hash=0;
int index;
index=key.length();
return index;
}
this is my hash.h
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef __hashtable__hash__
#define __hashtable__hash__
class hash
{
public:
int Hash(string key);
};
#endif /* defined(__hashtable__hash__) */
Your hash class symbol is clashing with std::hash
A quick fix could be using a global namespace qualifier
int main(int argc, const char * argv[]) {
::hash hash_object;
but a better and recommended one would be to stop polluting your global namespace with
using namespace std;
and just using std::cout or std::endl when you need them.
You could also create your own namespace in case you're writing a library.
Besides, you have some capital letter typos here:
index = hash_object.hash("patrickkluivert");
^ I suppose you're referring to the Hash() function here
and here
int Hash(std::string key) {
^ this needs to be capital as well
int hash = 0;
in case you want to match your declaration and avoid cast/linking errors.
Your hash class is conflicting with std::hash. Stop using using namespace std; right now. If you want to make print statements shorter, try using std::cout; using std::endl;
Related
I want to fetch value of certain key from back-end.
In back-end the structures are defined and values are initialized.
Consider this as structure defined in back-end:
struct person{
string nme;
string adrs;
int id;
};
person p1 = {"steve","ABC street",23};
The key address corresponds to value of p1.adrs in back-end.
Now the key address is to be mapped to (p1 & adrs) in external file and should get the value "ABC street".
My question is how mapping is to be done for key and its particular structure member in external file and how to fetch value for that key.
Here Can I use std::map concept for this?
I got solution for requirement. But to improve it further, mapping is to be declared in separate file (Here it was declared in .h file)
myfile.h
#ifndef MYFILE_H
#define MYFILE_H
#include <iostream>
#include <algorithm>
#include<vector>
#include <string>
#include <map>
using namespace std;
struct Chassis
{
string InLED;
string AstTg;
};
struct Manager
{
string MngrType;
int count;
};
const Chassis chassis1={"On","null"};
const Manager manager1={"BMC",23};
const map<string, const string>cha1={{"IndicatorLED", chassis1.InLED},{"AssetTag",chassis1.AstTg},{"ManagerType",manager1.MngrType}};
const map<string, int>cha2={{"Count",manager1.count}};
void func(string);
#endif
myfile.cpp
#include <iostream>
#include <string>
#include "myfile.h"
#include <map>
using namespace std;
void func(string item)
{ if(cha1.find(item) == cha1.end()) {if (cha2.find(item) == cha2.end()){} else {cout<< item<<":"<<cha2.at(item)<<endl;} }
else {cout<< item<<":"<<cha1.at(item)<<endl;}
}
main.cpp
#include <iostream>
#include <string>
using namespace std;
#include "myfile.h"
int main() {
string item="IndicatorLED";
func (item);
string item1="AssetTag";
func(item1);
string item2="ManagerType";
func(item2);
string item3="Count";
func(item3);
}
I had a little problem with my code. I am trying to create a simple script with a simple header, but things aren't quite working out. I get the following error: Invalid use of non-static data member 'name'. Would really appreciate some help with this problem, I am still a beginner to C++. Thank you in advance!
//header file
#ifndef Game_main_h
#define Game_main_h
#include <iostream>
#include <string>
using namespace std;
class main
{
public:
void resetInput();
string name;
};
#endif
//executional file
#include "main.h"
#include <iostream>
#include <string>
#include <iomanip>
#include <locale>
#include <sstream>
using namespace std;
int main(int argc, const char * argv[])
{
int nr;
string agree;
cout << "Enter your name.\n";
std::getline(cin, main::name);
return 0;
}
You need to create an instance of class main to access it! E.g. say
main x;
std::getline(cin, x.name);
Besides that it's a not so good idea to name a class main.
I got an error of C:\temp\hashTableProject\main.cpp|14|undefined reference to hash::Hash(std::string)
Anyone know how to solve this problem?
hash.h
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef HASH_H
#define HASH_H
class hash{
public:
int Hash(string key);
};
#endif // HASH_H
hash.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int hash::Hash(string key){
//int hash = 0;
int index;
index = key.length();
return index;
}
main.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include "hash.h"
using namespace std;
int main()
{
int index;
hash hashOb;
string traget = "Testing";
index = hashOb.Hash(traget);
cout << index << endl;
return 0;
}
Im using CodeBlock 13.12
There is only main.o file in obj folder. I dont know why the hash.o isn't there.
hash is an inbuilt template in "std" namespace.
Try removing using namespace std; lien and use namespace name as and when required.
I have a mysterious problem. I keep getting a ‘vector’ does not name a type error when trying to define a variable in the tour class. The library seems installed correctly, since I can define vectors in main. According to many posts here I have checked for the right vector includes and std namespace. Still I get the error.
main.cpp
#include <vector>
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <cmath>
#include <cstdlib>
#include "tour.h"
using namespace std;
int main () {
//vector declaration works here
return 0;
}
tour.h
#ifndef TOUR_H_
#define TOUR_H_
#include<vector>
using namespace std;
class tour
{
public:
//variables
int teamID;
vector<int> tourseq; //this is where the error occurs
//functions
tour(int);
};
#endif /* TOUR_H_ */
tour.cpp
#include<vector>
#include "tour.h"
using namespace std;
tour::tour(int id){
teamID = id;
}
What could be wrong here?
Instead of writing using namespace std; and vector<int> tourseq; consider writing std::vector<int> tourseq;.
You probably shouldn't put using namespace std; in your code anyway.
I have absolutely no idea what's going on. I've been looking up explanations for the weirdness going on here but it seems my situation is in some ways unique. I imagined it was the order in which I include my header files in each of my files, but to no avail, I have not found a combination that seems to be the solution.
The exact error I seem to be getting is "log does not name a type" when declaring LogArray[maxLength].
One of my classes, class logmgmt:
class logmgmt
{
private:
static const int maxLength = 500;
log LogArray[maxLength];
int length;
public:
void fillLogs(int index, int iD, std::string date, double startTime, double endTime);
void displayThisLog(int index);
void setLength(int length);
};
Pre-processor directives within logmgmt.cpp:
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
#include "log.h"
#include "Logmgmt.h"
And directives within main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
#include "employee.h"
#include "log.h"
#include "employeemgmt.h"
#include "Logmgmt.h"
Remove using namespace std.
That is polluting the global namespace with lots of symbol names that can cause these conflicts.
In your example, the function std::log becomes log. So it can no longer name a global type.