如果把所有的.cpp文件改成.c,程序正常运行。我认为加上extern "C"应该可以正常运行了,但是不行,求解 原理。
student.h
struct Student
{
char name[30];
int score;
struct Student *next;
};
typedef struct Student Stu;
typedef struct Student * Pstu;
#ifdef __cplusplus
extern "C" {
#endif
void add(Stu** head, char* name, int score);
void showall(Pstu head);
Pstu reverse(Pstu head);
int getNum(Pstu head);
Pstu getStu(Pstu head, int score);
int change(Pstu head, int oldscore, int newscore);
void freeall(Pstu head);
#ifdef __cplusplus
}
#endif
Student.cpp
#include"student.h"
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#ifdef __cplusplus
extern "C"{
#endif
void add(Stu** head, char* name, int score)
{
Pstu p1 = *head;
Pstu p2 = (Pstu)malloc(sizeof(Stu));
strcpy(p2->name, name);
p2->score = score;
p2->next = p1;
*head = p2;
}
void showall(Pstu head)
{
Pstu p = head;
while (p)
{
printf("%s,%d", p->name, p->score);
p = p->next;
}
}
#ifdef __cplusplus
}
#endif
main.cpp
#include"student.h"
#include<stdio.h>
#include<stdlib.h>
int main()
{
Pstu head = NULL;
add(&head, "zz", 90);
add(&head, "ymt", 100);
//showall(head);
return 0;
}