It's giving me this error for lines 21 and 22, which are the ones I've noted. Judging from other cases with similar error messages, I've got a syntax error somewhere. I just can't figure out what.. Here's my .cpp file:
#include <iostream>
#include <cstdlib>
#include "deque.h"
using namespace std;
struct node{
int data;
node *prev;
node *next;
};
Deque::Deque(){
count = 0;
node->head->next = node->head; //error on this line
node->head->prev = node->head; //and this one
}
Here's my header file:
# ifndef DEQUE_H
# define DEQUE_H
class Deque
{
private:
int count;
class node *head;
public:
Deque();
~Deque();
int size();
void addFirst(int);
void addLast(int);
int removeFirst();
int removeLast();
int getFirst();
int getLast();
};
#endif
Correct code for these lines:
head->next = head;
head->prev = head;
Your variable is named head, and node is its type, but there is no member named node in your class Deque
struct node has no member named head, which is a problem.
Where is your node variable coming from in Dequeue()? Looks undefined given the code that you have posted. node is a type, not a variable.
In C++ there is no need to prefix each declaration of a struct type variable with struct. If it needs to be C compatible you can always typedef the struct as well.
Related
Okay so I am trying to understand the internals of a LinkedList and I have made two classes, Node and LinkedList. For some reason, the node constructor is spitting out an int rather than a node.
Node* head; // throws " error: invalid conversion from 'int' to 'Node*' [-fpermissive]"
int whyDoesThisWork = new Node(); //by god this works
The first case was annoying, but the second is actually kind of funny.
Here is my Node.h:
#ifndef NODE_H_
#define NODE_H_
class Node {
public:
Node();
Node(int val);
~Node();
int value;
Node* next;
};
#endif /* NODE_H_ */
My Node.cpp:
#include "Node.h"
#include <stdlib.h>
Node::Node() {
this->value = 0;
this->next = NULL;
}
Node::Node(int val) {
this->value = val;
this->next = NULL;
}
I am absolutely clueless to why a Node constructor is returning an int. Any insight would be appreciated
Edit
Here is my driver file:
#include <iostream>
#include "LinkedList.h"
#include "Node.h"
using namespace std;
int main() {
Node* head;
int* val = new Node();
return 0;
}
Node* head; is just a declaration of an uninitialized pointer, it can't throw a conversion error.
int whyDoesThisWork = new Node(); will fail to compile, because a pointer cannot be assigned to an int (without an explicit type-cast). This is a type mismatch. Are you sure the compiler is not actually saying error: invalid conversion from 'Node*' to 'int' instead?
int* val = new Node(); will also fail to compile, because a Node* pointer is not an int* pointer. This is also a type mismatch.
C++ is a strongly typed language. It is not forgiving of type mismatches, unless implicit conversions are defined for the types in question, which is not the case here.
Hey guys so I am getting this error when I am trying to create this function for my linked list class. The function I am having problems with is my search function. I haven't even started creating the function yet but the error I am receiving is in the declaration of the search function. On line 38 under NodePtr it says it is undefined and under search it says Error: declaration incompatible with "LinkedList::NodePtr (declared on line 17). The code is below. Any help appreciated.
// LinkedListProject.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <list>
using namespace std;
class LinkedList {
public:
struct Node {
int data;
Node* link;
};
typedef Node* NodePtr;
//NodePtr head = new Node;
void head_insert(NodePtr& head, int the_number);
NodePtr search(NodePtr head, int target);
private:
};
int main()
{
LinkedList obj;
//obj.head->data = 3;
//obj.head->link = NULL;
return 0;
}
void LinkedList::head_insert(NodePtr& head, int the_number) {
NodePtr temp_ptr = new Node;
temp_ptr->data = the_number;
temp_ptr->link = head;
head = temp_ptr;
}
NodePtr LinkedList::search(NodePtr head, int target)
{
return NodePtr();
}
You must set the right scope where NodePtr is defined.
LinkedList::NodePtr LinkedList::search(NodePtr head, int target)
{
return LinkedList::NodePtr();
}
NodePtr is a name scoped to you class. In order to use it outside of the class you need LinkedList::NodePtr. So you have to change
NodePtr LinkedList::search(NodePtr head, int target)
to
LinkedList::NodePtr LinkedList::search(NodePtr head, int target)
Now you may ask, "But wait, I didn't need it in search, what gives?", and the answer to that is after you do
LinkedList::search
The class name is injected into the rest of the scope of the function. Because of this we do not need to explicitly qualify any name that is scoped to the class.
im trying to learn linked lists in c++; my code look like this
#include <iostream>
#include <string>
using namespace std;
struct node{
int number;
node *next;
};
struct zoznam{
node *head=NULL;
node *tail=NULL;
};
void insertAsFirst(node *&head,node *&last, int number){
node *tmp = new node;
tmp->number = number;
tmp->next=NULL;
head=tmp;
last=tmp;
}
void insertValues( node *&head , node *&last, int number){
if(head==NULL){
insertAsFirst(zoznam->head,zoznam->tail,number);
}else{
node *tmp = new node;
tmp->number=number;
tmp->next=NULL;
last->next=tmp;
//last=tmp;
}
}
int main()
{
for( int i = 0; i < 10; i++){
insertValues(zoznam->head,zoznam->tail,i);
}
node *current=zoznam->head;
while(current!=NULL){
cin<<current->number << endl;
current=current->next;
}
return 0;
}
expected primary-expression before '->' token|
Being new to c++ , i have no idea what the error means , how can i fix that , i tried to check it up but found nothing. Thanks
As you're new to C++, I am giving you some details.
When you are using struct I mean you define any struct then it becomes a type name not a variable. To use that you need to declare variable(s) of that type.
Here, in the main, you're using zoznam, which is a type name and the definition of that type is the definition of the struct.
Declaration: zoznam var_name;
Use: var_name.head; or 'var_name.tail;'.
Learn more about struct and pointer. Read the documentations carefully not only the example codes in this two link.
So I have been working with linked lists and I am trying to assign temp variable to the first node in the list but it errors out
code where I set a temp node to list
Node *temp = NULL;
Node *found = NULL;
bool isfound = false;
temp = list;
the place where list defined in LinkedList class
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include "Node.h"
class LinkedList{
private:
Node *list;
Node *createNode();
Node *searchLocation(int);
public:
LinkedList();
~LinkedList();
void insertNode();
void deleteNode(int);
void printList();
void searchNode();
};
#endif
node header
#ifndef NODE_H
#define NODE_H
class Node{
public:
char lastName[20];
char firstName[20];
int idNumber;
Node *next;
Node *head;
Node(char a[], char b[], int i);
void printNode();
};
#endif
node class
#include <iostream>
#include "Node.h"
using namespace std;
Node::Node(char a[], char b[], int i){
*lastName = *a;
*firstName = *b;
idNumber = i;
}
void Node::printNode(){
cout<<lastName<<", "<<firstName<<": "<<idNumber<<endl;
}
error says can't convert from LinkedList* to Node* in assignment temp = list;
You haven't shown where you declare the list object, but from the error I can see it is of type LinkedList*.
If you want to point to the first node, then you have to assign
temp = list->list.
Of course since list is a private member of LinkedList, you cannot do this outside of the LinkedList class.
So you need to provide an accessor function that returns this node object.
Assuming that list is a LinkedList *, then temp = list->list; should work.
Situation: I am attempting to create a range of methods within a Nodes class, all of which will use a struct "listnode" composed of playerName (string) and next (listnode). I have created the struct within a header file as I will be using the struct in the main class also.
Error: When I compile, I get an unusual error, its an error "c4430: missing type specifier - int assumed. Note: C++ does not support default int" I get this error on like 8.
#ifndef STRUCTS_H
#define STRUCTS_H
#include <Windows.h>
#include <string>
typedef struct
{
string playerName;
listnode * next;
} listnode;
#endif
If you are compiling as C++ , you should be able to do:
struct listnode
{
string playername;
listnode* next;
};
(no need for typedef here)
If you want to be able to compile in C, you will need to use a tag-name for the struct:
typedef struct listnode_tag
{
string playername;
struct listnode_tag* next;
} listnode;
(Obviously string may need std::string to work in C++, and you should have a #include <string> in this file, just to make sure it's "complete" on its own).
string lives in the std namespace, so refer to it as std::string. You also don't need the typedef syntax in C++:
#include <string>
struct listnode
{
std::string playerName;
listnode * next;
};
Make it:
typedef struct listnode
{ ^^^^^^^^
std::string playerName;
^^^^^
struct listnode * next;
^^^^^^
} listnode;