-3

I found a begginers c++ challenge I wanted to try. However, the following code is saying that it contains erros when I compile it. If I try to take 1 line at a time, it exits in the first class definition at the end... I have no idea what's wrong :)

#include <iostream>
using namespace std;

class Polynomial {
    int a, b, c, functionValue;

public:
    Polynomial (int, int, int);
    static void functionValue(Polynomial);
};

Polynomial::Polynomial (int x, int y, int z) {
    a = x;
    b = y;
    c = z;
}

void Polynomial::functionValue(Polynomial x) {
    for (int i = 0; i < 5; i++) {
        x.functionValue = x.a * pow(i, 2) + x.b * i + x.c;
        cout << "The value of the function for x = "
             << i << " is " << x.functionValue;
    }
}

int main () {
    Polynomial poly (2, 3, 5);
    Polynomial::functionValue(poly);

    system("pause");
    return 0;
}

I don't know why the formatting is so poor. Here is a pastebin link.

(Edit: My fault, I edited over a previous edit and accidentally removed these - BoBTFish)

Compiler errors:

'Polynomial::functionValue' : redefinition; previous definition was 'data member' 'see declaration of 'Polynomial::functionValue'
'Polynomial::functionValue' : not a function' 'illegal reference to non-static member 'Polynomial::functionValue'

Thanks in advance.

4

4 回答 4

7

你有functionValue变量,也有函数。

于 2013-05-29T08:31:57.380 回答
3

functionValue以两种不同的方式使用两次,一次作为整数,另一次作为静态函数。

于 2013-05-29T08:32:05.320 回答
1

functionValue can be any of the member or the function name. You should rename any of those.

Also what is the need of making the function static.

于 2013-05-29T08:34:37.983 回答
0

原因是还有一个名为 的变量functionValue

我建议你安装一个替代的 C/C++ 编译器 - Clang. Clang 提供了有关可能出现错误的原因和位置的更多详细信息。

例如,我用 编译了您的代码Clang,它为我提供了以下信息。

test.cc:10:17: error: redefinition of 'functionValue' as different kind of symbol
    static void functionValue(Polynomial);
                ^
test.cc:6:18: note: previous definition is here
    int a, b, c, functionValue;
                 ^
test.cc:19:18: error: redefinition of 'functionValue' as different kind of symbol
void Polynomial::functionValue(Polynomial x) {
                 ^
test.cc:6:18: note: previous definition is here
    int a, b, c, functionValue;

这些给出了可能的错误的准确位置。

Clang 对初学者真的很有帮助。

于 2013-05-29T09:54:50.610 回答