通过引用传递参数,首先要保证调用函数中已经将相应变量分配了空间,被调用函数中也就不应该再去分配空间了。请看我的修改:
#include
#include
#include
using namespace std;
typedef struct AddNode{
string name;
string num;
struct AddList *piror;
struct AddList *next;
}*Link,*Position;
typedef struct{
Link head,tail;
int len;
int link_size;
}LinkList;
int MakeNode(Link &p,string name,string num)
{
//p=(Link)malloc(sizeof(AddNode));//p's address has been allocated, this is illegal behaviour.
if(!p) return 0;
p->name=name;
p->num=num;
return 1;
}
int main()
{
Link p=new AddNode;//Memmory should be allocated.
string n="Tom";
string m="15671628627";
MakeNode(p,n,m);
cout<name<<"\n"< num< return 0;
}
把错误的提示截图吧,这样子好难判断啊!!
mingw g++ 4.6.1 没问题