English 中文(简体)
Undefined reference
原标题:

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically.

First, I have namespace LCD. Then I have two separate files, LCDText.h and LCDGraphic.h.

LCDText.h:

//[snip]
void TextDraw(Widget *w);

void TextBarDraw(Widget *w);

void TextHistogramDraw(Widget *w);

void TextIconDraw(Widget *w);

void TextBignumsDraw(Widget *w);

void TextGifDraw(Widget *w);

}; // End namespace

LCDGraphic.h:

//[snip]
void GraphicDraw(Widget *w);

void GraphicIconDraw(Widget *w);

void GraphicBarDraw(Widget *w);

void GraphicHistogramDraw(Widget *w);

void GraphicBignumsDraw(Widget *w);

void GraphicGifDraw(Widget *w);

}; // End namespace

And in WidgetBignums.h I have:

//[snip]
using namespace LCD;

extern void TextBignumsDraw(Widget *w);
extern void GraphicBignumsDraw(Widget *w);

template <class T>
WidgetBignums<T>::WidgetBignums(Generic<T> *v, std::string n, Json::Value *section,
    int row, int col) : Widget(n, section, row, col,
    WIDGET_TYPE_BIGNUMS | WIDGET_TYPE_RC | WIDGET_TYPE_SPECIAL) {

    if( v->GetType() == LCD_TEXT )
        Draw = TextBignumsDraw; // Line 66
    else if( v->GetType() == LCD_GRAPHIC )
        Draw = GraphicBignumsDraw;
    else
        Draw = NULL;
//[snip]

And I get the following linker error:

LCDControl.o: In function `WidgetBignums :
/home/starlon/Projects/LCDControl/WidgetBignums.h:66: undefined reference to `LCD::TextBignumsDraw(LCD::Widget*) 

Now here s one way to fix it, but I don t like it. I can move LCD::TextBignumsDraw outside of the LCD namespace and it works. Strange enough, the linker sees LCD::GraphicBignumsDraw. Any clues?

Edit: I m using gcc 4.4.1-2 on Fedora 11. Using SCons to compile.

Edit: Here s WidgetBignums, showing Draw.

template <class T>
class WidgetBignums : public Widget {
    Generic<T> *visitor_;
    std::vector<char> FB_;
    std::vector<char> ch_;
    int min_;
    int max_;
    int update_;
    int layer_;
    Property *expression_;
    Property *expr_min_;
    Property *expr_max_;

    QTimer *timer_;

    void (*Draw)(Widget *);

    public:
    WidgetBignums(Generic<T> *visitor, std::string name, Json::Value *section, int row, int col);
    ~WidgetBignums();
    void TextScroll() {};
    void SetupChars();
    void Update();
    void Start();
    void Stop();
    std::vector<char> GetFB() { return FB_; }
    std::vector<char> GetCh() { return ch_; }
    Generic<T> *GetVisitor() { return visitor_; }
};

Edit: Here s TextBignumsDraw s signature.

//[snip]
void TextBignumsDraw(Widget *w) {
//[snip]

Edit: Incidentally, I m getting the same error for TextHistogramDraw and TextGifDraw as well. TextIconDraw and the others are fine.

最佳回答

Where is the definition for LCD::TextBignumsDraw()? That s what the linker seems to be complaining about. Not the declaration, but the actual definition of the function.

The fact that when you move the declaration out of namespace LCD things start working indicates that the definition for TextBignumsDraw() is in the global namespace, not the LCD namespace.

This (in some .cpp file):

void TextBignumsDraw(Widget *w) {
    // ...
}

Needs to be wrapped in a

namespace LCD {

}

block.

问题回答

Try dropping the "using namespace LCD", and change that line 66 to:

Draw = LCD::TextBignumsDraw;

That s more explicit, which may help the linker understand what you re asking for.

Besides, you should never say "using namespace Anything" in a header file. It hoists everything in that namespace out into the global space for every user of that header. That almost completely destroys the value of having a namespace in the first place. You should hoist things out like this in the narrowest scope that s practical. Sometimes I put "using namespace foo" at the top of a single function, for instance, if that s the only user of the bits in the namespace within a given .cpp file.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签