static variables declared multiple times

Hello. Every time I try to compile this it tells me that all of the static members in the Numbers class have been declared multiple times. The part that confuses me is the errors are telling me that every single one of them was first declared in the main class. I am really mystified as to why it does that...

Does anyone know where I went wrong with my static members? Ty for any help!

main.cpp:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Numbers.h"

using namespace std;

int main()
{
    Numbers numbers(9999);
    numbers.print();
    return 0;
}


Numbers.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
26
27
28
29
30
#ifndef NUMBERS_H
#define NUMBERS_H

#include <iostream>
#include <string>

using namespace std;

class Numbers
{
    public:
        Numbers(int n);
        void print();
    protected:
    private:
        int number;

        static const string lessThan20[20];
        static const string tens[8];
        static const string hundred;
        static const string thousand;
};

const string Numbers::lessThan20[20] = {"zero","one","two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen",
                                            "fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
const string Numbers::tens[8] = {"twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
const string Numbers::hundred = "hundred";
const string Numbers::thousand = "thousand";

#endif // NUMBERS_H 


Numbers.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
#include "Numbers.h"
#include <sstream>

Numbers::Numbers(int n)
{
    if(n < 0)
        cout << "ERROR: NUMBERS MAY NOT BE INITIALIZED WITH A NON-NEGATIVE NUMBER.";
    else
        number = n;
}

void Numbers::print()
{
    string composite = "";
    int subn = 0;
    stringstream ss;
    if(number / 1000 > 0)
    {
        ss.str("");
        ss << (number / 1000) << " " << thousand << " ";
        composite += ss.str();
        subn = number % 1000;
    }
    if(subn/100 > 0)
    {
        ss.str("");
        ss << subn/100 << " " << hundred << " ";
        composite += ss.str();
        subn = subn % 100;
    }
    if(subn < 20)
    {
        composite += lessThan20[subn];
    }
    else
    {
        ss.str("");
        ss << tens[(subn/10)-2] << " " << subn % 10;
        composite += ss.str();
    }

    cout << composite;
}
Try moving the initializations for your static members (lines 24-28 of Numbers.h) to your .cpp file.
Thank you, but I still don't understand why the book I am learning from was able to both declare and initialize a static member (an int) within the header file in the example they gave for static members.

Is this example valid? It came from "starting out with c++ from control structures through objects"

Example: Tree.h:
1
2
3
4
5
6
7
8
9
10
11
12
class Tree
{
private:
    static int objectCount;
public:
    Tree()
    { objectCount++; }
    int getObjectCount() const
    { return objectCount; }
};

int Tree::objectCount = 0;
Last edited on
Is there a Tree.cpp also, or is it just Tree.h and the file containing the main() function?

The problem with your code is that since both main.cpp and Numbers.cpp include Numbers.h, both .cpp files will get the declarations/initializations of your static member variables. The linker will see this as multiple definitions of those variables and spit out the error you were seeing.

In this case (with the example from the book), if there's no Tree.cpp file (specifically, if there's only one .cpp file that #include s Tree.h), then you only get one definition of int Tree::objectCount and so the linker won't complain.

In general, though, you should probably put those static member declarations/initializations in a .cpp file (to avoid those linker errors).
Topic archived. No new replies allowed.