I wish to make a calculator that can calculate numbers of almost any length.
The first function that I needed is one that converts a string into a linked list and then returns a pointer to the head of the list.
However, when compiling I am faced with an error: error C2352: 'main::StringToList' : illegal call of non-static member. Line: 7;
I provide you with my main.cpp and main.h files.
Thanks for any
#include "main.h"
int main()
{
main::node *head = main::StringToList("123");
main::node *temp = new main::node;
temp = head;
while (temp->next != NULL)
{
cout << temp->data;
temp = temp->next;
}
std::cout << "\nThe program has completed successfully\n\n";
system("PAUSE");
return 0;
}
main::node * StringToList(string number)
{
int loopTimes = number.length() - 1;
int looper = 0;
int *i = new int;
i = &looper;
main::node *temp = new main::node;
main::node *head;
head = temp;
for ( i = &loopTimes ; *i >= 0; *i = *i - 1)
{
temp->data = number[*i] - 48;
main::node *temp2 = new main::node;
temp->next = temp2;
temp = temp2;
}
temp->next = NULL;
return head;
}
#ifndef MAIN_H
#define MAIN_H
#include <iostream>
#include <string>
using namespace std;
class main
{
public:
typedef struct node
{
int data;
node *next;
};
node* StringToList (string number);
};
#endif
You need to instanciate your main class and call StringToList as member:
main* m = new main;
main::node *head = m->StringToList("123");
...
delete m;