我是Qt的c ++程序新手。 我做两个班Personnage和Arme。 我有5个文件Personnage.h,personnage.cpp,Arme.h和Arme.cpp
Arme.h
#ifndef ARME_H
#define ARME_H
#include<iostream>
class Arme
{
public:
Arme();
Arme(int m_idArme,std::string m_nomArme);
void armeAffich() const;
public:
int idArme;
std::string nomArme;
};
#endif // ARME_H
Arme.cpp
#include "Arme.h"
#include<string>
#include<iostream>
using namespace std;
Arme::Arme():idArme(0),nomArme(0)
{
}
Arme::Arme(int m_idArme,string m_nomArme)
{
idArme=m_idArme;
nomArme=m_nomArme;
}
void Arme::armeAffich() const
{
cout<<"identifiant d'arme"<<idArme<<endl;
cout<<"nom d'arme"<<nomArme<<endl;
}
Personnage.h
#ifndef PERSONNAGE_H
#define PERSONNAGE_H
#include "Arme.h"
class Personnage
{
Personnage(std::string nom,std::string prenom);
Personnage();
~Personnage();
void affichPers() const;
public:
std::string nomPers;
std::string prenomPers;
Arme *arme;
};
#endif // PERSONNAGE_H
Personnage.cpp
#include "Arme.h"
#include<string>
#include<iostream>
using namespace std;
Arme::Arme():idArme(0),nomArme(0)
{
}
Arme::Arme(int m_idArme,string m_nomArme)
{
idArme=m_idArme;
nomArme=m_nomArme;
}
void Arme::armeAffich() const
{
cout<<"identifiant d'arme"<<idArme<<endl;
cout<<"nom d'arme"<<nomArme<<endl;
}
并在main.cpp文件中输出两条仅用于编译的指令。
#include"Personnage.h"
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello word!";
return 1;
}
编译时我得到这些错误:
duplicate symbol __ZN4ArmeC1Ev in:
Arme.o
Personnage.o
duplicate symbol __ZN4ArmeC2Ev in:
Arme.o
Personnage.o
duplicate symbol __ZN4ArmeC1EiSs in:
Arme.o
Personnage.o
duplicate symbol __ZN4ArmeC2EiSs in:
Arme.o
Personnage.o
duplicate symbol __ZNK4Arme10armeAffichEv in:
Arme.o
Personnage.o
ld: 5 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [ExempleClasses] Error 1
18:55:46: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project ExempleClasses (kit: Desktop Qt 5.2.0 clang 64bit)
When executing step 'Make'
请帮忙吗?
答案 0 :(得分:5)
在你的帖子中:
Personnage.cpp
#include "Arme.h"
#include<string>
#include<iostream>
using namespace std;
Arme::Arme():idArme(0),nomArme(0)
{
}
当然,你真的不想在Personage.cpp中重复Arme::Arme
吗?