【发布时间】:2012-08-23 11:06:56
【问题描述】:
我想做一个计算器,可以计算几乎任何长度的数字。
我需要的第一个函数是将字符串转换为链表,然后返回指向链表头部的指针。
但是,在编译时我遇到了一个错误: 错误 C2352:'main::StringToList':非法调用非静态成员。行:7;
我为您提供了我的 main.cpp 和 main.h 文件。
谢谢大家
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
【问题讨论】:
-
将你的类命名为“main”真的很混乱。
-
int *i = new int; i = &looper;为什么要新建一个整数,然后马上扔掉指向它的指针,让它永远丢失?
标签: c++ function call non-static