Undefined reference?

Pages: 12
When ever I try compiling this code I get undefined reference to something::get why is that?
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
#include <iostream>
using namespace std;

class something {
protected:
    struct random {
        public:
            union {
                int x;
                int v;
            };
            int a;
    };
    static random* get;
};
class another : public something {
public:
    int test(int value) {
        get->x = value;
        return get->x;
    }

        
};
int main() {
    int ret;
    another thing;
    cout << "Enter a value: ";
    cin >> ret;
    cout << thing.test(ret);
}
When ever I try compiling this code I get undefined reference to something::get why is that?

Because you've never defined something::get, you have only declared it.

Outside of the class somewhere you need to put:

something::random* something::get = /* some value */ ;
Last edited on
Well for some reason that didn't work? However, when I changed random from static to non static it worked, why is that? I'll try and define it again outside the class once more but how come it works without static?
Well for some reason that didn't work?

That's... very helpful. What do you mean by "didn't work?" If you left the (static) declaration in the class, the definition outside the class should work.


However, when I changed random from static to non static it worked, why is that? I'll try and define it again outside the class once more but how come it works without static?

It's always good to use keywords when you don't know what they do.

A static variable in a class is a "class" variable. It is a single variable that exists as a member of the class. A non-static variable is an "instance" variable where every object or instance of the class type has its own member variable.
closed account (E0p9LyTq)
http://stackoverflow.com/questions/11300652/static-data-member-initialization
@Cire sorry for being extremely vague with my response I sort of have, not really...And yeah I kind of had an understanding of static its just that this was the first for me using them. But thanks for explaining it to me better.

Anyways the problem I have now is how do I set get to point to all the values within random? As this didn't work obviously?
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
#include <iostream>
using namespace std;

class something {
protected:
    struct random {
        public:
            union {
                int x;
                int v;
            };
            int a;
    };
    static random* get;
};
something::random* something::get = something::random;
class another : public something {
public:
    int test(int value) {
        get->x = value;
        return get->x;
    }

        
};
int main() {
    int ret;
    another thing;
    cout << "Enter a value: ";
    cin >> ret;
    cout << thing.test(ret);
}

Same with this one...
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
#include <iostream>
using namespace std;

class something {
protected:
    struct random {
        public:
            union {
                int x;
                int v;
            };
            int a;
    };
    static random* get;
};
something::random* something::get = something::random* something::random;
class another : public something {
public:
    int test(int value) {
        get->x = value;
        return get->x;
    }

        
};
int main() {
    int ret;
    another thing;
    cout << "Enter a value: ";
    cin >> ret;
    cout << thing.test(ret);
}

Thanks to the furry guy for link, as it helped my understand of static members more!
> As this didn't work obviously?
That's... very helpful. What do you mean by "didn't work?"



- ¿what do you want to do?
- ¿why is `get' a pointer?
- ¿why are you using an union?
Last edited on
Oh my goodness this is seriously an issue lol. From now on I pledge to be as specific as possible haha. Ok what I would like to do is make *get be a static pointer that I can use in a derived class in order to be able to use/manipulate the variables within random with the use of the get pointer. And I'm using a union because unions hold only a single value and well I'm only going to be storing and using one object at a time so that's why. And the error is at line 16 when I'm trying to initialize get to the random class that I can use get and have it points to its objects within the derived classes.
Ok what I would like to do is make *get be a static pointer that I can use in a derived class in order to be able to use/manipulate the variables within random with the use of the get pointer.

Why a pointer? What do you mean "within random?" something::random is a type.


And I'm using a union because unions hold only a single value and well I'm only going to be storing and using one object at a time so that's why.

Variables hold single values and you can use one at a time. Why are you using a union?


And the error is at line 16 when I'm trying to initialize get to the random class that I can use get and have it points to its objects within the derived classes.

You have no objects of type something::random, so you have nothing to point your pointer to.
Sorry about my misunderstanding and my poor word choice.
Why a pointer? What do you mean "within random?" something::random is a type.

Well I guess it doesn't have to be a pointer, I could make it not pointer. However what I mean "within random" was have the static value be a "random" type and thus house all of its components is what I meant.

Variables hold single values and you can use one at a time. Why are you using a union?

I know variables hold single and I can use on a time but I'm using a union because they only hold a single object out of a bunch of objects at a time and well yeah, I'll only have one object active/in use at a time. Unless my understanding of unions is incorrect, in which case fail...

You have no objects of type something::random, so you have nothing to point your pointer to.


I understand that but the problem is that how do I assign it to point to something. Where do I even get a value of type random for this thing to point to?
The only thing I know that it can be assigned to is a nullptr, which is just sad...
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
#include <iostream>
using namespace std;

class something {
protected:
    struct random {
        public:
            union {
                int x;
                int v;
            };
            int a;
    };
    static random* get;
};
something::random* something::get = nullptr;
class another : public something {
public:
    int test(int value) {
        get->x = value;
        return get->x;
    }

        
};
int main() {
    int ret;
    another thing;
    cout << "Enter a value: ";
    cin >> ret;
    cout << thing.test(ret);
}

.....
Here's something that compiles. I'm 99% sure this isn't what you want to do. You still haven't really told us what you're trying to do. You've told us the "solution" you want to use, but we don't know what problem you think you're solving with it.

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

class something {
protected:
    struct random {
    public:
        union {
            int x;
            int v;
        };
        int a;
    };
    static random object;
};

something::random something::object ;

class another : public something {
public:
    int test(int value) {
        something::object.x = value;
        return value;
    }


};
int main() {
    int ret;
    another thing;
    cout << "Enter a value: ";
    cin >> ret;
    cout << thing.test(ret);
}
Ok let me tell you what I'm trying to do, as this was a small example compared to the real thing I was having issue. Anyways the problem I'm using this to solve for is making an armor class. In my armor class I have a struct and inside a union of armor types, steel, iron, leather, etc. Also inside the struct are the states, physical resist, cold resist, fire resist, etc. And then I have 4 static variables of that struct for each one of my classes. The helm class, chest class, leggings class, and gloves class. The reason I needed static variables was so that they wouldn't have to be remade for each class instance. And then within the base class, in the public section I have virtual functions for my derived classes.
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
#include <iostream>

using namespace std;

class armor {
private:
    struct ARMOR_ELEMENTS {
        typedef bool ARMOR;
        union {
            ARMOR STEEL;
            ARMOR IRON;
            ARMOR LEATHER;
            ARMOR TITANIUM;
        };
        int defenseRange[2];
        int fireResist;
        int coldResist;
        
    };
    static int armorCount;
    static ARMOR_ELEMENTS Hpiece;
    static ARMOR_ELEMENTS Cpiece;
    static ARMOR_ELEMENTS Lpiece;
    static ARMOR_ELEMENTS Giece;

public:
    virtual int returnArmor() {
        return armorCount;
    }
    virtual void setArmor() = 0;
};
class helmet : armor {
private:
    
};
int main() {
    cout << "Enter armor";
    armor H;
    H.collectArmor(10);
    cout << H.returnArmor;
}
That would be a definite abuse of the union.

This makes more sense to me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class armor
{
    enum material { steel, iron, leather, titanium };
    enum body_part { head, chest, legs, hands };

    material _material;
    body_part _slot;

    int _defense[2];
    int _fire_resist;
    int _cold_resist;
    // ...

public:
   // public interface

};


I can't, for the life of me, imagine having only one piece of armor in the entire game for the head, chest, legs or hands. Want variety? Too bad!
Wait can't you change/switch which object you want in a union at run time or no? I'm really inexperienced with them and I was only told or saw that they hold one object at a time. Also I never even thought about using an enum. Are enums/ the variables they hold static, can you make them static?

EDIT: Jesus my grammar/format was terrible in that last post after* re-reading.
EDIT2: Alright you can make enums static although I'd need to learn how to use them correctly.
Last edited on
Wait can't you change/switch which object you want in a union at run time or no?

Yes. But you have to keep track of which member of a union is active. And since you have to keep track of that, you're just introducing another level of (unnecessary) indirection with a union.

Also I never even thought about using an enum. Are enums/ the variables they hold static, can you make them static?

An enum is a type. (You may also want to consider a scoped enum, but I'll let you research that for yourself.) A variable of enum type can be static, yes, if you make it so. (But I think you're barking up the wrong tree with static variables here, too.)
But aren't static variables kinda like global variables with a scope? Thus they never change in values unless you manually change them correct? Thus wouldn't that be useful since I always know what my armor values are?
Last edited on
But aren't static variables kinda like global variables with a scope?

Yes.


Thus they never change in values unless you manually change them correct?

In that respect, they're not different than any other variable. Nothing changes unless you change it.


Thus wouldn't that be useful since I always know what my armor values are?

If you always know what your armor values are, why store it at all?
Alright I'm going to make another pledge in that I will now correctly format and revise my sentences haha.
Thus wouldn't that be useful since I always know what my armor values are?

What I meant to say was wouldn't it be useful to have my variables mainly since they can always store armor stats, thus I always know what armor I have and they won't be replaced unless they need to be.
What I meant to say was wouldn't it be useful to have my variables mainly since they can always store armor stats, thus I always know what armor I have and they won't be replaced unless they need to be.

I still don't know what you're trying to say. But, consider - would you ever want more than one player in the game? Would you ever want your player to control more than one character in a party?
Would you ever want to reuse the armor class for npcs?

Do you imagine having these static variables would help or hinder in these hypothetical situations?
Do you think this one is a better version than the other one I posted earlier? I changed the union to an enum struct(which is a scoped enum correct?) to make more simple. I also put all the body part pieces into a single struct and then had a static variables of that struct type to permanently house/know the variable info.

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
#include <iostream>

using namespace std;

class armor {
private:
    struct ARMOR_ELEMENTS {
        enum struct ARMORS {
            STEEL = false,
            IRON = false,
            LEATHER = false,
            TITANIUM = false
        };
        ARMORS type;
        int defenseRange[2];
        int fireResist;
        int coldResist;
        
    };
    static int armorCount;
    struct pieces {
        ARMOR_ELEMENTS Hpiece;
        ARMOR_ELEMENTS Cpiece;
        ARMOR_ELEMENTS Lpiece;
        ARMOR_ELEMENTS Giece;
    };
    static pieces PART;
   
public:
    virtual int returnArmor() {
        return armorCount;
    }
    virtual int setArmor() = 0;
};
armor::pieces armor::PART;
int main() {
    int value;
    cout << "Enter armor: ";
    cin >> value;
    armor H;
}
Last edited on
Pages: 12