【问题标题】:Reading polynomial读取多项式
【发布时间】:2016-03-29 19:51:19
【问题描述】:

您好,我在阅读我创建的多项式时遇到问题。我必须实现一些操作,但是我在阅读阶段遇到了问题,我没有找到任何解决方案。 错误是:

||=== 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)) ===|

读取函数和main:

#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;
}

入队函数是这样的:

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

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

我做错了什么?

【问题讨论】:

  • 请将错误以文本形式发布在您的问题中。
  • 完成,抱歉,我的第一篇文章。
  • 您正在使用索引运算符访问指针:struct term *p; p[i],这会产生未定义的行为(至少在这种情况下)。

标签: c++ struct compilation polynomial-math


【解决方案1】:

这些错误是不言自明的:

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

您将一个 term 对象传递给 enqueue 函数,该函数需要 T (=int)。也许您打算通过p[i].coefp[i].expo

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

Queue 似乎没有索引运算符。

编辑:

我也注意到了一些别的东西。当您阅读terms 时,您将它们放入struct term *p 变量中,但使用p[i] 访问它。这意味着如果 i > 0 你处于未定义的行为领域。

我的建议是去掉 p 变量并像这样编写你的 for 循环:

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);
}

【讨论】:

  • 我必须将多项式的每一项保持在一个循环队列中,多项式的一项有一个指数和一个系数。我必须以某种方式保留它们。
  • 那为什么有Queue&lt;T&gt;,为什么不只是Queue&lt;term&gt;
  • 我用 Queue 改变了它。现在我可以编译它,但是在我阅读了成绩和系数之后,它就停止了工作。并且其他函数也有一些问题与队列,但它是可管理的
猜你喜欢
  • 1970-01-01
  • 2012-09-15
  • 1970-01-01
  • 1970-01-01
  • 2014-10-29
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
相关资源
最近更新 更多