I'm writing a singly linked list class and I have just started. I'm having problems with
Code:
(*startPointer).name = name;
Here is my code:
main.cpp
Code:
#include <iostream>
#include <cstring>
#include "LinkedList.h"
using namespace std;
int main()
{
cout << "Enter your name:" << endl;
char myName[20];
cin.getline(myName, 20, '\n');
return 0;
}
LinkedList.cpp
Code:
#include <cstdlib>
#include <iostream>
#include <cstring>
#include "../include/LinkedList.h"
//default constructor
LinkedList::LinkedList() : startPointer(NULL), length(0)
{
}
//destructor
LinkedList::~LinkedList()
{
}
bool LinkedList::createNode(char name[])
{
if(startPointer == NULL)
{
startPointer = new aNode;//aNode is a struct
(*startPointer).name = name;
std::cout << "Should be the same: " << (*startPointer).name <<std::endl;
}
return true;
}
LinkedList.h
Code:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
class LinkedList
{
public:
LinkedList();
~LinkedList();
void addToList();
bool createNode(char name[]);
private:
unsigned int length;
struct aNode
{
char name[20];
aNode *next;
};
aNode *startPointer;
};
#endif // LINKEDLIST_H
In constructor 'LinkedList::LinkedList()':
warning: 'LinkedList::startPointer' will be initialized after
warning: 'unsigned int LinkedList::length'
warning: when initialized here
In member function 'bool LinkedList::createNode(char*)':
error: incompatible types in assignment of 'char*' to 'char [20]'
warning: no return statement in function returning non-void