#include <iostream>
class BST {
private:
struct Bst_node
{
int data;
struct Bst_node* left;
struct Bst_node* right;
}*root;
// static Bst_node* get_inorder_predecessor(Bst_node* ptr);
static BST::Bst_node* get_inorder_predecessor(Bst_node* ptr)
{
ptr = ptr->left;
if (ptr == NULL) return NULL;
while (ptr->right != NULL)
{
ptr = ptr->right;
}
return ptr;
}
public:
void create()
{
root = new Bst_node;
root->data = 8;
root->right = NULL;
root->left = new Bst_node;
root->left->data = 10;
root->left->left = NULL;
root->left->right = NULL;
}
void print()
{
Bst_node * ptr = get_inorder_predecessor(this->root);
if(ptr == NULL) std::cout<<"NULL\n";
else std::cout<<ptr->data<<std::endl;
}
};
int main()
{
BST obj;
obj.create();
obj.print();
return 0;
}
The above code is working fine but when i define 'get_inorder_predecessor()' function outside the class it gives error see the below code.
#include <iostream>
class BST {
private:
struct Bst_node
{
int data;
struct Bst_node* left;
struct Bst_node* right;
}*root;
static Bst_node* get_inorder_predecessor(Bst_node* ptr);
public:
void create()
{`
root = new Bst_node;
root->data = 8;
root->right = NULL;
root->left = new Bst_node;
root->left->data = 10;
root->left->left = NULL;
root->left->right = NULL;
}
void print()
{
Bst_node * ptr = get_inorder_predecessor(this->root);
if(ptr == NULL) std::cout<<"NULL\n";
else std::cout<<ptr->data<<std::endl;
}
};
static BST::Bst_node* get_inorder_predecessor(Bst_node* ptr)
{
ptr = ptr->left;
if (ptr == NULL) return NULL;
while (ptr->right != NULL)
{
ptr = ptr->right;
}
return ptr;
}
int main()
{
BST obj;
obj.create();
obj.print();
return 0;
}
See the ERRORS:
'''binary.cpp:33:13: error: 'struct BST::Bst_node' is private within this context
static BST::Bst_node* get_inorder_predecessor(Bst_node* ptr)
^~~~~~~~
binary.cpp:4:12: note: declared private here
struct Bst_node
^~~~~~~~
binary.cpp:33:47: error: 'Bst_node' was not declared in this scope
static BST::Bst_node* get_inorder_predecessor(Bst_node* ptr)
^~~~~~~~
binary.cpp:33:57: error: 'ptr' was not declared in this scope
static BST::Bst_node* get_inorder_predecessor(Bst_node* ptr)
^~~
'''
I have this block of code below, and I cant find out what that class ContractB : public: ContractA means?
#include
using namespace std;
class ContractA
{
unsigned int ether = 0;
public:
ContractA(unsigned int e) :ether(e) {}
auto sendEther() { return ether; }
};
class ContractB : public ContractA
{
unsigned int wei = 1;
public:
ContractB(unsigned int w) :wei(w) {}
auto sendWei() { return wei; }
};
int main()
{
ContractB b(0);
cout << b.sendEther() << " " << b.sendWei();
return 0;
}
It represents inheritance. 'public' is the access specifier that limits the most accessible level for the members inherited from the base class (ContractA).
You can read more about it here.
Member function (reverseString()) has to turn the string but when I create and try to initialize a new pointer in the member function (char *temporary = stringo;), it aparently doesn't see the member of the class (That's not the whole code though).
#include"Header.h"
#include<iostream>
using namespace std;
Stringfuncs::Stringfuncs(char *string)
{
this->stringo = string;
}
int Stringfuncs::lengthString()
{
int counter = 0;
for (int i = 0; stringo[i]; i++) { counter++; }
return counter;
}
char Stringfuncs::*reverseString() //this function doesn't see the member of the class
{
char *temporary = stringo; //identifier "stringo" isn't defined
}
Motivated due to the fact, that the avr-g++ places the vtables in RAM, I wrote a replacement using static polymorphy.
Consider the following example:
volatile uint8_t x;
struct IX {
virtual void f() const = 0;
// virtual ~IX() = default; // need delete
};
struct A : public IX {
const uint8_t v = 0;
void f() const override {
x = v;
}
};
struct B : public IX {
const uint8_t v = 1;
void f() const override {
x = v;
}
};
struct C : public IX {
const uint8_t v = 2;
void f() const override {
x = v;
}
};
volatile uint8_t index = 2;
int main() {
A a;
B b;
C c;
const std::array<const IX*, 3> cc{&a, &b, &c};
cc[index]->f();
while(true) {}
}
Here we have some types A, B and C implementing an interface IX and placing pointers in the array cc. Then we call the virtual function f() for a specific instance. (Using this on a small µC like the AVRs, there is a "waste" of RAM, since the vtables are placed in RAM and each object contains a vptr, and a performance penalty due to the indirect call of f().
So I looked for an alternative solution in this case: the simplest way is to use an heterogenous container like std::tuple and write a switch-statement:
const std::tuple<A, B, C> t;
auto f = [](const auto& v) {
v.f();
};
switch (index) {
case 0:
f(std::get<0>(t));
break;
case 1:
f(std::get<1>(t));
break;
case 2:
f(std::get<2>(t));
break;
default:
assert(false);
break;
}
This yields to optimale machine-code but it is an unflexible solution. So I wrote a metafunction to call f() for a specific element of the tuple:
const std::tuple<A, B, C> t;
Meta::visitAt(t, index, [](const auto& v){v.f();});
And the implementation looks like:
namespace Meta {
namespace detail {
template<uint8_t N>
struct visit {
template<typename T, typename F>
static void at(T& tuple, uint8_t index, const F& f) {
if (index == (N - 1)) {
f(std::get<N - 1>(tuple));
}
else {
visit<N - 1>::at(tuple, index, f);
}
}
};
template<>
struct visit<0> {
template<typename T, typename F>
static void at(T&, uint8_t , const F&) {
assert(false);
}
};
template<typename T, typename F, size_t... I>
void all(const T& tuple, const F& f, std::index_sequence<I...>) {
(f(std::get<I>(tuple)), ...);
}
}
template<typename... T, typename F>
void visitAt(const std::tuple<T...>& tuple, uint8_t index, const F& f) {
detail::visit<sizeof...(T)>::at(tuple, index, f);
}
template<typename... T, typename F>
void visitAt(std::tuple<T...>& tuple, uint8_t index, const F& f) {
detail::visit<sizeof...(T)>::at(tuple, index, f);
}
template<typename... T, typename F>
void visit(const std::tuple<T...>& tuple, const F& f) {
detail::all(tuple, f, std::make_index_sequence<sizeof...(T)>{});
}
}
This works very well in my scenarios, yet is obviously limited to static containers (like std::tuple). There is also a for-each-like iteration Meta::visit().
My question is: are there any other drawbacks / limitations with this approach?
Are there any improvements?
The function I want works within the class butt won't apply to main. Must maintain the initial (Entity *entity = new Nummchange(flarb);)
#include <iostream>
using namespace std;
class Entity
{
public:
Entity(){}
~Entity(){}
virtual int reset(int NUMM) = NULL;
protected:
private:
};
class Nummchange : public Entity
{
public:
Nummchange(int NUMM);
~Nummchange();
int reset(int NUMM);
protected:
private:
int numm;
};
Nummchange::Nummchange(int NUMM)
{
}
Nummchange::~Nummchange()
{
}
int Nummchange::reset(int NUMM)
{
numm = 50;
NUMM = numm;
std::cout << "\nnumm+++++++"<< numm << "\n" << std::endl;
return numm;
}
int main()
{
int flarb = 50;
Entity *entity = new Nummchange(flarb);
while (flarb >= 0)
{
flarb--;
cout << flarb;
if(flarb == 0)
{
entity->reset(flarb);
std::cout << "flarb+++++++"<< flarb << "\n" << std::endl;
}
}
system("pause");
return 0;
}
Success is if the while loop continues perpetually.
int reset(int NUMM); method should take reference of integer type as below.
int reset(int &NUMM);
Make sure that you change the parameter to be reference of integer in all the three places of the method use in the program.