Undefined Reference to a Defined Function?

closed account (zb0S216C)
It seems the linker thinks that my member function isn't defined, when it actually is.

The Problem:
I have a non-template member function call Price( ), which returns the price of the item; no surprise there. It's declared within the Database_Item structure. This structure inherits from Database_Item_Base with no conflicting names. Here's the declaration of Database_Item::Price( ):

1
2
U_Int &Price( );
const U_Int &Price( ) const;


The definition of these member functions are defined within a separate source file, which contains all definitions of Database_Item_Base and Database_Item.

Here's what I've tried so far:

1) Made sure all dependant files are included into the project
2) Made sure all object files are up-to-date
3) Made sure there's an actual definition
4) Made sure the declaration signature matches the corresponding definition
5) Made sure the directory to the source files is set (including headers)
6) Made sure the source (where the definitions are) includes the declarations

If it helps, here are the declarations of both structures:

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
//
// I T E M   B A S E
//
struct Database_Item_Base
{
    Database_Item_Base( );
    Database_Item_Base( const Database_Item_Base &Item );
    Database_Item_Base( const U_Int &New_ID, const char *New_Name );

    protected:
        //
        // D A T A   M E M B E R S
        //
        U_Int Base_ID_;
        const char *Base_Name_;

    public:
        //
        // M E M B E R   F U N C T I O N S
        //
        virtual U_Int &ID( );
        virtual const U_Int &ID( ) const;
        virtual const char *&Name( );
        virtual const char * const &Name( ) const;
};

//
// I T E M
//
struct Database_Item : public Database_Item_Base
{
    Database_Item( );
    Database_Item( const Database_Item &Item );
    Database_Item( const U_Int &New_Price );

    private:
        //
        // I N H E R I T E D   M E M B E R S
        //
        using Database_Item_Base::Base_ID_;
        using Database_Item_Base::Base_Name_;

        //
        // D A T A   M E M B E R S
        //
        U_Int Price_;

    public:
        //
        // I N H E R I T E D   M E M B E R   F U N C T I O N S
        //
        using Database_Item_Base::ID;
        using Database_Item_Base::Name;

        //
        // M E M B E R   F U N C T I O N S
        //
        U_Int &Price( );
        const U_Int &Price( ) const;

        //
        // O P E R A T O R S
        //
        Database_Item &operator << ( std::ostream &Stream );
        const Database_Item &operator << ( std::ostream &Stream ) const;
};

It's just Database_Item::Price( ) that causes the error; nothing else.

Thoughts?

Wazzak
Last edited on
closed account (zb0S216C)
Problem solved. It turns out I was defining Database_Item::Price( ) as inline.

Resolution for Future Readers:

If your function is being defined as inline within your source, remove the definition from the source file and place it within the class. That way, the compiler/linker can locate it.

Wazzak
Last edited on
I compiled this code and the only error or warning I got has to do with this.

1
2
U_Int &Price( );
const U_Int &Price( ) const;


The reason why, is because function signatures are only unique by their name and parameters so the compiler cannot distinguish these functions from one another. Also I believe a function must belong to a class to be able to be declared as const.

Hope this helps guy. Looking at this code really makes appreciate C# when it comes to writing Data Access Layers for applications. : )

Edit - I see you solved it. Nice, I never would have caught that with the code you posted. I noticed you are using the class access using statements quite a bit. Maybe you could rearrange your inheritance structure so you don't have to do adjust access explicitly that much. God luck.
Last edited on
closed account (zb0S216C)
Thanks for your reply, IceThatJaw :)

The Price( ) prototypes you referenced are associated with the Database_Item structure, as seen within my code in the original post (under the Member Function comment).

IceThatJaw wrote:
The reason why, is because function signatures are only unique by their name and parameters so the compiler cannot distinguish these functions from one another.

Actually, when a constant object of Database_Item is created, the compiler will select the read-only version of Price( ). However, the returned value must also be constant so that the compiler can distinguish between the two types returned.

IceThatJaw wrote:
Looking at this code really makes appreciate C# when it comes to writing Data Access Layers for applications. : )

I hate C# with a passion.

IceThatJaw wrote:
I noticed you are using the class access using statements quite a bit. Maybe you could rearrange your inheritance structure so you don't have to do adjust access explicitly that much.

It works without them, but I prefer to have them :)

Thanks again for your response.

Wazzak
Last edited on
1
2
        virtual const char *&Name( );
        virtual const char * const &Name( ) const;
¿Are you sure that you want to return a reference there?
closed account (zb0S216C)
ne555 wrote:
¿Are you sure that you want to return a reference there?

Why not?

Wazzak
Framework wrote:
Why not?

Do you want to be able to change the name through that function?
1
2
Database_Item_Base dib;
dib.Name() = "HAHA You get a new name! :P";
Last edited on
closed account (zb0S216C)
It won't let you - It returns a reference to a constant pointer to constant data, and additionally, it's read-only. Bullet-proof. And yeah, I wouldn't have put it there if I didn't want it to be changed.

Wazzak
Last edited on
virtual const char *&Name( );

This returns a constant reference but can you still change it since the method itself is not const? Nm, I will test this out.
Last edited on
Topic archived. No new replies allowed.