Surcharge d'opérateur : erreur de linkage
Bonjour,
Je suis en train d'écrire une classe de gestion de grands entiers, et j'aimerais redéfinir les opérateurs de base.
Seulement voilà, j'ai une erreur de linkage que je n'arrive pas à résoudre (ou pas comme je veux en tout cas).
voici le code de ma classe :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#ifndef BIGINT_H
#define BIGINT_H
#include <iostream>
#include <string>
using namespace std;
namespace RSAPTut {
class BigInteger {
private:
int size; // le nombre de chiffres
int sign; // -1 si <0, 0 sinon
unsigned short * number; // chiffres du moins significatif au plus significatif
public:
// Constructeurs
BigInteger();
BigInteger(BigInteger * big);
// Getters
inline int getSign() { return this->sign; }
inline int getSize() { return this->size; }
unsigned short * getNumber() { return this->number; }
// Setters
inline void setSign(int sign) { this->sign=sign; }
inline void setSize(int size) { this->size=size; }
inline void setNumber(unsigned short * number, int size) {
this->size = size;
this->number = new unsigned short[size]; // on alloue la mémoire pour number
for (int i=0; i<size; i++)
this->number[i] = number[i]; // on recopie la valeur dans number
}
// Utilitaires
void print();
void stringToBigInteger(char * s);
void setDigit(unsigned short d);
};
// Méthodes primitives
[...]
// Opérations de base
BigInteger add(BigInteger M, BigInteger N); // Addition
[...]
// Opérations modulaires
[...]
// Opérateurs
BigInteger operator+(const BigInteger &m, const BigInteger &n) {
BigInteger r = new BigInteger( add(m,n) );
return r;
}
};
#endif |
j'ai défini mon opérateur + en dehors de ma classe mais dans un namespace.
En plus de ce code j'ai :
- l'implémentation dans un fichier séparé, qui inclus ce .h et où toutes les méthodes sont implémentées dans le namespace
- un fichier main pour tester
Si je compile tel quel, j'obtiens le message d'erreur :
Code:
1 2 3 4 5 6 7 8 9 10 11
|
Compiling...
main.cpp
BigInteger.cpp
Linking...
main.obj : error LNK2005: "class RSAPTut::BigInteger __cdecl RSAPTut::operator+(class RSAPTut::BigInteger const &,class RSAPTut::BigInteger const &)" (??HRSAPTut@@YA?AVBigInteger@0@ABV10@0@Z) already defined in BigInteger.obj
main.obj : error LNK2005: "class RSAPTut::BigInteger __cdecl RSAPTut::operator+(class RSAPTut::BigInteger const &,class RSAPTut::BigInteger const &)" (??HRSAPTut@@YA?AVBigInteger@0@ABV10@0@Z) already defined in BigInteger.obj
Debug/RSA.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
RSA.exe - 3 error(s), 0 warning(s) |
J'arrive à faire fonctionner l'opérateur en le définissant dans le fichier ou se trouve le main :
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
// Opérateurs
RSAPTut::BigInteger operator+(const RSAPTut::BigInteger &m, const RSAPTut::BigInteger &n) {
RSAPTut::BigInteger r = new RSAPTut::BigInteger( RSAPTut::add(m,n) );
return r;
}
int main()
{
[...]
} |
Voilà donc je ne sais pas comment résoudre ce problème (je ne souhaite pas mettre tous les opérateurs dans le main.cpp), donc si quelqu'un sait comment faire ?
merci.