C++;:对象错误的多重定义 我最近开始在C++中使用文件结构,但没有什么成功。该项目被拆分为以下文件 -groups.h -groups.cpp -people.h -people.cpp -main.cpp

C++;:对象错误的多重定义 我最近开始在C++中使用文件结构,但没有什么成功。该项目被拆分为以下文件 -groups.h -groups.cpp -people.h -people.cpp -main.cpp,c++,makefile,include,C++,Makefile,Include,有两个基类,组和玩家以及其中任何一个继承的其他类 这是文件 h组 而且所有的代码都在一个文件中,它现在的工作方式 当我尝试制作程序时,出现以下错误 gcc -o main main.o groups.o people.o groups.o:(.bss+0x0): multiple definition of `players' main.o:(.bss+0x0): first defined here groups.o:(.bss+0x20): multiple definiti

有两个基类,
玩家
以及其中任何一个继承的其他类

这是文件

h组 而且所有的代码都在一个文件中,它现在的工作方式

当我尝试制作程序时,出现以下错误

gcc -o main main.o groups.o people.o 
groups.o:(.bss+0x0): multiple definition of `players'
main.o:(.bss+0x0): first defined here
groups.o:(.bss+0x20): multiple definition of `directors[abi:cxx11]'
main.o:(.bss+0x20): first defined here
groups.o:(.bss+0x40): multiple definition of `fans'
main.o:(.bss+0x40): first defined here
people.o:(.bss+0x0): multiple definition of `players'
main.o:(.bss+0x0): first defined here
people.o:(.bss+0x20): multiple definition of `directors[abi:cxx11]'
main.o:(.bss+0x20): first defined here
people.o:(.bss+0x40): multiple definition of `fans'
main.o:(.bss+0x40): first defined here

...

collect2: error: ld returned 1 exit status
makefile:3: recipe for target 'footballmaker' failed
make: *** [footballmaker] Error 1
整个错误超过了400行,它是附加的

我不确定如何包含文件,这样我就不会复制它们,因为程序运行需要这些文件,有没有更好的方法将代码拆分成文件

您可以在头文件
people.h
的文件范围内定义(而不仅仅是声明!)变量。这种变量定义在链接时对所有其他转换单元可见。如果不同的翻译单元,例如
people.cpp
main.cpp
,现在包括
people.h
,那么这就好像这些变量定义直接写入了
people.cpp
main.cpp
,每次都在全局范围内定义一个具有相同名称的单独变量

为了克服这个问题,在头文件中声明变量,但只在一个翻译单元中定义它,例如
people.cpp
。仅声明一个变量就意味着将关键字
extern
放在它前面(告诉编译器在链接时,变量定义将由不同的翻译单元提供):

//people.h
向量播放器;
外部标准:列出董事;
外部标准::矢量风扇;
//人民网
向量机;
std:上市董事;
矢量风扇;

复制并粘贴问题中的代码Don't link to itOne of header files在文件范围内定义变量(或对象),并且该header包含在多个源(.cpp)文件中。每个变量的结果都是多定义错误。将所有命名变量(
players
directors
fans
等)的声明移出头文件,并将它们精确地移动到一个源文件中。
#include <algorithm>
#include <iostream>
#include <vector>
#include <typeinfo>
#include <boost/units/detail/utility.hpp>
#include <cstdlib>
#include <fstream>
#include <list>
#include "groups.h"

using namespace std;

//Fan Club member functions 
FanClubs::FanClubs(string name, int id) {
    clubName = name;
    clubId = id;
    fanclubs.push_back(*this);
};

void FanClubs::showFans() {
    cout << "Players in " << fanclubs.begin() -> clubName << endl;
    fan.showFanas();
}

void FanClubs::addFans() {
    int choice = 0;
    cout << "1. Add a bunch of fans\n2. Add custom fans\nChoice: ";
    cin >> choice;
    switch(choice) {
        case 1: {
            int requirement;
            cout << "How many fans do you need: ";
            cin >> requirement;
            static const string names[] = {
                "Margarita", "Amalia", "Sam", "Mertie", "Jamila", "Vilma", 
                "Mazie", "Margart", "Lindsay", "Kerstin", "Lula", "Corinna", "Jina",
                "Jimmy", "Melynda", "Demetrius", "Beverly", "Olevia", "Jessika", 
                "Karina", "Abdallah", "Max", "Prateek", "Aghaid"
            };
            for (int i = 0; i < requirement; ++i) {
                fan.name = names[rand() % 24];
                fan.age = (rand() % 80 + 1);
                fan.sex = ((rand() % 2) ? 'M' : 'F');
                fan.under_auth = false;
                fan.auth_level = 0;
                fans.push_back(fan);
            }
            break;   
        }           
        case 2: {
            int requirement;
            cout << "How many fans you want to add?\nnumber: ";
            cin >> requirement;
            for (int i = 0; i < requirement; ++i) {
                cout << "======Fan " << i + 1 << "=======\n";
                cout << "Enter name: ";
                cin >> fan.name;
                cout << "Enter age: ";
                cin >> fan.age;
                cout << "Enter sex: ";
                cin >> fan.sex;
                fan.under_auth = false;
                fan.auth_level = 0;
                fans.push_back(fan);
            }   
            break;
        }
        default: 
        cout << "Incorrect choice\n";
        break;
    }
}

//Soccer Teams member functions 
string SoccerTeams::nameTeam(int id) {
    return teams.begin() -> teamName;
}

void SoccerTeams::showPlayers() {
    cout << "Players in " << teams.begin() -> teamName << endl;
    player.showPlayas();
}

void SoccerTeams::showManagers() {
    int counter = 1;
    list<ManagingDirectors>::iterator i;
    for (i = directors.begin(); i != directors.end(); i++) {
        cout << "Director " << counter << endl;
        cout << "Works for team " << nameTeam(i -> directorId) << endl;
        cout << "Name: " << i -> name << endl;
        cout << "Sex: " << i -> sex << endl;
        counter++;
    }
}

void SoccerTeams::addPlayer() {
    int newId;
    int number;
    cout << "Number of players to be added: ";
    cin >> number;
    for (int i = 0; i < number; ++i) {
        cout << "\nEnter player name: ";
        cin >> player.name;
        cout << "Enter sex(M/F): ";
        cin >> player.sex;
        cout << "Enter age: ";
        cin >> player.age;
        cout << "Enter player id(0 for random id): ";
        cin >> newId;
        newId == 0 ? player.playerId = (rand() % 100 + 1) : player.playerId = newId;
        player.under_auth = true;
        player.auth_level = 0;
        players.push_back(player);
        teams.begin()->num_of_people++;
    }
}

void SoccerTeams::deletePlayer(int id) {
    std::vector<Players>::iterator i;
    for (i = players.begin(); i != players.end(); ) {
        if(i->playerId == id) {
            i = players.erase(i);
            teams.begin()->num_of_people--;
        }
        else
            i++;
    }
}

void SoccerTeams::showTeam() {
    vector<SoccerTeams>::iterator i;
    for (i = teams.begin(); i != teams.end(); ++i) {
        cout << "\nTeam name: " << i -> teamName << endl;
        cout << "Team id: " << i -> teamId << endl;
        cout << "Number of players: " << i -> num_of_people << endl;
        cout << "Average age: " << i -> player.ageCalc()/teams.begin() -> num_of_people << endl;
    }
}

SoccerTeams::SoccerTeams(string tn, int id) {
    teamName = tn;
    teamId = id;
    teams.push_back(*this);
};

void SoccerTeams::addManager() {
    ManagingDirectors mandir;
    int number;
    cout << "How many managers you want to add: ";
    cin >> number;
    for (int i = 0; i < number; i++) { 
        cout << "Manager " << i + 1 << endl;
        cout << "Enter name of the director: ";
        cin >> mandir.name;
        cout << "Enter the age: ";
        cin >> mandir.age;
        cout << "Enter the sex(M/F): ";
        cin >> mandir.sex;
        mandir.directorId = teams.begin() -> teamId;
        mandir.auth_level = 3;
        mandir.under_auth = false;
        directors.push_front(mandir);
    }
}

void SoccerTeams::exportToFile(const char *filename) {
    ofstream outfile;
    outfile.open(filename, ios::out);
    vector<Players>::iterator i;
    int counter = 1;
    outfile << "Team Data" << endl;
    outfile << "Team name  : " << teamName << "\nPlayers    : " << teams.begin() -> num_of_people << endl;
    outfile << "Average age: " << teams.begin() -> player.ageCalc()/teams.begin() -> num_of_people << endl;
    for (i = players.begin(); i != players.end(); ++i) {
        outfile << "\nPlayer " << counter << endl;
        outfile << "Name: " << i -> name << endl;
        outfile << "Sex : " << i -> sex << endl;
        outfile << "Age : " << i -> age << endl;
        outfile << "Pid : " << i -> playerId << endl;
        counter++;
    }

    outfile.close();
}
#ifndef PEOPLE_H
#define PEOPLE_H

//People base class
class people {
    string name;
    char sex;
    int age;
    bool under_auth;
    int auth_level;
    friend class SoccerTeams;
    friend class Players;
    friend class Fans;
    friend class FanClubs;
public:
    //virtual string getclass() { return char2str(typeid(*(this)).name()); }
    people(){};
    ~people(){};    
    //virtual int get_age(){ return this->age; }; 
};

//players class people
class Players : public people {
    int playerId;
    int avgAge;
    friend class SoccerTeams;
public:
    void showPlayas();
    float ageCalc();
    Players(){};
    ~Players(){};
};
std::vector<Players> players;

//Class Managing Directors people 
class ManagingDirectors : public people {
    int directorId;
    friend class SoccerTeams;
public:
    ManagingDirectors(int);
    ManagingDirectors() {};
    ~ManagingDirectors(){};
};
std::list<ManagingDirectors> directors;


//Fans people class
class Fans : public people {
public:
    void showFanas();
    Fans(){};
    ~Fans(){};
};
std::vector<Fans> fans;

#endif
#include <algorithm>
#include <iostream>
#include <vector>
#include <typeinfo>
#include <boost/units/detail/utility.hpp>
#include <cstdlib>
#include <fstream>
#include <list>
#include "people.h"

using namespace std;
const int vector_resizer = 50;
string char2str(const char* str) { return boost::units::detail::demangle(str); }


//Fan class member functions
void Fans::showFanas() {
    int counter = 1;
    vector<Fans>::iterator i;
    for (i = fans.begin(); i != fans.end(); ++i) {
        cout << "\nFan " << counter << endl;
        cout << "Name: " << i -> name << endl;
        cout << "Sex: " << i -> sex << endl;
        cout << "Age: " << i -> age << endl;
        counter++;
    }
}


//Players class member functions
float Players::ageCalc() {
    int totalAge = 0;
    vector<Players>::iterator i;
    for (i = players.begin(); i != players.end(); ++i) {
        totalAge += i->age;
    }
    return totalAge;
}

void Players::showPlayas() {
    int counter = 1;
    vector<Players>::iterator i;
    for (i = players.begin(); i != players.end(); ++i) {
        cout << "\nPlayer " << counter << endl;
        cout << "Name: " << i -> name << endl;
        cout << "Sex: " << i -> sex << endl;
        cout << "Age: " << i -> age << endl;
        cout << "Player id: " << i -> playerId << endl;
        counter++;
    }
}

//Member functions of Managing DIrectos
ManagingDirectors::ManagingDirectors(int number) {
    directorId = number;
};
//makefile

footballmaker: main.o groups.o people.o  
    gcc -o main main.o groups.o people.o 
    rm groups.o people.o  
gcc -o main main.o groups.o people.o 
groups.o:(.bss+0x0): multiple definition of `players'
main.o:(.bss+0x0): first defined here
groups.o:(.bss+0x20): multiple definition of `directors[abi:cxx11]'
main.o:(.bss+0x20): first defined here
groups.o:(.bss+0x40): multiple definition of `fans'
main.o:(.bss+0x40): first defined here
people.o:(.bss+0x0): multiple definition of `players'
main.o:(.bss+0x0): first defined here
people.o:(.bss+0x20): multiple definition of `directors[abi:cxx11]'
main.o:(.bss+0x20): first defined here
people.o:(.bss+0x40): multiple definition of `fans'
main.o:(.bss+0x40): first defined here

...

collect2: error: ld returned 1 exit status
makefile:3: recipe for target 'footballmaker' failed
make: *** [footballmaker] Error 1
// people.h
extern std::vector<Players> players;
extern std::list<ManagingDirectors> directors;
extern std::vector<Fans> fans;

// people.cpp
std::vector<Players> players;
std::list<ManagingDirectors> directors;
std::vector<Fans> fans;