Structure et classe : problème
Bonjour,
J'ai réalisé une classe pour les nombres complexes. Elle fonctionne.
Maintenant, je dois réaliser un stack (liste chainée) avec des nombres complexes.
Voilà mon code :
main.cpp // fonction principale => pas importante
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
|
#include <stdlib.h>
#include <iostream>
#include <strstream>
#include "stack.h"
using namespace std;
int main(int argc, char * argv[]){
init(); //stack init
// pour chaque arguement
int currentArg = 1;
while (currentArg < argc)
{
// fait les opérations
if (strcmp(argv[currentArg],"+") == 0)
{
Listcomp op1 = pop();
Listcomp op2 = pop();
Listcomp result = op2 + op1;
push(result);
}
else if (strcmp(argv[currentArg],"-") == 0)
{
Listcomp op1 = pop();
Listcomp op2 = pop();
Listcomp result = op2 - op1;
push(result);
}
else if (strcmp(argv[currentArg],"x") == 0)
{
Listcomp op1 = pop();
Listcomp op2 = pop();
Listcomp result = op2 * op1;
push(result);
}
else if (strcmp(argv[currentArg],"/") == 0)
{
Listcomp op1 = pop();
Listcomp op2 = pop();
Listcomp result = op2 / op1;
push(result);
}
// parse le tout
else
{
int i = 0;int j = 0;
char re[20];
char im[20];
char parseStr[100];
strcpy(parseStr, argv[currentArg]);
while ((parseStr[i] != '+')&&(parseStr[i] != '-'))
{
re[j] = parseStr[i]; i++;j++;
}
re[j] = 0;
j=0;
im[j] = parseStr[i];
i = i+2; j++;
while (parseStr[i] != 0)
{
im[j] = parseStr[i]; i++;j++;
}
im[j] = 0;
float fre = 0.0;
float fim = 0.0;
istrstream str1(re, 20);
str1 >> fre;
istrstream str2(im, 20);
str2 >> fim;
push(Listcomp(fre,fim));
}
currentArg++;
}
// affiche le résulat
if (size() != 1) cout << "Invalid expression! ";
Listcomp c = pop();
c.print();
clear(); // vide le stack
return 0;
} |
complex.h // header de la classe pour les nombres complexes
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
|
// header for complex class
#ifndef complex
#define complex
class Complex
{
private:
float a;
float b;
public:
Complex(float a, float b);
Complex add(Complex &myco) const;
Complex sub(Complex &myco) const;
Complex mult(Complex &myco) const;
Complex div(Complex &myco) const;
Complex operator+(Complex &myco) const;
Complex operator-(Complex &myco) const;
Complex operator*(Complex &myco) const;
Complex operator/(Complex &myco) const;
float abs() const;
void print() const;
};
#endif |
complex.cpp // classe pour les nombres complexes
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 68 69
|
#include <iostream>
#include <stdlib.h>
#include <math.h>
#include "complex.h"
using namespace std;
// constructor:
Complex::Complex(float re, float img)
{
a = re;
b = img;
}
Complex Complex::add(Complex &myco) const
{
return Complex(a+myco.a,b+myco.b);
}
Complex Complex::sub(Complex &myco) const
{
return Complex(a-myco.a,b-myco.b);
}
Complex Complex::mult(Complex &myco) const
{
return Complex((a*myco.a)-(b*myco.b),(a*myco.b)+(b*myco.a));
}
Complex Complex::div(Complex &myco) const
{
return Complex((a*(myco.a)/(myco.abs()*myco.abs()))-(b*(-myco.b)/(myco.abs()*myco.abs())),(a*(-myco.b)/(myco.abs()*myco.abs()))+(b*(myco.a)/(myco.abs()*myco.abs())));
}
float Complex::abs() const
{
return sqrt((a*a)+(b*b));
}
void Complex::print() const
{
if(b>=0)
{
cout<<a<<"+i"<<b;
}
else
{
cout<<a<<"-i"<<-b;
}
}
Complex Complex::operator+(Complex &myco) const
{
return add(myco);
}
Complex Complex::operator-(Complex &myco) const
{
return sub(myco);
}
Complex Complex::operator*(Complex &myco) const
{
return mult(myco);
}
Complex Complex::operator/(Complex &myco) const
{
return div(myco);
} |
stack.h
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
#ifndef stack
#define stack
#include "complex.h"
namespace ListStack{
void init() ;
Complex pop();
void push(Complex element);
int size() ;
void clear();
}
#endif |
stack.cpp // fichier ou se trouve le problème
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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <stdlib.h>
#include <iostream>
#include <string>
#include "complex.h"
using namespace std;
// the structure of an element
struct ele{
Complex value;
ele* next;
};
int nb=0; // number of elements
ele *top=NULL; // pointer to the first element
// init the stack
void init() {
top=NULL;
nb=0;
}
// read the top element
Complex pop(){
// if the stack is empty
if (top==NULL){
cout<<"the stack is empty"<<endl;
return Complex(0,0);
}
// pointer to the top element
ele* buffer = top;
Complex myvalue=top->value; // save the value
top=top->next; // the top is the next element
delete buffer; // we have the adress and the content => we can kill the element
nb--;
return myvalue;
}
// write an element
void push(Complex element){
// allocate a new element
ele* my = new ele;
my->value = element; // save the value
my->next = top; // connect the element the the second
top = my; // it's the first element
nb++;
}
// return the size of the stack
int size() {
return nb;
}
// clear the stack
void clear() {
// pop() until free (not the faster solution but it's simple)
while(nb!=0){
pop();
}
} |
Quand je compile j'ai le message suivant :
Code:
1 2 3 4 5 6 7 8 9
|
g++ -c -Wall stack.cpp
stack.cpp: In constructor ele::ele():
stack.cpp:9: error: no matching function for call to Complex::Complex()
complex.h:10: note: candidates are: Complex::Complex(float, float)
complex.h:5: note: Complex::Complex(const Complex&)
stack.cpp: In function void push(Complex):
stack.cpp:41: note: synthesized method ele::ele() first required here
make: *** [stack.o] Error 1 |
Je ne trouve pas ma faute. Pourquoi créer un objet dans une structure pose problème ?
Quelqu'un peut il m'aider ?
merci