[英]Multiple definition of function in C++
I am having a very unusual problem: 我有一个非常不寻常的问题:
I keep getting multiple definition of functions in my class. 我在课堂上不断得到函数的多个定义。
This is my main .cpp 这是我的主要.cpp
#include <iostream>
#include "Calculation.cpp"
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
this is my class .h 这是我的课.h
#ifndef CALCULATION_H_INCLUDED
#define CALCULATION_H_INCLUDED
class Calculation
{
public:
Calculation();
private:
};
#endif // CALCULATION_H_INCLUDED
this is my implementation file .cpp 这是我的实现文件.cpp
#include "Calculation.h"
Calculation::Calculation()
{
}
Please help me; 请帮我; I have tried to create a new project but that didn't help.
我试图创建一个新项目,但这没有帮助。
All help is appreciated. 感谢所有帮助。
make your main.cpp like : 使您的main.cpp像:
#include <iostream>
#include "Calculation.h" // not Calculation.cpp
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
You have to include your Calculation.h in th main.cpp and you have to compile it as follows, 您必须将自己的Calculation.h包含在main.cpp中,并且必须按如下所示进行编译,
g++ main.cpp Calculate.cpp -o main -I<path for your .h file>
main.cpp main.cpp中
#include<iostream>
#include "Calculation.h"
//using namespace std; // Avoid this, always to use std::cout .. etc on place
int main()
{
Calculation c; //Creating the object of Calculation class
std::cout<<"Hello World!"<<std::endl;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.