C++ [Error] invalid use of non-static data member 'Tree::root' - c++11

#include<iostream>
using namespace std;
class Node{
public:
Node *lch,*rch;
char data;
Node(char data){
this->data=data;
this->lch=nullptr;
this->rch=nullptr;
}
};
class Tree{
private:
Node *root;
public:
Tree(){
root=nullptr;
}
void Road(char p,Node *T=root);
public:
void init(Node* &T=root){
char data;
cin>>data;
if(data!='#'){
T=new Node(data);
init(T->lch);
init(T->rch);
}
}
};
void Tree::Road(char p,Node *T){
if(!T) return;
Road(p,T->lch);
Road(p,T->rch);
if(T->data==p){
cout<<T->data<<"\t";
p=T->data;
}
}
int main(){
Tree T;
T.init();
T.Road('A');
return 0;
}
In class Tree, if I use "static Node* root", It would work well. So why I must add "static"?
And when I used "static Node* root",I got an new error that " test.cpp :(.rdata$.refptr._ZN4Tree4rootE[.refptr._ZN4Tree4rootE]+0x0): undefined reference to `Tree::root' "

Node *T=root is illegal in a default argument because default arguments do not have access to this. You can use two overloads instead, one with a Node* parameter and one without.
void Road(char p) { Road(p, root); }
void Road(char p, Node* T);

Related

A container that accumulates its elements metrics

I'm looking into a solution of building containers which track stored size of their elements in addition to basic functions.
So far I didn't saw a solution which doesn't create a huge amount of boilerplate code of each invalidating member of container. This also assumes that stored elements cannot change size after being stored.
Unless standard containers have some feature that allows to inject such behaviour. The following example should be working one, albeit abridged for brevity. The declarations used are:
typedef uint8_t Byte;
typedef Byte PacketId;
template <class T>
struct CollectionTraits {
typedef T collection_type;
typedef typename collection_type::value_type value_type;
typedef typename collection_type::size_type size_type;
typedef typename collection_type::iterator iterator;
typedef typename collection_type::reference reference;
typedef typename collection_type::const_iterator const_iterator;
const_iterator begin() const { return _collection.begin(); }
const_iterator end() const { return _collection.end(); }
iterator begin() { return _collection.begin(); }
iterator end() { return _collection.end(); }
size_type size() const { return _collection.size(); }
protected:
T _collection;
};
struct Packet : CollectionTraits<std::vector<Byte>>
{
PacketId id;
};
The container itself:
struct PacketList : CollectionTraits<std::deque<Packet>>
{
public:
typedef Packet::size_type data_size;
void clear() { _collection.clear(); _total_size = 0; }
data_size total_size() const { return _total_size; }
void push_back(const Packet& v) {
_collection.push_back(v);
_add(v);
}
void push_back(const Packet&& v) {
_collection.push_back(std::move(v));
_add(v);
}
void push_front(const Packet& v) {
_collection.push_front(v);
_add(v);
}
void push_front(const Packet&& v) {
_collection.push_front(std::move(v));
_add(v);
}
void pop_back() {
_remove(_collection.back());
_collection.pop_back();
}
void erase(const_iterator first, const_iterator last) {
for(auto it = first; it != last; ++it) _remove(*it);
_collection.erase(first, last);
}
PacketList() : _total_size(0) {}
PacketList(const PacketList& other) : _total_size(other._total_size) {}
private:
void _add(const Packet& v) { _total_size += v.size(); }
void _remove(const Packet& v) { _total_size -= v.size(); }
data_size _total_size;
};
The interface in result should similar to a standard container. Is there a way to avoid this amount of repeated code? Is there some standard solution for this problem?

warning: friend declaration 'void traverse(List<T>)' declares a non-template function [-Wnon-template-friend]

i have a Node template class in one "Node.cpp" and included in file "List.cpp" and List template class
i have to create a traverse function which is friend to the List class to traverse all the node in a List but facing an error here is my error proner
List Class Decleration
template<typename T>
class List{
private:
Node<T> *headNode;
Node<T> *currentNode;
Node<T> *lastCurrentNode;
int size;
public:
List();
void add(T);
T get();
bool next();
friend void traverse(List<T>); // **there is an issue**
};
Friend function Intialization
template <typename T>
void traverse(List <T>list)
{
Node<T> *savedCurrentNode = new Node<T>();
list.currentNode = list.headNode;
for(int i=1; list.next(); i++)
{
cout << "\n Element " << i << " " << list.get();
}
list.currentNode = savedCurrentNode;
}
i am a bigner and try to implement using templates but stuck into a problem now i have no idea how to solve this

Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion)

I'm having problems with my friend function within my template class. For some reason it doesn't like the fact that I'm trying to use a variable that is type T in an operator overloading friend function.
#include <iostream>
#include <fstream>
#include <string>
template <typename T>
class LL
{
struct Node
{
T mData;
Node *mNext;
Node();
Node(T data);
};
private:
Node *mHead, *mTail;
int mCount;
public:
LL();
~LL();
bool insert(T data);
bool isExist(T data);
bool remove(T data);
void showLinkedList();
void clear();
int getCount() const;
bool isEmpty();
friend std::ofstream& operator<<(std::ofstream& output, const LL& obj)
{
Node* tmp;
if (obj.mHead != NULL)
{
tmp = obj.mHead;
while (tmp != NULL)
{
output << tmp->mData << std::endl; // "tmp->mData is causing the error
tmp = tmp->mNext;
}
}
return output;
}
};
This is a linked list class, and I need the friend function operator overload to basically allow me to output any particular list of objects onto a text file. I hope someone can help me out.

c++11 segmentation fault while playing with variadic templates

So I was Playing around with c++11 Varidiacs, and I wanted to create a thing called CallClass, basically a class that warps a function, for later call,when all variables are set(truly I have No Idea If It can Be Useful):
#include <tuple>
template <typename OBJ,typename F,typename... VARGS>
class CallClass
{
public:
CallClass(OBJ& object,F callFunction)
:_object(&object),_func(callFunction)
{ }
CallClass(const CallClass& other)
:_func_args(other._func_args)
,_object(other._object)
,_func(other._func)
{ }
template <size_t INDEX>
auto get(){ return std::get<INDEX>(_func_args); }
template <size_t INDEX,typename T>
void set(const T& val){ std::get<INDEX>(_func_args) = val; }
template <size_t INDEX,typename T>
void set(T&& val){ std::get<INDEX>(_func_args) = val; }
auto Call()
{
//throws segmentation Fault Here
return InnerCall<0>(_func_args);
}
virtual ~CallClass() {}
protected:
private:
std::tuple<VARGS...> _func_args;
OBJ* _object;
F _func;
template <size_t INDEX,typename... ARGS>
auto InnerCall(std::tuple<VARGS...>& tup,ARGS... args)
{
auto arg = std::get<INDEX>(tup);
return InnerCall<INDEX + 1>(tup,args...,arg);
}
template <size_t INDEX,VARGS...>
auto InnerCall(std::tuple<VARGS...>& tup,VARGS... args)
{
return (_object->*_func)(args...);
}
};
Now when I try to compile(compiling using IDE:code::blocks, configured to use MINGW On windows ), it prints Compiler:Segmentation Fault, anybody any Ideas?
Usage:
class obj{
public:
obj(int a)
:_a(a)
{ }
virtual ~obj() {}
int add(int b,int c){
return _a + b + c;
}
private:
int _a;
};
int main(){
obj ob(6);
CallClass<obj,decltype(obj::add),int,int> callAdd(ob,obj::add);
callAdd.set<0,int>(5);
callAdd.set<1,int>(7);
cout << "result is " << callAdd.Call() << endl;
return 0;
}
After a Bit of a search i stumbled upon a similar issue, in a way.
apparently the way I'm unpacking the tuple is an issue, so i decided to use a different approach as shown in: enter link description here
had to add a few changes to suit my needs:
changes:
namespace detail
{
template <typename OBJ,typename F, typename Tuple, bool Done, int Total, int... N>
struct call_impl
{
static auto call(OBJ& obj,F f, Tuple && t)
{
return call_impl<OBJ,F, Tuple, Total == 1 + sizeof...(N), Total, N..., sizeof...(N)>::call(obj,f, std::forward<Tuple>(t));
}
};
template <typename OBJ,typename F, typename Tuple, int Total, int... N>
struct call_impl<OBJ,F, Tuple, true, Total, N...>
{
static auto call(OBJ& obj,F f, Tuple && t)
{
return (obj.*f)(std::get<N>(std::forward<Tuple>(t))...);
}
};
}
// user invokes this
template <typename OBJ,typename F, typename Tuple>
auto call(OBJ& obj,F f, Tuple && t)
{
typedef typename std::decay<Tuple>::type ttype;
return detail::call_impl<OBJ,F, Tuple, 0 == std::tuple_size<ttype>::value, std::tuple_size<ttype>::value>::call(obj,f, std::forward<Tuple>(t));
}
and changed Call():
auto Call()
{
std::tuple<VARGS...> func_args = _func_args;
return call(*_object,_func, std::move(func_args));
}
I will probably make a few more changes, like passing the tuple as a reference, and making the structs a part of my class.

Run time error in linked list

#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
void deletelist(Node*&head)
{
Node* temp=new Node;
temp=head;
while(head!=NULL)
{
head=head->next;
delete(temp);
temp=head;
}
}
int main() {
Node aman={1,NULL},manjot={2,&aman},sima={3,&manjot},jasbir={4,&sima};
Node* head=&jasbir;
deletelist(head);
return 0;
}
Why this is showing run time error (delete(temp); function is not working here but why)?
Working code for you. Take special note of two scopes in main.
#include <iostream>
using namespace std;
struct Node{
int data;
Node* next;
};
void print(Node*head)
{
while(head!=NULL)
{
std::cout<<head->data<<'\n';
head=head->next;
}
}
int main() {
Node* head;
{
Node aman={1,NULL},manjot={2,&aman},sima={3,&manjot},jasbir={4,&sima};
head=&jasbir;
print(head);
}
{
Node aman={10,NULL},manjot={20,&aman},sima={30,&manjot},jasbir={40,&sima};
head=&jasbir;
print(head);
}
return 0;
}
example on coliru

Resources