error: non-template 'node' used as template.
note: use 'Ntree<T>:: template node' to indicate that it is a template.
error: need 'typename' before 'Ntree<T>::node' because 'Ntree<T>' is a dependent scope.
I have no idea why im getting these errors. Any help?
So I literally had to write the word 'typename' before my return type on line 7. That fixed the issue (but now I am dealing with other errors). Can anyone explain why this works? I've never had to do this before.
You may want to read: 'Why can’t I separate the definition of my templates class from its declaration and put it inside a .cpp file?' in https://isocpp.org/wiki/faq/templates
There are two way I know.
First way is put definition in .h file and remove .cpp.
The other way is define function in .cpp file and include ".cpp" when you used it
Need help serializing this N-ary tree into a file.
The function call in main looks like this: tree.serialize("foodtree.out");
I stored the N children of the tree in a vector called 'children'. Even though I declared 'children' publicly in a struct within the class, my compiler says it is not within scope when I try to use it in my serialize and serializeRec function. Any idea why this is happening?
// author: Arslan
// CISC 3130 Spring 2016
// Program #3 main for testing
#include <assert.h>
#include <iostream>
#include <string>
#include "Ntree.h"
typedef Ntree<std::string>::tnode node;
int main() {
Ntree<std::string> tree("Food"); // Create tree with root containing the string "food"
// Ntree::getRoot() should return a postd::stringer to the tree's root
// Ntree::node() should have an addChild method that takes a T, allocates a new node with that T as its value, and adds it to the called node's list of children
node* a = tree.getRoot()->addChild("Plant");
node* b = tree.getRoot()->addChild("Animal");
node* c = a->addChild("Roots");
node* d = a->addChild("Leaves");
node* e = a->addChild("Fruits");
node* f = b->addChild("Fish");
node* g = b->addChild("Mammals");
node* h = b->addChild("Birds");
node* i = c->addChild("Potatoes");
node* j = c->addChild("Carrots");
node* k = d->addChild("Lettuce");
node* l = d->addChild("Cabbage");
node* m = e->addChild("Apples");
node* n = e->addChild("Pears");
node* o = e->addChild("Plums");
node* p = e->addChild("Grapes");
node* q = e->addChild("Oranges");
node* r = f->addChild("Salmon");
node* s = f->addChild("Tuna");
node* t = g->addChild("Beef");
node* u = g->addChild("Lamb");
node* v = h->addChild("Chicken");
node* w = h->addChild("Turkey");
node* x = r->addChild("Wild");
node* y = r->addChild("Farm");
node* z = m->addChild("GrannySmith");
node* aa = m->addChild("Gala");
// write toString method
//std::cout << tree.toString() << std::endl;
// write serialize method that takes filepath
tree.serialize("foodtree.out");
// default constructor creates empty tree.
Ntree<std::string> tree2;
// tree2 should become the tree deserialized from filepath
//tree2.deserialize("foodtree.out");
assert(tree==tree2); // overload equality operator
}
Here is my .h file (made by me):
Take a look at some comments I put in here.
#ifndef NTREE_H
#define NTREE_H
#include <iostream>
#include <fstream>
#include <vector>
usingnamespace std;
constint N = 50; // anyway to avoid this global variable?
// If I put it within the class, I get a out of scope error, and I dont know why....
template <class T>
class Ntree
{
public:
struct tnode
{
T value;
tnode* children[N];
tnode(const T& val): value(val){
for(int i = 0; i<N; ++i)
children[i] = 0;
}
tnode* addChild(const T& val)
{
tnode* child = new tnode(val);
for(int i = 0; i<N; ++i)
{
if(children[i] == 0)
{
children[i] = child;
break;
}
}
return child;
}
};
Ntree(): root(0){}
Ntree(const T& val): root(new tnode(val)){}
tnode* getRoot(){ return root; }
booloperator==(const Ntree<T>& rhs)
{
return root->value == rhs.root->value;
}
void serialize (constchar* filepath)
{
std::ofstream outfile(filepath);
serializeRec(root);
}
void serializeRec(tnode* root)
{
if(!root) return;
cout << root->value << endl;
for(int i=0; i<N && root->children[i]; ++i)
{
serializeRec(root->children[i]);
}
cout << "#" << endl;
}
private:
tnode* root;
};
#endif // NTREE_H
My serialize function works, in that it prints out what is expected, however, I get a "program.exe. has stopped working" message pop up. So It seems my program crashed after printing the tree.
Here is my output:
Food
Plant
Roots
Potatoes
#
Carrots
#
#
Leaves
Lettuce
#
Cabbage
#
#
Fruits
Apples
GrannySmith
#
Gala
#
#
Pears
#
Plums
#
Grapes
#
Oranges
#
#
#
Animal
Fish
Salmon
Wild
#
Farm
#
#
Tuna
#
#
Mammals
Beef
#
Lamb
#
#
Birds
Chicken
#
Turkey
#
#
#
#
Process returned -1073741819 (0xC0000005) execution time : 2.233 s
Press any key to continue.
Any idea why my program crashes even though the function seems to work?
In your main, at the very end, I see that you commented out the call to deserialize(), but you did not comment out assert() function, which if you take a look is actually an #include .h file. I am guessing your professor will use that file to check your work? The program seems to work fine if you comment it out for testing.
I would suggest adding in your deserialize funtion, then leave assert() commented out and test to see what happens.
I've commented out my assertion in main(we'll deal with that later). Right now, my call toString() is giving me an error in main, saying no match for 'operator<<'. Do I need to over the << operator?
Would I overload it as a friend function and define it fully within the .h file?