C++ :: How To Use Pointer To Function To Traverse The List
Nov 26, 2014
i am having trouble figuring out inorder traversal in this assignment. I was given this function definition in the homework i have to declare it. The problem i am having is how to use pointer to function to traverse the list.
void CBSTree<NodeType>::InOrderTraversal(void (*fPtr)(const NodeType&)) const;
void CBSTree<NodeType>::InOrder(const CTreeNode<NodeType> *const nodePtr
,void (*fPtr)(const NodeType&)) const;
//nodePtr is a pointer to a NodeType object (this is a recursive function, initially this points to the root).
//fPtr is a pointer to a non-member function that takes a const reference to a NodeType object as input, and
//returns nothing
I know without the pointer to function parameter it will be like this.
I'm trying to make a list that contains other lists. I want to use the <list> library /not my own implementation/. Here is what I do:
list<list<int>> Lists; //I create the main list list<int> A, B; //Then I create the other lists //After I fill them with data I add them to the main list Lists.push_back(A); Lists.push_back(B);
The problem comes when I try to go through A and B. I make an iterator of the main list:
list<list<int>> Iter = Lists.begin();
Then I need to make an iterator to traverse through A and B. But I can't make it. I do this:
list<int> Iter2 = Iter.begin() //But the compiler says it is not possible
I'm trying to write a function called 'set' that sets the value of the i'th cell to val on my linkedList, and then returns the previous contents. I am stuck on it due to compiling errors:
template <typename T> T set(Node<T> *head, int i, const T &val) { for(int n = 0; n < i; n++) if(head == val) { val = i; } return val; } #endif
When I try to call it in the main() I get these errors:
node_utils.h: In function ‘T set(Node<T>*, int, const T&) [with T = int]’: node_demo.cpp:26:38: instantiated from here node_utils.h:161:2: error: ISO C++ forbids comparison between pointer and integer [-fpermissive] node_utils.h:162:3: error: assignment of read-only reference ‘val’
So, I understand that I can't compare head & val on my if-statement -- But what do I do?
I was having problems changing the value of my head node I passed it as an argument as head which would be the address. The parameter was defined as struct node *head. like this
I tried manipultaing pointer values to change head node value but it did not work. I saw some code online which used pointer to pointers(in code below) to change head node value it worked I dont fully understand why. Would like better understanding of why.
Would also like to know why the argument call needed &head instead of just head.
remove = deleteNode(&head,found); opposed to remove = deleteNode(head,found);
I am currently working on a Huffman code program for one of my classes. I have gone all the way up to the point where I have created a tree but when I try to traverse it, the program crashes and says there is a segmentation fault. After running the debugger, the error occurs when I try to traverse to the left node (Line 170). The code I have below is not completely finished, I ran across the issue while testing to see that it works. See the code below:
#include <iostream> #include <fstream> #include <cstdlib> #include <vector> #include "Node.h" using namespace std; void fillList(char* sent, int size, vector<Node> &vec);
I have this array which holds 3000 numbers and is initialized by assigning a random number to each element.
Code: #include <stdio.h> #include <stdlib.h> int main (void) { int x[3000] = {rand()}; return 0; }
I have to traverse this array so that each random value lies between -3000 and +3000 inclusive.I'm not sure how to traverse this, there is nothing in my notes discussing traversing.
I'm trying to call a function via a function pointer, and this function pointer is inside a structure. The structure is being referenced via a structure pointer.
Code:
position = hash->(*funcHash)(idNmbr);
The function will return an int, which is what position is a type of. When I compile this code,
I get the error: error: expected identifier before ( token.
Is my syntax wrong? I'm not sure what would be throwing this error.
I've been playing around with making a linked list in C and am having trouble adding to the beginning of the list. For the add_beg() function: the statement "head = newNode" only works locally. This leads me to believe this is a pointer problem. However, the add_end() function works correctly. Ideally, I would like to print "0 1 2 3 4" after using the add_end(root, 0). Currently, print only gives me "1 2 3 4".
#include <stdio.h> #include <stdlib.h> //Create a node struct typedef struct node { //data int val;
I'm having a bit of an issue here. I have a linked list where each node contains a pointer to a string (which has been malloc'd when the node was created and inserted) and a pointer to the next node in the linked list.
I'm creating a function which will free the node (or effectively delete it). However, I'm receiving a free(): invalid pointer error.
//void Fighter::LoadAnimation(...){ std::list<SCML_SFML::Entity*> entities; for(std::map<int, SCML::Data::Entity*>::iterator e = data.entities.begin(); e != data.entities.end(); e++) { SCML_SFML::Entity* entity = new SCML_SFML::Entity(&data, e->first); entity->setFileSystem(&fs); entity->setScreen(&window); entities.push_back(entity);//problem line } }
But if i change entities to a member variable, std::list<SCML_SFML::Entity*> Fighter::m_entities, it does not. Instead i get a write access violation when i try to push_back(entity). i need it to be a member variable because i need to use it in other member functions.
I am having trouble modifying a linked list. I am writing a function to delete the last node from the linked list, but it gave me incompatible types error.Here is my struct:
It gives me the following errors: initialization from incompatible pointer type at line: PCB_p temp = process_list assignment from incompatible pointer type at line: process_list = temp
I am getting and infinite loop for loop pointer variable current which points to head and is incremented by current->next in while loop. I use the pointer variable the same way in my display routine and it works. Here is listing of code.
#include "stdafx.h" #include<iostream> struct node{ int data; node *next;
I am trying to write a generic linked list in c, but for some reason i keep getting errors saying "incompatible pointer type. This is the code and erros:
int example (int [], int, *int,*int,*int,*int); int main () { My code will be here example (int array[], int size, &a,&b,&c,&d); // Like this??? I try it didnt work
error C3867: 'WordParsor::Form1::PutUpfrmIO': function call missing argument list; use '&WordParsor::Form1::PutUpfrmIO' to create a pointer to memberc:userskingc++wordparsorwordparsorForm1.h... and the suggestion fix generate another error.
One person suggested the gcroot<> object wrapper... but I do not know how to modify/declair the function or its argument type.
I was wondering what magic does a * pointer before function actually do? Today our programming teacher asked us to look into it and explain it in the next class!
#include<iostream> using namespace std; int *binary(int []);
As the title says, i'm using a function which returns a pointer to a struct:
the struct is the following:
Code: typedef struct POINT { uint16_t x; uint16_t y; }
Coordinate; the function i'm using:
Code: Coordinate * Read_XTP2046(void) {static Coordinate screen; //calculations to determine the coordinates screen.x=(temp[1]+temp[2])/2; screen.y=(temp[0]+temp[2])/2; // and so on... return &screen;}
The question is: how do i catch this pointer and make it into a Coordinate struct in which i can read the x and y.
In my main program i would do the following:
Code: Coordinate cor; cor = Read_XTP2046();
This does not work, as the function returns a pointer, but how to transform this pointer into a Coordinate struct.