错误LNK1169:找到一个或多个多重定义的符号

时间:2013-12-24 12:17:31

标签: c++ compiler-errors

我正在制作Avl树项目,但遇到2个构建错误,即

error LNK1169: one or more multiply defined symbols found
error LNK2005: "struct avl_node * root" (?root@@3PAUavl_node@@A) already defined in AVL.obj 

这是我的代码:

#include "AVL.h"

int main()
{
  AVL obj;
  char pn[30],fn[30],add[20],di[15],dn[30];
  int id,age,nic;
  int ch;
  cout<<"Want To Build BST On Basis Of:"<<endl
    <<"1.NIC"<<endl
    <<"2.ID"<<endl;
  cout<<"Enter Your Choice:";
  cin>>ch;
  switch(ch)
  {
    case 1:
      do{
        cout<<"Patients Database"<<endl
          <<"1.Enter New Record"<<endl
          <<"2.Search Record"<<endl
          <<"3.Delete Record"<<endl
          <<"4.Show Record"<<endl
          <<"5.Exit"<<endl;
        cout<<"What Do You Want To Do:";
        cin>>ch;
        switch(ch)
        {
          case 1: cout<<"Enter Patient ID:";
                  cin>>id;
                  cin.clear();
                  cin.ignore();
                  cout<<"Enter Patient Name:";
                  gets(pn);
                  cout<<"Enter Father Name:";
                  gets(fn);
                  cout<<"Enter Patient's Age:";
                  cin>>age;
                  cout<<"Enter Patient NIC:";
                  cin>>nic;
                  cin.clear();
                  cin.ignore();
                  cout<<"Enter Address:";
                  gets(add);
                  cout<<"Enter Disease:";
                  gets(di);
                  cout<<"Enter Doctor Name:";
                  gets(dn);
                  root=obj.adddatanic(root,id,pn,fn,age,nic,add,di,dn);
                  break;

          case 2: obj.searchbynic();
                  break;

          case 3:
                  break;

          case 4: cout<<"Show Record:-"<<endl
                  <<"1.Show Greatest NIC"<<endl
                    <<"2.Show Least NIC"<<endl
                    <<"3.Show All Records In Ascending Order"<<endl
                    <<"4.Show All Records In Descending Order"<<endl;
                  cout<<"Enter Your Choice:";
                  cin>>ch;
                  switch(ch)
                  {
                    case 1: obj.showgreatestnic();
                            break;

                    case 2: obj.showleastnic();
                            break;

                    case 3: obj.showascendingnic();
                            break;

                    case 4: obj.showdescendingnic();
                            break;
                  }
                  break;

          case 5: exit(-1);

          default: cout<<"Wrong Entry"<<endl;
                   break;
        }
      }
      while(ch!=5);
      break;

    case 2:
      do{
        cout<<"Patients Database"<<endl
          <<"1.Enter New Record"<<endl
          <<"2.Search Record"<<endl
          <<"3.Delete Record"<<endl
          <<"4.Show Record"<<endl
          <<"5.Exit"<<endl;
        cout<<"What Do You Want To Do:";
        cin>>ch;
        switch(ch)
        {
          case 1: cout<<"Enter Patient ID:";
                  cin>>id;
                  cin.clear();
                  cin.ignore();
                  cout<<"Enter Patient Name:";
                  gets(pn);
                  cout<<"Enter Father Name:";
                  gets(fn);
                  cout<<"Enter Patient's Age:";
                  cin>>age;
                  cout<<"Enter Patient NIC:";
                  cin>>nic;
                  cin.clear();
                  cin.ignore();
                  cout<<"Enter Address:";
                  gets(add);
                  cout<<"Enter Disease:";
                  gets(di);
                  cout<<"Enter Doctor Name:";
                  gets(dn);
                  root=obj.adddataid(root,id,pn,fn,age,nic,add,di,dn);
                  break;

          case 2: obj.searchbyid();
                  break;

          case 3:
                  break;

          case 4: cout<<"Show Record:-"<<endl
                  <<"1.Show Greatest ID"<<endl
                    <<"2.Show Least ID"<<endl
                    <<"3.Show All Records In Ascending Order"<<endl
                    <<"4.Show All Records In Descending Order"<<endl;
                  cout<<"Enter Your Choice:";
                  cin>>ch;
                  switch(ch)
                  {
                    case 1: obj.showgreatestid();
                            break;

                    case 2: obj.showleastid();
                            break;

                    case 3: obj.showascendingid();
                            break;

                    case 4: obj.showdescendingid();
                            break;
                  }
                  break;

          case 5: exit(-1);

          default: cout<<"Wrong Entry"<<endl;
                   break;
        }
      }
      while(ch!=5);
      break;
  }
  return 0;
}

这是我的AVL.h文件。

#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
#include<cstdio>
#include<sstream>

#define pow2(n) (1 << (n)

using namespace std;

   struct avl_node
   {
avl_node* left;
avl_node* right;
char pname[30]; //Patient Name
char fname[30]; //Father Name
int age;
int nic;
char address[20];
char disease[15];
char dname[30]; //Doctor Name
int p_id; //Pateint Id
   }*root;

      class AVL
    {
     public:
AVL();
int height(avl_node*);
int diff(avl_node*);
avl_node* r_rotation(avl_node*); //Right Rotation
avl_node* l_rotation(avl_node*); //Left Rotation
avl_node* lr_rotation(avl_node*); //Left-Right Rotation
avl_node* rl_rotation(avl_node*); //Right-Left Rotation
avl_node* balance(avl_node*);
//Functions For NIC Key
avl_node* getdatanic(avl_node*,int,char[],char[],int,int,char[],char[],char[]);
avl_node* adddatanic(avl_node*,int,char[],char[],int,int,char[],char[],char[]);
void searchbynic();
void showgreatestnic();
void showleastnic();
void showascendingnic();
void ascendingnic(avl_node*);
void showdescendingnic();
void descendingnic(avl_node*);
//Functions For ID Key
avl_node* getdataid(avl_node*,int,char[],char[],int,int,char[],char[],char[]);
avl_node* adddataid(avl_node*,int,char[],char[],int,int,char[],char[],char[]);
void searchbyid();
void showgreatestid();
void showleastid();
void showascendingid();
void ascendingid(avl_node*);
void showdescendingid();
void descendingid(avl_node*);
void delete_all(avl_node*);
~AVL();
    };

1 个答案:

答案 0 :(得分:5)

最有可能的是,您已经在root标题中定义了 - 而不是声明 - AVL.h,并且您有多个文件包含AVL.h标题(没有多少文件)如果只有一个文件包含它,则指向一个标题),因此您可以对变量root进行多次定义。

你有机会写下:

avl_node *root;

(可能包括= 0之类的初始值设定项)而不是正确的:

extern avl_node *root;
头文件中的

。修复是双重的:

  1. 在标题中包含extern(并确保没有初始值设定项)。
  2. 在适当的源文件中定义avl_node *root;(可能是AVL.cpp,或者您用于C ++源代码的任何扩展名。)

  3. 您的标题包含:

    struct avl_node
    {
        avl_node* left;
        avl_node* right;
        char pname[30]; //Patient Name
        char fname[30]; //Father Name
        int age;
        int nic;
        char address[20];
        char disease[15];
        char dname[30]; //Doctor Name
        int p_id; //Patient Id
    } *root;
    

    它需要包含:

    struct avl_node
    {
        avl_node* left;
        avl_node* right;
        char pname[30]; //Patient Name
        char fname[30]; //Father Name
        int age;
        int nic;
        char address[20];
        char disease[15];
        char dname[30]; //Doctor Name
        int p_id; //Patient Id
    };
    
    extern avl_node *root;
    

    AVL.cpp中的代码需要有额外的行:

    avl_node *root = 0;
    

    所有东西都需要重新编译。

    (我们将struct vs class的设计选择留给另一天。)