Failure of #include<string>? - syntax

I am using the following code for a class project, but for some reason the #include string is not working, and the compiler is flagging every declaration using string. What did I do wrong?
#ifndef MEMORY_H
#define MEMORY_H
#include <string>
class Memory
{
private:
string mem[1000];
public:
Memory()
{
for each(string s in mem)
{
s = "nop";
}
};
string get(int loc)
{
return mem[loc];
};
void set(int loc, string input)
{
mem[loc] = input;
}
};
#endif

string is part of the std namespace, instead of string, you need:
std::string
For more on namespaces go here.

Add this after your include statement:
using namespace std;

Related

How to derive abstract template classes, with template-types as function parameters (C++11)

I've been assigned to write a class "binaryExpressionTree" which is derived from the abstract template class "binaryTreeType." binaryExpressionTree is of type String. As part of the assignment, I have to override these 3 virtual functions from binaryTreeType:
//Header File Binary Search Tree
#ifndef H_binaryTree
#define H_binaryTree
#include <iostream>
using namespace std;
//Definition of the Node
template <class elemType>
struct nodeType
{
elemType info;
nodeType<elemType> *lLink;
nodeType<elemType> *rLink;
};
//Definition of the class
template <class elemType>
class binaryTreeType
{
public:
virtual bool search(const elemType& searchItem) const = 0;
virtual void insert(const elemType& insertItem) = 0;
virtual void deleteNode(const elemType& deleteItem) = 0;
binaryTreeType();
//Default constructor
};
binaryTreeType<elemType>::binaryTreeType()
{
}
#endif
Here is what I have so far for binaryExpressionTree:
#define EXPRESSIONTREE_H
#include "binaryTree.h"
#include <iostream>
#include <string>
class binaryExpressionTree : public binaryTreeType<string> {
public:
void buildExpressionTree(string buildExpression);
double evaluateExpressionTree();
bool search(const string& searchItem) const = 0;
void insert(const string& insertItem) = 0;
void deleteNode(const string& deleteItem) = 0;
};
And here's binaryExpressionTree.cpp:
#include <string>
#include <cstring>
#include <stack>
#include <cstdlib>
#include <cctype>
#include "binaryExpressionTree.h"
#include "binaryTree.h"
using namespace std;
bool binaryExpressionTree::search(const string& searchItem) const {
return false;
}
void binaryExpressionTree::insert(const string& insertItem) {
cout << "this";
}
void binaryExpressionTree::deleteNode(const string& deleteItem) {
cout << "this";
}
Here's main.cpp:
#include <iostream>
#include <iomanip>
#include <fstream>
#include "binaryExpressionTree.h"
int main()
{
binaryExpressionTree mainTree = binaryExpressionTree(); //Error:[cquery] allocating an object of abstract class type 'binaryExpressionTree'
return 0;
}
The problem is, since binaryExpressionTree is a derived class of type String, it doesn't know what "elemType" means and I would need to change searchItem, insertItem and deleteItem
to string& objects. But once I do, the compiler no longer recognizes that I am overriding virtual functions (as I've changed their parameters), and declares binaryExpressionTree to be an abstract class. How do I work around this, so that I can override the functions and make binaryExpressionTree non-abstract?
Assuming the abstract class is defined like this:
template <typename elemType>
class binaryTreeType { ... }
You should define your class as follows:
class binaryExpressionTree : public binaryTreeType<String> { ... }
EDIT: original question was edited.
You are incorrectly declaring the overriding functions (inside binaryExpressionTree).
Your declaration is like this:
bool search(const string& searchItem) const = 0;
Such declaration creates a pure virtual method (because of = 0 at the end of the declaration. A pure virtual method (aka an abstract method) is a method which must be overridden by a deriving class. Thus, binaryTreeType declared its methods pure virtual, in order for you to implement, in binaryExpressionTree.
Classes which have abstract methods which are not implemented yet, cannot be instantiated - that is the error your compiler is generating.
Instead, you should declare your methods like this:
virtual bool search(const elemType& searchItem) const;
Such declaration creates regular virtual function, which would override the parent implementation (which is non-existent, at this case).
TL;DR - remove = 0.

Template function taking generic pointer to member function with both const& and by-value implementations

I want to have a template function which accepts unary member-function pointers of an instance of some generic type.
My problem is that I must support both void(T val) and void(const T& val) member functions.
I have written one template function for each case and it works fine, but this leads to code duplication since the function logic is completely the same. (I found something completely similar here: Function taking both pointer to member-function and pointer to const member-function but I fail to see a definitive solution).
An example of the generic type mentioned above:
using UserAddress = std::string;
class User
{
private:
int mHeight;
UserAddress mAddress;
public:
void SetHeight(int height){mHeight = height;}
void SetAddress(const UserAddress& address){mAddress = address;}
};
Where UserAddress is some heavy type I want to pass by reference.
My templated function:
template <typename TPersistentObject>
class Persistence
{
private:
std::map<std::string, std::function<void(User*)>> mSetterOfProperty;
template <typename TPersistentObject, typename TPropertyValue>
void DefinePropertySettingMethod(const std::string& propertyName,
void (TPersistentObject::*propertySetter)(TPropertyValue), std::function<TPropertyValue(void)> dataReader)
{
mSetterOfProperty[propertyName] =
[propertySetter, columnDataReader](TPersistentObject* persistentObject)
{
(persistentObject->*propertySetter)(dataReader());
};
}
};
/// Const& implementation leading to code duplication
template <typename TPersistentObject, typename TPropertyValue>
void DefinePropertySettingMethod(const std::string& propertyName,
void (TPersistentObject::*propertySetter)(const TPropertyValue&), std::function<TPropertyValue(void)> dataReader)
{
...
}
};
Is there some way to define this function to support the following:
int main()
{
auto intDataReader = []() {
return 1;
};
auto stringDataReader = []() {
return UserAddress("Next Door");
};
Persistence p;
p.DefinePropertySettingMethod<User,int>("Height", &User::SetHeight, intDataReader);
p.DefinePropertySettingMethod<User,UserAddress>("Address", &User::SetAddress, stringDataReader);
}
Thanks to Igor Tandetnik 's tip I managed to compile a solution. std::enable_if is not what I needed though since I did not need to deactivate an overload (or at least I couldn't come to a solution using it).
std::conditional did the trick.
Here is the code:
#include <string>
#include <functional>
#include <map>
#include <string>
#include <type_traits>
using UserAddress = std::string;
class User
{
private:
int mHeight;
UserAddress mAddress;
public:
void SetHeight(int height){mHeight = height;}
void SetAddress(const UserAddress& address){mAddress = address;}
};
template <typename TPersistentObject>
class Persistence
{
public:
std::map<std::string, std::function<void(TPersistentObject*)>> mSetterOfProperty;
template <typename TPropertyValue>
void DefinePropertySettingMethod(const std::string& propertyName,
void (TPersistentObject::*propertySetter)(TPropertyValue),
std::function<
typename std::conditional<!std::is_same<TPropertyValue, typename std::decay<TPropertyValue>::type>::value,
typename std::decay<TPropertyValue>::type, TPropertyValue>::type
(void)> dataReader)
{
mSetterOfProperty[propertyName] =
[propertySetter, dataReader](TPersistentObject* persistentObject)
{
(persistentObject->*propertySetter)(dataReader());
};
}
};
int main()
{
std::function<int()> intDataReader = []() {
return 1;
};
std::function<std::string()> stringDataReader = []() {
return UserAddress("Next Door");
};
Persistence<User> p;
p.DefinePropertySettingMethod("Height", &User::SetHeight, intDataReader);
p.DefinePropertySettingMethod("Address", &User::SetAddress, stringDataReader);
}

Cannot convert ‘boost::multiprecision::cpp_int

Giving this error on compilation:-
no matching function for call to ‘to_string(boost::multiprecision::cpp_int&)’ string s = to_string(i);
#include <boost/lexical_cast.hpp>
#include <boost/multiprecision/cpp_int.hpp>
namespace mp = boost::multiprecision;
using namespace std;
#define int long long int
int32_t main()
{
mp::cpp_int l,i;
for(i=l;i<r;i++)
{
string s = to_string(i);
}
return 0;
}
You're including boost::lexical_cast's header file and boost::to_string. Include the proper header file for boost::to_string, which is "boost/exception/to_string.hpp", or use boost::lexical_cast.

Why the function don't work on the string?

#include <iostream>
#include <string>
using namespace std;
void chuli(string a)
{
a.erase(0,2);
}
int main()
{
string a = "012345";
a = chuli(a);
cout << a;
}
I am beginner in C++, I want to know why after this function, this string doesn't change. Is this something about the namespace?
The string is passed by value, so your action is applied to a copy of your object.
If you need to modify your value, you need to pass it by pointer or by reference:
void chuli(string &a)
{
a.erase(0,2);
}
void chuli(string *a)
{
a->erase(0,2);
}

How to write a C++ API class that will allow a user to register its own callback functions?

Let's say a user links his app against a library I wrote and I want to let him specify a callback function that I will call whenever an error occurs in my library. The implementation below works but I want to double check that I'm not missing something here:
Thread safety
DLL initialization issues
Public API considerations (I'm giving away a reference to an instance from the DLL is that OK?)
Anything that could be done better to hide implementation details from the public API?
errordispatcher.h:
#pragma once
#include <functional>
#include <memory>
#include <string>
namespace WE
{
class ErrorDispatcher
{
public:
ErrorDispatcher()
{}
explicit ErrorDispatcher(std::function<void(std::string)> user_func)
: error_callback_func{user_func}
{}
virtual ~ErrorDispatcher(){}
static ErrorDispatcher& getInstance()
{
return instance_;
}
void setErrorCallback(std::function<void(std::string)> user_func)
{
error_callback_func = nullptr;
if (user_func)
error_callback_func = user_func;
}
void dispatchError(std::string message)
{
if (error_callback_func)
error_callback_func(message);
}
private:
explicit ErrorDispatcher(const ErrorDispatcher&) = delete;
explicit ErrorDispatcher(ErrorDispatcher&&) = delete;
ErrorDispatcher& operator = (const ErrorDispatcher&) = delete;
ErrorDispatcher& operator = (ErrorDispatcher&&) = delete;
static ErrorDispatcher instance_;
std::function<void(std::string)> error_callback_func = nullptr;
};
}
NOTE: above I have inline implementation details in the public header to make this post shorter but they will be moved to a .cpp and won't be part of the public header
errordispatcher.cpp:
#include "errordispatcher.h"
namespace WE
{
ErrorDispatcher ErrorDispatcher::instance_;
}
apitest.h
namespace WE
{
void dllFunctionThatMightGiveError();
}
apitest.cpp
#include "errordispatcher.h"
#include "apitest.h"
namespace WE
{
void dllFunctionThatMightGiveError()
{
// Some error happens in dll so call user function and give a message to the user!
ErrorDispatcher::getInstance().dispatchError("Error in DLL!");
}
}
main.cpp (USER APP)
#include "errordispatcher.h"
#include "apitest.h"
#include <iostream>
#include <string>
void error_callback(std::string message)
{
std::cout << message << "\n";
}
int main(void)
{
WE::ErrorDispatcher::getInstance().setErrorCallback(error_callback);
WE::ErrorDispatcher::getInstance().dispatchError("Error in APP!");
WE::dllFunctionThatMightGiveError();
return 0;
}
Output is:
Error in APP!
Error in DLL!

Resources