2016-03-29 3 views
0

Hallo Ich habe ein Problem beim Lesen des Polynoms, das ich erstellt habe. Ich muss einige Operationen implementieren, aber ich habe ein Problem mit der Lesephase, ich habe keine Lösung gefunden. Die Fehler sind solche, mit denenLesepolynom

||=== Build: Debug in Big HW (compiler: GNU GCC Compiler) ===| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp||In instantiation of 'void Polynomial<T>::readP() [with T = int]':| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp|58|required from here| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp|31|error: no matching function for call to 'Queue<int>::enqueue(term&)'| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp|31|note: candidate is:| 
C:\Facultate Personal\DSA\Big HW\queue.h|17|note: void Queue<T>::enqueue(T) [with T = int]| 
C:\Facultate Personal\DSA\Big HW\queue.h|17|note: no known conversion for argument 1 from 'term' to 'int'| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp||In instantiation of 'void Polynomial<T>::invert() [with T = int]':| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp|59|required from here| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp|50|error: no match for 'operator[]' (operand types are 'Queue<int>' and 'int')| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp|50|error: no match for 'operator[]' (operand types are 'Queue<int>' and 'int')| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp||In instantiation of 'void Polynomial<T>::computeX(T) [with T = int]':| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp|60|required from here| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp|42|error: no match for 'operator[]' (operand types are 'Queue<int>' and 'int')| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp|42|error: no match for 'operator[]' (operand types are 'Queue<int>' and 'int')| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp|42|error: no match for 'operator[]' (operand types are 'Stack<int>' and 'int')| 
C:\Facultate Personal\DSA\Big HW\big_hw.cpp|42|error: no match for 'operator[]' (operand types are 'Stack<int>' and 'int')| 
||=== Build failed: 7 error(s), 6 warning(s) (0 minute(s), 0 second(s)) ===| 

Die Lesefunktion und Haupt:

#include <iostream> 
#include <math.h> 
#include "queue.h" 
#include "stack.h" 
using namespace std; 

struct term{ 
int coef; 
int expo;}; 

template<typename T> class Polynomial{ 
private: 
T coef; 
T expo; 
public: 
Queue<T> polin; 
int n; 
struct term *p; 


Polynomial(){} 
~Polynomial(){} 

void readP() 
{ 
    cout<<"Please insert the maximum grade of the polynomial: "; cin>>n; 
    for(int i=0;i<=n;i++) 
    { 
     cout<<"Please insert the coefficient for x^"<<(n-i)<<": "; cin>>p[i].coef; 
     p[i].expo=n-i; 
     polin.enqueue(p[i]); 

    } 
    cout<<endl; 
} 
}; 
int main() 
{ 
Polynomial<int> polin; 
polin.readP(); 
polin.invert(); 
polin.computeX(2); 
return 0; 
} 

Die enqueue Funktion ist dies:

void enqueue(T x) { 
     if (size >= NMAX) { 
      cout << "The queue is FULL" << endl; 
      return; 
     } 

     queueArray[tail] = x; 
     tail=(tail+1)%NMAX; 
     size--; 
    } 

Was habe ich falsch gemacht?

+0

Sie die Fehler als Text in Frage stellen. – NathanOliver

+0

Fertig, sorry, mein erster Beitrag. –

+0

Sie greifen auf einen Zeiger mit einem Indexierungsoperator zu: '' 'struct term * p;' '' '' 'p [i]' '', was zu undefiniertem Verhalten führt (in diesem Fall zumindest). –

Antwort

0

Die Fehler sind ziemlich selbsterklärend:

error: no matching function for call to 'Queue<int>::enqueue(term&) 

Sie einen Begriff Objekt in die enqueue Funktion, die eine T (= int) erwartet vorbei. vielleicht wollten Sie p[i].coef oder p[i].expo?

error: no match for 'operator[]' (operand types are 'Queue<int>' and 'int') 

Es sieht nicht wie Queue hat einen Indizierungsoperator.

Edit:

Ich bemerkte, etwas anderes zu. Wenn Sie die term s lesen, setzen Sie sie in die Variable struct term *p, aber greifen Sie darauf mit p[i] zu. Das heißt, wenn du> 0 bist, bist du in undefiniertem Verhaltensgebiet.

Mein Vorschlag wäre der p Variable loszuwerden und Ihr, wie dies für Schleife schreiben:

for (int i = 0; i <= n; i++) 
{ 
    term t; 

    std::cout << "Please insert the coefficient for x^" << (n - i) << ": "; 
    std::cin >> t.coef; 
    t.expo = n - i; 

    polin.enqueue(t); 
} 
+0

Ich muss jeden Ausdruck des Polynoms in einer kreisförmigen Warteschlange halten, und ein Ausdruck des Polynoms hat einen Exponenten und einen Koeffizienten. Ich muss sie beide irgendwie behalten. –

+0

Warum also eine '' 'Queue ' '', warum nicht einfach eine '' 'Queue ' ''? –

+0

Ich habe es mit der Warteschlange geändert. Jetzt kann ich es kompilieren, aber nachdem ich die Note und den Koeffizienten gelesen habe, hört es auf zu arbeiten. Und auch die anderen Funktionen haben einige Probleme mit dieser Queue , aber es wird managable sein –