我有以下三个文件:
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";
}和带有主的文件
#include "Movie.cpp"
using namespace std;
int main()
{
struct Movie *m;
printMovieInfo(m);
}当我运行这个程序时,我会得到以下错误:
collect2 ld returned 1 exit status和警告:
/tmp/ccIe4dlt.o In function `printMovieInfo()':
Movie.cpp (.text+0x0): multiple definition of `printMovieInfo()'
/tmp/cc91xrNB.o HelloWorld.cpp:(.text+0x0): first defined here我只想调用一个函数来打印"Hi",但我不知道为什么会出现这个错误
发布于 2013-02-01 18:19:00
不要使用#include "movie.cpp",你想要#include "movie.h"!
包含一个".cpp“文件是非常罕见的--它们被编译成单独的单元,然后由链接器(这里称为collect2)链接到一起。
https://stackoverflow.com/questions/14652549
复制相似问题