Reputation: 21
I have three files like the following:
Movie.h
struct Movie{
char title[30]; // the hour of the current time
char director[30]; // the minute of the current time
int length;
} ;
void printMovieInfo(Movie *s);
Movie.cpp
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include "Movie.h"
using namespace std;
void printMovieInfo(Movie *s){
cout << "Hi";
}
and a file with the main
#include "Movie.cpp"
using namespace std;
int main()
{
struct Movie *m;
printMovieInfo(m);
}
When I run the program, I get the following error:
collect2 ld returned 1 exit status
and warnings:
/tmp/ccIe4dlt.o In function `printMovieInfo()':
Movie.cpp (.text+0x0): multiple definition of `printMovieInfo()'
/tmp/cc91xrNB.o HelloWorld.cpp:(.text+0x0): first defined here
I just want to call a function to print "Hi", but I'm not sure why I get this error
Upvotes: 2
Views: 6311
Reputation: 129314
Do not use #include "movie.cpp"
you want #include "movie.h"
!
It is extremely rare the correct thing to include a ".cpp" file - they are compiled as separate units, and then linked together by the linker (here called collect2
).
Upvotes: 2