The static member has already been defined?

I try to write a polynomial in a global array at the beginning, But, the error says that the static member has already been defined.
What should I do to solve the problem ?
Thank you.

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
#define MaxTerms 10
#include <iostream>
using namespace std;

class term
{
	friend class Polynomial;
private:
	float coef;
	int exp;
};

class Polynomial
{
public:
	void input();
	void output();
private:
	static term termArray[MaxTerms];
	static int free;
	int Start, Finish;

};

term Polynomial::termArray[MaxTerms];
int Polynomial::free = 0;

void Polynomial::input()
{
	Start = free;
	cout << "Please type the coefficient of polynomial:" << endl;
	for (int i = free; i < MaxTerms; i++)
	{
		cin >> termArray[i].coef;
		if ( termArray[i].coef == 0 )
		{ 
			free = i;
			Finish = i-1;
			break;
		}
	}

	cout << "Please type the degree of polynomial:" << endl;
	for (int i = Start; i <= Finish ; i++)
	{
		cin >> termArray[i].exp;
	}
}


void Polynomial::output()
{
	for (int i = Start; i <= Finish ; i++)
	{
		cout << termArray[i].coef << "x^" << termArray[i].exp;
		( i <= Finish ) ? cout << "+" : cout << endl;
	} 
	cout << "free: " << free << endl;
}


int main()
{
	Polynomial a;
        a.input();
        a.output();
	system("PAUSE");
	return 0;
}
error says that the static member has already been defined.

yes, line 25 essentially repeats line 19 and is superfluous. Also instead of #define MaxTerms (line 1) you could use constexpr:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>

constexpr auto MaxTerms = 20;

struct term{};

struct PolyNomial
{
    static term termArray[MaxTerms];
};

int main()
{

}

Thank you, But when i remove the line 25, it still can not run ?
, it still can not run
could you try and be a bit more specific?
yes, line 25 essentially repeats line 19 and is superfluous.

Line 19 is a declaration. Line 25 is a definition (and required, not superfluous.)

The original code works just fine for me if I replace your special character on lines 31 and 43 (:) with the normal version (:)
Line 19 is a declaration

since OP, on line 1, did: #define MaxTerms 10 line 19 static term termArray[MaxTerms]; is actually a definition and hence
the error says that the static member has already been defined.
The following program is however valid that does both in-class and out-of-class definitions but only with the same array size in both cases:
http://coliru.stacked-crooked.com/a/22b31f0443052930
so one possibililty could be that OP was using different array subscripts in-class, out-of-class when s/he first encountered the issue, setting aside the colon font issue that is not applicable in the coliru example
Last edited on
Hi, commemorate.
The structure of your program looks a bit complicated, doesn’t it?

In one class (Polynomial) you declare an array of elements of another class (term), which is friend of the first class. The only advantage I can see, at the present stage of development, of this ‘friendship’ is that you don’t need to use some getter/setter methods.

An other point I should avoid is the usage of C-style arrays, since std::vectors are much, much more reliable.

Then, but that’s absolutely personal, I’d try to decide a style for giving names to the different elements of my code. For example, capitalized camel-case for classes, non capitalized camel-case for functions and all lowercase (with underscores or not) for variables, or another one that can suit your needs.

If you decided to give a chance to vectors, you could see how they can make your code easier to read. Here are some hints:

Term.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef TERM_H
#define TERM_H


class Term
{
public:
    Term() = default;

    double getCoef() const {return coef;}
    int getExp() const {return exp;}

    void setCoef(const double coef_arg) {coef = coef_arg;}
    void setExp(const int exp_arg) {exp = exp_arg;}

private:
    double coef {};
    int exp {};
};

#endif // TERM_H 


Terms.cpp:
 
#include "Term.h" 


Polynomial.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H

#include <iostream>
#include <vector>
#include "Term.h"

class Polynomial
{
public:
    Polynomial(const int);
    void input();
    void output();

private:
    const int max_terms;
    static int free;
    int start {}, finish {};
    static std::vector<Term> terms;
};

#endif // POLYNOMIAL_H 


Polynomial.cpp:
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
#include "Polynomial.h"

int Polynomial::free = 0;
std::vector<Term> Polynomial::terms;

Polynomial::Polynomial(const int maxterms) : max_terms {maxterms}
{}

void Polynomial::input()
{
    start = free;
    for (int i = free; i < max_terms; i++)
    {
        std::cout << "Please, type the coefficient of polynomial "
                     "or 0 to stop: ";
        double tmpdouble;
        std::cin >> tmpdouble;
        if (0 == tmpdouble)
        {
            free = i;
            finish = i-1;
            break;
        }
        Term tmpterm;
        tmpterm.setCoef(tmpdouble);
        terms.push_back(tmpterm);
    }

    for (int i = start; i <= finish; i++)
    {
        std::cout << "Please type the degree of polynomial: ";
        int tmpint;
        std::cin >> tmpint;
        terms.at(i).setExp(tmpint);
    }
}

void Polynomial::output()
{
    for (int i = start; i <= finish ; i++)
    {
        std::cout << terms[i].getCoef() << "x^" << terms[i].getExp();
        ( i <= finish ) ? std::cout << "+" : std::cout << std::endl;
    }
    std::cout << "free: " << free << std::endl;
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <limits>
#include "Polynomial.h"

constexpr int MAXTERMS = 10;

int main()
{
    Polynomial polyn(MAXTERMS);
    polyn.input();
    polyn.output();

    std::cout << "\n\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    return 0;
}



Please, type the coefficient of polynomial or 0 to stop: 2.2
Please, type the coefficient of polynomial or 0 to stop: 4.4
Please, type the coefficient of polynomial or 0 to stop: 6.6
Please, type the coefficient of polynomial or 0 to stop: 0
Please type the degree of polynomial: 3
Please type the degree of polynomial: 5
Please type the degree of polynomial: 7
2.2x^3+4.4x^5+6.6x^7+free: 3

Thanks for Enoizat, gunnerfunner and cire.
I am appreciate for your kind help.
I tried again and found the code which I posted on the page works fine.
The origin code is separated into different part which looked like the code below.
Then, the code can't work and I don't know the reason.


Polynomial.cpp
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
#include <iostream>
#include "Poly.h"
using namespace std;


void Polynomial::input()
{
	Start = free;
	cout << "Please type the coefficient of polynomial:" << endl;
	for (int i = free; i < MaxTerms; i++)
	{
		cin >> termArray[i].coef;
		if ( termArray[i].coef == 0 )
		{ 
			free = i;
			Finish = i-1;
			break;
		}
	}

	cout << "Please type the degree of polynomial:" << endl;
	for (int i = Start; i <= Finish ; i++)
	{
		cin >> termArray[i].exp;
	}
}


void Polynomial::output()
{
	for (int i = Start; i <= Finish ; i++)
	{
		cout << termArray[i].coef << "x^" << termArray[i].exp;
		( i <= Finish ) ? cout << "+" : cout << endl;
	} 
	cout << "free: " << free << endl;
	
}



Poly.h
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
#define MaxTerms 10

class term
{
	friend class Polynomial;
private:
	float coef;
	int exp;
};

class Polynomial
{
public:
	void input();
	void output();

private:
	static term termArray[MaxTerms];
	static int free;
	int Start, Finish;

};

term Polynomial::termArray[MaxTerms];
int Polynomial::free = 0;


GlobalArray.cpp
1
2
3
4
5
6
7
8
int main()
{
	Polynomial a;
        a.input();
        a.output();
	system("PAUSE");
	return 0;
}

Topic archived. No new replies allowed.