I'm trying to parse a math expression into a tree. My question is of what type the "root" argument should be?
I guess this is where my program crushes. Here is the code of that function:
The only thing I see so far is on line 12: for(int i=0; i<= str.size(); ++i)
The variable i would step 1 character past the end of str which should crash when str[i] is accessed. Try it without the =.
i didn't look your code completely but i never used such kind of parameter: pointer to reference.
you have two alternatives:
1. instantiate the TreeNode root object and give it as a pointer argument to your function
2. create it in the function and return it as object: TreeNode TreeParser(string str) { ... return root; }
An advice: learn the basics and differences between instance variable, pointer and reference.
hello all,
I am a beginner in C++. i tried to run the program i copied from this site below but it would execute. i am using C++ version 5.02. Any helps pls.
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
@marko: Pointer references are actually used. They break encalsulation somewhat, but it allows you to change the actual pointer you are passed. E.g. to insert a node in the head of a linked list you can do
void headInsert(Node*& head, T newNodesData)
{
Node* temp = new Node();
temp->data = newNodesData;
temp->next = head;
head = temp;
}
This can only be done by passing head as a pointer reference, otherwise the last line would modify a local copy of head only.