C++ :: Linked List Set Function - Forbids Comparison Between Pointer And Integer
May 5, 2014
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:
This is my Node struct:
#ifndef NODE_H
#define NODE_H
#include <iostream>
template <typename T>
struct Node {
friend std::ostream &operator <<(std::ostream &os, const Node<T> &node) {
[Code] ....
The following is my set function:
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?
View 7 Replies
ADVERTISEMENT
Apr 21, 2013
I am trying to make a program that prints out an 8 by 8 board with 1 queen on each row.
For example, if the user enters: 0,3,4,0,0,7,6,7
the program should create following board and print:
Q.......
...Q....
....Q...
Q.......
Q.......
.......Q
......Q.
.......Q
I think I am doing it right but im getting a couple of errors that i cant fix ...
#include <iostream>
using namespace std;
int main(){
int x [8] [8], r, c;
for(c = 0; c <= 8; c++){
cout << " Enter a number from 1 to 7 " << endl;
[Code] .....
these are the errors :
assignment15_meem.cpp: In function âint main()â:
assignment15_meem.cpp:7: error: no match for âoperator>>â in âstd::cin >> x[c]â
assignment15_meem.cpp:10: error: ISO C++ forbids comparison between pointer and integer
View 7 Replies
View Related
May 4, 2014
template <class ST>
bool OrderedSet<ST>::IsIn (const ST & value) const {
for (LNode * np = first; np != NULL; np = np -> next)
if (np -> next == value)
return true;
return false;
}
View 1 Replies
View Related
Jul 14, 2014
This is some code that simulates files and directories the same way an operating system does so. I commented out every std::string occurrence because I got the :
terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create
View 3 Replies
View Related
Jan 17, 2014
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
bool deleteNode(struct node *head, struct node *delptr)
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);
#include "stdafx.h"
#include<iostream>
struct node{
[Code].....
View 1 Replies
View Related
Jan 27, 2015
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;
[Code] .....
View 11 Replies
View Related
Nov 30, 2014
Why is it sufficient to only have a pointer to the last node of a doubly linked list?
View 1 Replies
View Related
Nov 30, 2014
Why is it sufficient to only have a pointer to the last node of the list?
View 3 Replies
View Related
Mar 30, 2015
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.
My function:
void removeNode(node *(*nodeToRemove))
{
free((*nodeToRemove)->data);
(*nodeToRemove)->next = NULL;
free(*nodeToRemove);
}
Is this how I should go about freeing this node?
View 5 Replies
View Related
Feb 27, 2015
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:
Code:
typedef struct PCB{
int id;
struct PCB *next;
struct PCB *prev;
}PCB_rec, *PCB_p;
Here is my function to delete the last node (given the pointer is pointing at the last node of the list):
Code:
void del_last_node(PCB_p *process_list){
PCB_p temp = process_list;
if (temp->prev != NULL){
temp = temp->prev;
[Code] ....
And here is how I called the function:
Code: del_last_node(&process_list);
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
View 14 Replies
View Related
Jan 14, 2014
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;
[Code] .....
View 1 Replies
View Related
Nov 30, 2014
Why would it be cumbersome to maintain an additional pointer to the first node in a doubly linked list?
View 1 Replies
View Related
May 19, 2014
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:
#ifndef SLIST_H
#define SLIST_H
#include <stdlib.h>
typedef struct {
void *data;
slist_elem *next;
[code]....
View 2 Replies
View Related
Jul 4, 2013
The log file gives me: In function ‘memFileAlloc’ assignment makes pointer from integer without a cast..When compiling the drivers for the Matrox card in the DL580. The offending code is:
STACK_LINKAGE MEMHANDLE memFileAlloc(
UINT32 dwSize,
const char* pszFileName,
int iLine) {
void* pvChunk;
#if MEMORY_STATS
[code]...
I think the offending line is:
pvChunk = ClientMemAlloc(dwSize + sizeof(UINT32), NULL)
because that's what the log file tells me.
The system is a 16 core HP DL580 G4 with 8g RAM, RAID 0, Mandrivalinux 11.0 and the display is a Matrox Parhelia 256PCIx.
View 11 Replies
View Related
Jul 3, 2013
I have a problem with the parameters of two of my functions:
typedef List<int> Dlist;
template<class T>
Dlist concat(Dlist L1, Dlist L2) {
Dlist L;
elem<T> *temp1, *temp2;
temp1 = L1.Start_ptr;
temp2 = L1.Start_ptr;
[Code] ....
Here are the errors:
no matches converting function `concat' to type `class Dlist (*)(class List<int>, class List<int>)'
error candidates are: template<class T> Dlist concat(Dlist, Dlist)
no matching function for call to `concat(Dlist&, Dlist&)'
I can't understand what the compiler is trying to tell me.
View 4 Replies
View Related
Feb 18, 2013
I'm trying to create a function that allows insertion anywhere in a linked list...here is what I have so far but am a bit confused on where to go from here
Code:
void llAddAtIndex(LinkedList** ll, char* value, int index) {
LinkedList* newNode = (LinkedList*)malloc(sizeof(ll));
newNode = *ll;
LinkedList* prevNode = (LinkedList*)malloc(sizeof(ll));
[Code].....
View 7 Replies
View Related
Jan 20, 2014
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct node {
int data;
struct node *next;
[Code] ....
The above code is a SLL. I have a problem in my deleteNode function. I can't delete the exact data on the list. How to do this right?
View 1 Replies
View Related
Oct 22, 2014
I'm writing a linked list program for class that has a simple text based menu and I believe I am nearly done, it just wont recognize my "count" function as a function and I don't know why. The error comes up at line 70.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int datum;
struct node *next;
} node_t;
[code]....
View 3 Replies
View Related
Mar 19, 2013
I have a linkedList search function given and I am having a little trouble getting it to work. I've made a few attempts with no success. Given normal search code:
template <class Type>
bool orderedLinkedList<Type>::search(const Type& searchItem) const {
bool found = false;
nodeType<Type> *current; //pointer to traverse the list
current = first; //start the search at the first node
[Code] .....
My attempt to make it a recursive search:
template <class Type>
bool orderedLinkedList<Type>::search(const Type& searchItem) const {
//bool found = false;
nodeType<Type> *current; //pointer to traverse the list
current = first; //start the search at the first node
[Code] ....
View 3 Replies
View Related
Oct 7, 2013
I'm having trouble getting my sort function to work,
void Linkedlist::sort(int num)
{
node *q;
node *a;
int input=num;
[Code].....
I get a crash and I've narrowed it down to the last if statement. Also this function is meant to handle new members after 1 member has been added.
View 7 Replies
View Related
Oct 14, 2014
It is suppose to insert items in Linked List in sorted ascending order and no duplicates are allowed.The code complies but it creates duplicates out of order.
ListRetVal CSortedList :: InsertItem(const ListItemType &newItem)
{
ListItemNode* newLNode = new ListItemNode();
newLNode->value=newItem;
newLNode->next=NULL;
[Code].....
View 5 Replies
View Related
Feb 9, 2014
I'm have troubles with this program that requires me to make a search through a Linked List and find a specified value. It also needs to be a template function. I've completed the rest of the program fine and everything runs ok except for the search function. Code below:
Linked.h
#ifndef LINKED_H
#define LINKED_H
#include<iostream>
template <class T>
class Linked {
[Code] ....
I have commented out my attempt at the search function. When uncommenting the function I get several errors about <template> being incorrect syntax.
View 8 Replies
View Related
Feb 9, 2014
The program I have below. If you copy and paste it it should work. However, When I uncomment my search function I get lots of errors and I think it has to do with incorrect syntax of it being a template. Need to do this search function:
Linked.h header file
Code:
#ifndef LINKED_H
#define LINKED_H
#include<iostream>
template <class T>
class Linked {
private:
// Declare a structure for the list
[Code] .....
View 2 Replies
View Related
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.
void InOrder(CTreeNode *nodePtr)
{
if(nodePtr != NULL)
{
InOrder(nodePtr->left);
cout<<nodePtr->value;
InOrder(nodePtr->right);
}
[Code]...
I just don't know how to traverse inorder with the function definition i am given.
View 3 Replies
View Related
Jun 5, 2013
Code:
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct node
{
int data;
struct node *next;
[Code] ....
It fills the singly-linked list with 0 through 9 and calls a function to prompt the user to search for a number. I don't see any glaring errors, I was just wondering what could be done to simplify it or if there's anything I missed.
View 8 Replies
View Related
Feb 23, 2013
I am trying to write a function to return the first element of a link list queue. I am not real sure how to implement this. I have include a copy of the struct for my Node & queue.
Code:
typedef struct event_Node {
void *data;
double arri_time;
double serv_time;
double depart_time;
double start_o_serv;
[Code]...
View 4 Replies
View Related