Illegal call of non-static member function

Avgar picture Avgar · Aug 23, 2012 · Viewed 12.4k times · Source

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

main.cpp

#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;
}

main.h

#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

Answer

Andreas Fester picture Andreas Fester · Aug 23, 2012

You need to instanciate your main class and call StringToList as member:

main* m = new main;
main::node *head = m->StringToList("123");
...
delete m;