Issue
I am using C++, I'm quite new to programming and wanted to try out some things.
I have been smooth sailing until I reached multifile programming..
I tried putting all the contents of the 3 files into a single cpp file and it works:
#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;
}
}
but these would just not work:
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;
}
}
I keep getting an error in the makefile.win pointing this error out:
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
the error says
"head.h:9: multiple definition of `individual'; head.h:9: first defined here" >I removed the directories but I think you guys get the point
Solution
The error is that you define the variable individual
in the header file:
struct person
{
std::string name;
} individual[4];
That means the variable will defined in each translation unit where the header file was included, and C++ only allows variables (and functions) to be defined once.
I suggest you split these into the separate structure definition, and a separate variable declaration:
struct person
{
std::string name;
};
extern struct individual[4];
Then in a single source file you define the variable:
struct individual[4];
Answered By - Some programmer dude Answer Checked By - Marilyn (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.