为什么我在 makefile.win 上收到错误消息? (开发 C++ 6.3)

Why am I getting an error on the makefile.win? (dev c++ 6.3)

我正在使用 C++, 我对编程很陌生,想尝试一些东西。

在我达到多文件编程之前,我一直一帆风顺..

我尝试将 3 个文件的所有内容放入一个 cpp 文件中并且它有效:

#include <iostream>

using namespace std;

const int numOf_people = 4;

struct person
{
    std::string name;
} individual[4]; // 4 identities

void input_movieName (person individual[], int numOf_people)
{
    for (int i = 0; i < numOf_people; i++)
    {
        cout << "Enter individual " << i+1 << " name : ";
        cin >> individual[i].name;
    }
}

int main()
{
    input_movieName (individual, numOf_people);
    
    for (int i = 0; i < numOf_people; i++)
    {
        cout << "Name of person " << i+1 << " : " << individual[i].name << endl;
    }
}

但这些都行不通:

head.h

#ifndef MAIN
#define MAIN

const int numOf_people = 4;

struct person
{
    std::string name;
} individual[4]; // 4 identities

void input_movieName (person*, int);

#endif

main.cpp

#include <iostream>
#include "head.h"

using namespace std;

int main()
{
    input_movieName (individual, numOf_people);
    
    for (int i = 0; i < numOf_people; i++)
    {
        cout << "Name of person " << i+1 << " : " << individual[i].name << endl;
    }
}

func.cpp

#include <iostream>
#include "head.h"

using namespace std;

void input_movieName (person individual[], int numOf_people)
{
    for (int i = 0; i < numOf_people; i++)
    {
        cout << "Enter individual " << i+1 << " name : ";
        cin >> individual[i].name;
    }
}

我在 makefile.win 中不断收到错误指出此错误:

$(BIN): $(OBJ)
    $(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)

错误说

"head.h:9: `individual' 的多重定义;head.h:9: 首先在这里定义" >我删除了目录,但我想你们明白了

错误是你在头文件中定义了变量individual:

struct person
{
    std::string name;
} individual[4];

这意味着变量将在每个包含头文件的translation unit中定义,而C++只允许定义一次变量(和函数)。

我建议您将它们拆分为单独的结构定义和一个单独的变量 声明:

struct person
{
    std::string name;
};

extern struct individual[4];

然后在单个源文件中定义变量:

struct individual[4];