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.
Related
I am just starting my implementation of my Skip List, and I have tried everything to get rid of this error. My Build Succeeds, but when I put "SList p" into my main program, it fails and said linked reference ld error. What is the problem?
Here is my .h file
#ifndef SLIST_H
#define SLIST_H
#include <string>
#include <fstream>
class SList {
public:
SList();
// overloaded assignment operator
SList& operator = (const SList&);
// copy constructor
SList(const SList&);
// destructor
~SList();
private:
const static int DUMMY_VALUE = -1000;
struct Node {
int data;
Node *next;
};
struct upperNode {
upperNode *next;
Node *down;
};
Node *head;
upperNode *upperHead;
int length;
};
#endif // SLIST_H
__
My .cpp file
#include "SList.h"
SList::SList() {
length = 0;
head->data = DUMMY_VALUE;
head->next = nullptr;
upperHead->down = head;
upperHead->next = nullptr;
}
and my main
#include "SList.h"
#include <iostream>
int main() {
SList p;
return 0;
}
What am I missing here? I feel like it is terribly obvious I just need a second set of eyes. Thank you.
You have declared a copy assignment operator, a copy constructor and a destructor, but you don't provide implementations.
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.
I feel this question may be a bit trivial, but I simply cannot wrap my head around it. I currently have a class, Node, which is trying to point to what node occurs before the current node using the pointer prevNode. However I seem unable to access any variables within prevNode.
When running Main.cpp from the following code, it prints the result '15340756'. Where am I going wrong? Appologies as Im still a bit new to C++.
Node.h
#include "stdafx.h"
class Node
{
public:
Node();
void setPrevNode(Node n);
Node getPrevNode();
int i;
private:
Node *prevNode;
};
Node.cpp
#include "stdafx.h"
#include "Node.h"
Node::Node(){
i = 0;
}
void Node::setPrevNode(Node n){
prevNode = &n;
}
Node Node::getPrevNode(){
return *prevNode;
}
Main.cpp
#include "stdafx.h"
#include "Node.h"
int _tmain(int argc, _TCHAR* argv[])
{
Node nodes[] = {Node(), Node()};
nodes[0].i = 1;
nodes[1].setPrevNode(nodes[0]);
printf("%i", nodes[1].getPrevNode().i);
while(true){
}
return 0;
}
void setPrevNode(Node n);
Here setPrevNode is declared to take a copy of the node passed as an argument, and point to such node. After the function returns, the pointed to node no longer exist and what you get is undefined behavior.
What you want is to take the Node either as a reference or a pointer instead:
void setPrevNode(Node& n)
{
prevNode = &n;
}
void setPrevNode(Node* n)
{
prevNode = n;
}
On the same line, getPrevNode is defined to return a copy of the previous node. You most certainly want to return a reference here instead, although you can also return a pointer:
Node& getPrevNode()
{
return *prevNode;
}
Node* getPrevNode()
{
return prevNode;
}
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.
I am trying to build a Linked list application using C++ programming language & features such as inheritance etc.
I have split the interface & implementation in different files but not able to compile.
Below are the list of files
Interface files :- node.h , abstractList.h , singleLinkedList.h
Implementation files: singleLinkedList.cpp
node.h
#ifndef NODE_H
#define NODE_H
#include <iostream>
struct nodeType {
int data;
struct nodeType *next;
}listNode;
#endif
abstractList.h
#ifndef ABSTRACT_LIST_H
#define ABSTRACT_LIST_H
#include <iostream>
#include "node.h"
#include "singleLinkedList.h"
class abstractList {
public:
virtual ~abstractList();
virtual bool isEmpty(Node* ) = 0;
virtual int get(const int&) = 0;
virtual int indexOf(const int& ) = 0;
virtual Node insert(const int& , const int& ) = 0;
virtual void delete(const int& ) = 0;
};
#endif
singleLinkedList.h
#ifndef SINGLE_LIST_H
#define SINGLE_LIST_H
#include <iostream>
#include "node.h"
#include "abstractList.h"
class singleLinkedList : public abstractList {
public:
singleLinkedList();
~singleLinkedList();
Node populateList( );
private:
void checkIndex();
int data;
Node head;
};
#endif
So far i have just coded the populateList() function in the implentation file, here goes the implementation file.
singleLinkedList.cpp
#include <iostream>
#include "node.h"
#include "singleLinkedList.h"
#include "abstractList.h"
Node singleLinkedList :: populateList()
{
Node temp;
int data;
temp = head;
char ch;
std::cout<<"Enter Data? (y/n) " << std::endl;
std::cin>>ch;
while(ch == 'Y' || ch == 'y')
{
std::cout<<"Enter the data that you would like to store.\n"<<std::endl;
std::cin>>data;
temp = new Node();
temp->data = data;
temp->next = head;
head = temp;
std::cout<<"Enter more data?"<<std::endl;
std::cin>>"\n">>ch;
}
return temp;
}
When i give g++ -c singleLinkedList.cpp , i am getting lot of errors. I am pretty sure i have done something stupid. Can anyone please pin point my error?
EDIT: Error Log With specfic issues.
struct nodeType {
int data;
struct nodeType *next;
}listNode;
virtual listNode *insert();
Is the above statement correct?
Thanks
Kelly
delete is a keyword in C++, you can't use it as a method name. You need to use a different name here:
class abstractList {
public:
//...
virtual void delete(const int& ) = 0;
//-----------^^^^^^ rename this.
};
The problem is in your typedef:
typedef listNode *Node;
means that all instances of Node will essentially replaced by listnode*
temp = new Node();
actually reads
temp = new listnode*();
But new Foo() would return a Foo* (because new returns a pointer to memory allocated for an object), meaning that new listnode*() would return a listnode**. temp being a listnode* has no Idea what a listnode** is and complains.
what you want to do is:
Node temp = new listnode();
or forget the typedef altogether:
listnode* temp = new listnode();