I am a developer of barcode reader of datamatrix.I am a greenhand of QT applications and i met some problems.I have to come here for help.
I was defining a simple class called "date" in C++. The IDE I'm using is QT creator. When I'm compiling, the compiler said that every function in this class has "multiple definition." Below is the Cpp and header file. Fisrt this is the header date.h
#ifndef DATE_H
#define DATE_H
#include <string>
/* A class representing an earth day.*/
class Date{
public:
/*construct a new day, initially assigned at Jan.1st.2000*/
Date();
/* take three numbers to create the date.*/
Date(int cmonth,int cday, int cyear);
/*clean up memory allocated to this data type*/
~Date();
private:
int year;
int month;
int day;
};
#endif // DATE_H
#ifndef DATE_H
#define DATE_H
#include <string>
/* A class representing an earth day.*/
class Date{
public:
/*construct a new day, initially assigned at Jan.1st.2000*/
Date();
/* take three numbers to create the date.*/
Date(int cmonth,int cday, int cyear);
/*clean up memory allocated to this data type*/
~Date();
private:
int year;
int month;
int day;
};
#endif // DATE_H
To copy to clipboard, switch view to plain text mode
And this is the Cpp file.
#include "date.h"
Date::Date(){
year=2000;
month=1;
day=1;
}
Date::Date(int cmonth,int cday, int cyear){
month=cmonth;
day=cday;
year=cyear;
}
/*clean up memory allocated to this data type*/
Date::~Date(){
//automatic
}
#include "date.h"
Date::Date(){
year=2000;
month=1;
day=1;
}
Date::Date(int cmonth,int cday, int cyear){
month=cmonth;
day=cday;
year=cyear;
}
/*clean up memory allocated to this data type*/
Date::~Date(){
//automatic
}
To copy to clipboard, switch view to plain text mode
Sample Error Message: D:samplepath\date.cpp:3: error: multiple definition of `Date:
ate()'
One possible main cpp that could induce the error (basically anything):
#include <iostream>
using namespace std;
int main() {
int sum=0;
cout<<sum<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main() {
int sum=0;
cout<<sum<<endl;
return 0;
}
To copy to clipboard, switch view to plain text mode
I've made sure that I avoided a few basic errors associated with this message, namely:
I didn't put any implementation in the header.
I used the #ifndef for protection
I did a global search on keyword "date" and did not find any conflict.
I always use a new build to compile
Every function in this class have a "multiple definition of" error, but I can't tell what went wrong. Currently I'm doing an all-limb quadruple face palm. Any help would be greatly appreciated.
Update: As it turns out, this is indeed a linker error. In the .pro file of Qtcreator, I included the source file twice with the following code.
SOURCES += $$files($$PWD/*.cpp)\
date.cpp
SOURCES += $$files($$PWD/*.cpp)\
date.cpp
To copy to clipboard, switch view to plain text mode
The second line of code "data.cpp" was actually automatically added here by QTcreator, if you create the new class through its menu. Many thanks to all the guys here for your generous help.
Bookmarks