Convert a string variable in enum variable in c++ - c++

I need your help please especially to know can I convert a string Variable in an enum variable.
Here is my code:
deco_fill.h
#include <iostream>
using namespace std;
#include <string.h>
class A{
class B{
public:
enum tStrict{
"ST_UNKNOWN"=-1;
"ST_USE"=0;
"ST_DEL"=1;
}
public:
tStrict mType;
void setStrict(tStrict& newStrict ){
return mType=newStrict;
}
}
}
test.h
#include <iostream>
using namespace std;
#include <string.h>
#include <deco_fill.h>
class C
{
public:
A::B::tStrict CnvStrToEnum(const string& str); //This method will return a tStrict type
}
test.cpp
#include <iostream>
using namespace std;
#include <string.h>
#include <test.h>
#include <deco_fill.h>
A::B::tStrict C::CnvStrToEnum(const string& str)
{
if (str=="ST_USE")
return ST_USE;
else if (str=="ST_DEL")
return ST_DEL;
else
return ST_UNKNOWN;
}
test_set.cpp
#include <iostream>
using namespace std;
#include <string.h>
#include <deco_fill.h>
#include <test.h>
string st=ST_USE;
A::B::tStrict strictType=CnvStrToEnum(st);
setStrict(strictType);//I want here to use the setStrict methode to set a new variable of type enum with that. This part is not important
I have a compile error in test.cpp like ST_DEL, ST_USE and ST_UNKNOWN were not declared. What's do I need here and how can I correctly the string type in my enum type. Thanks for your help.

enum is for numeric constants (not strings), so you can't write
enum tStrict{
"ST_UNKNOWN"=-1;
"ST_USE"=0;
"ST_DEL"=1;
}
Also note it's comma (NOT semicolon) after each enum constant.
So instead you should write:
enum tStrict{
ST_UNKNOWN=-1,
ST_USE,
ST_DEL
};
Usually you can then convert enum constants to string counterparts:
const char *tStrictStr( const enum tStrict t )
{
switch( t )
{
case ST_UNKNOWN : return "ST_UNKNOWN" ;
case ST_USE : return "ST_USE" ;
case ST_DEL : return "ST_DEL" ;
default : return "ST_UNKNOWN" ;
}
}

Related

c++ recursive class dependencies

I'm trying to make something similar to std::Map. I have two classes, NameValue which takes a name and a Value. The class Value can hold data of type int and string. I want the Value class to also accept NameValue to be able to create nested objects. Currently the boost::variant is used to hold the data types allowed to be used.
NameValue.h
#ifndef INC_NAME_VALUE_H_
#define INC_NAME_VALUE_H_
#include <boost/variant.hpp>
#include <iostream>
#include <string>
#include "value.h"
namespace config {
using namespace std;
class Value; // forward declaration
class NameValue {
private:
string name;
Value* valuePtr;
public:
NameValue(){};
NameValue(string name, Value& value)
: name(name)
, valuePtr(&value){};
void Print() {
cout << name << " : ";
// valuePtr->Print();
}
void Set(Value* value) { valuePtr = value; }
};
}
#endif /* INC_NAME_VALUE_H_ */
Value.h
#ifndef INC_VALUE_H_
#define INC_VALUE_H_
#include <boost/variant.hpp>
#include <iostream>
#include <string>
#include "name_value.h"
namespace config {
using namespace std;
using variantDataType = boost::variant<int, string>;
class Value {
private:
variantDataType value;
public:
Value(){};
Value(variantDataType const& value)
: value(value){};
void Print() { cout << value << endl; }
};
}
#endif /* INC_VALUE_H_ */
In Value.h I want to add NameValue to variant like this:
boost::variant<int,string,NameValue> value;
main.cpp
Value i(42);
NameValue nv("meaning", i);
NameValue nv2("nested, NameValue("deep", "value"));//this is what I want
Maybe I'm on the wrong track using variant or the way I'm using dependencies. If there is some other way to make it work I would appreciate the suggestions.

map structure member to key

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);
}

Reference to "class" is ambigous

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;

Trouble returning an object from a class I created

I have this class (hashMap.h):
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include "functions.h"
using std::cout;
using std::vector;
using std::endl;
using std::string;
class hashMap
{
public:
explicit hashMap(int hashEntrySize = 101) : hashVector(nextPrime(2 * hashEntrySize)), currentSize{ 0 }
{}
bool containsKey(const string & searchKey);
bool containsVector(const vector<string> searchVector);
void insert(const string & keyTarget, const vector<string> & insertVector);
void insertAfterReHash(const string & keyTarget, const vector<string> & insertVector);
int getCurrentSize() const;
void assignKey(string & newKey);
private:
enum EntryType { ACTIVE, EMPTY, DELETED };
struct hashEntry
{
vector<string> vectorValue;
EntryType status;
int keyID;
string key;
hashEntry(EntryType s = EMPTY)
:status(s), keyID{ -1 } {}
};
size_t hashFunction(const string & key);
bool isActive(int currentPos) const;
int findPos(const string & keyTarget);
void reHash();
vector<hashEntry> hashVector;
int currentSize;
};
And a function header file (functions.h):
#pragma once
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
{
hashMap hm(500);
//do stuff with object
return hm;
}
And the main file:
#include <iostream>>
#include <vector>
#include <string>
#include <fstream>
#include "hashMap.h"
using std::string;
using std::cout;
using std::vector;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
int main()
{
vector<string> words;
string line;
ifstream dictionaryFile;
dictionaryFile.open("largedictionary.txt");
words = readinWords(dictionaryFile);
dictionaryFile.close();
hashMap hm = computeAdjacentWords(words);
return 0;
}
I created the hashMap class and I want to be able to return a hashMap object, but this is giving me an error of "Error C4430 missing type specifier - int assumed." What am I doing wrong?
I put the code in files and nicely asked the compiler to do its job. This is the first warning from the list:
$ cc main.cpp -c
In file included from main.cpp:5:
In file included from ./hashMap.h:6:
./functions.h:16:1: error: unknown type name 'hashMap'
hashMap computeAdjacentWords(const vector<string> & words) //error at this line
^
The compiler doesn't know what hashMap is. When it reaches the line with the error, the hashMap symbol was not yet declared or defined.
You shouldn't define functions in header files.
Rename functions.h to functions.cpp, add #include "functions.h" at the end of the list of includes.
Create a new file functions.h that contains only the declarations of the functions (the function header) and the types they use:
#ifndef __FUNCTIONS_H__
#define __FUNCTIONS_H__
#pragma once
//#include <iostream>
#include <vector>
#include <string>
//#include <fstream>
#include "hashMap.h"
using std::string;
using std::vector;
// Do you really need all these types here?
using std::cout;
using std::endl;
using std::cin;
using std::ifstream;
using std::getline;
hashMap computeAdjacentWords(const vector<string> & words);
#endif // __FUNCTIONS_H__
You are including functions.h from hashmap.h BEFORE the hashMap class is defined. As such, when the compiler reads functions.h, the hashMap class is not defined.

Compiler error: does not name a type

I'm new to c++ and I usually can pick through errors and figure out what's wrong but I'm stumped.
I'm getting an error saying "line|10|error: 'string' in class 'mine' does not name a type"
Here is mine.h:
#ifndef MINE_H
#define MINE_H
#include <iostream>
#include <string>
using namespace std;
class mine
{
public:
mine();
string getName();
};
#endif // MINE_H
Here is mine.cpp:
#include "mine.h"
#include <iostream>
#include <string>
using namespace std;
mine::mine()
{
//ctor
}
mine::string getName()
{
}
mine::string getName()
{
}
Should have been
string mine::getName()
{
}
It should be:
string mine::getName()
{
}