confusing error :S

This is a discussion on confusing error :S within the C++ Programming forums, part of the General Programming Boards category; Hi this is my first post, and I'm kind of a beginner (re-learning) so i'm sure my code is full ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    2

    confusing error :S

    Hi this is my first post, and I'm kind of a beginner (re-learning) so i'm sure my code is full of bad coding practices etc.... anyway i've been making a little bit of code that is a window using gtkmm that you can quicky add buttons and bind member functions of other classes too and i'm toootally stumped by this bug (although no doubt its something stupid i've done). if anybody can point me in the right direction it would make me VERY happy.

    the bug :-

    g++ ./build/control_panel.o ./build/test.o -o build/control `pkg-config gtkmm-2.4 --libs`
    ./build/test.o: In function `Test::run()':
    test.cpp.text._ZN4Test3runEv[Test::run()]+0x5f): undefined reference to `void ControlPanel::add_button<Test, Test>(Glib::ustring, Test*, void (Test::*)())'

    here's my code

    control_panel.h
    Code:
    #ifndef GUARD_CONTROL_PANEL_H
    #define GUARD_CONTROL_PANEL_H
    
    #include <gtkmm.h>
    
    class ControlPanel : public Gtk::Window {
    public:
    	ControlPanel();
    	ControlPanel(Glib::ustring caption);
    	virtual ~ControlPanel();
    
    	template<class S, class T>
    	void add_button(Glib::ustring caption, S *object, void (T::*member_function_ptr)());
    protected:
    	Gtk::VBox widget_box;
    };
    #endif
    control_panel.cpp
    Code:
    #include "control_panel.h"
    
    ControlPanel::ControlPanel() {
    	set_title("control panel");
    	add(widget_box);
    	show_all_children();
    }
    
    ControlPanel::ControlPanel(Glib::ustring caption) {
    	set_title(caption);
    	add(widget_box);
    	show_all_children();
    }
    
    ControlPanel::~ControlPanel() {
    }
    
    // create button connected to a member function
    template<class S, class T> 
    void ControlPanel::add_button(Glib::ustring caption, S *object, void (T::*member_function_ptr)()) {
    	// create new button
    	Gtk::Button *button = new Gtk::Button(caption);
    	widget_box.pack_start(*button);
    
    	// connect member function to button.
    	button->signal_clicked().connect(sigc::mem_fun(*object, member_function_ptr));
    }
    test.cpp
    Code:
    #include "control_panel.h"
    #include <iostream>
    #include <gtkmm.h>
    
    class Test {
    public:
    	Test() { control = new ControlPanel(); }
    	~Test() { delete control;}
    
    	void show() { std::cout << "coooool" << std::endl; }
    	
    	void run() {
    		Glib::ustring s = "Button";
    		control->add_button(s, this, &Test::show);
    	}
    
    protected:
    	ControlPanel *control;
    };
    
    int main(int argc, char *argv[]) {
    	Test test;
    	test.run();
    	return 1;
    }

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Template functions should be done in the header. Try that and see if it works.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    2
    Thanks for the response. I tried it and it didn't work (but will continue to do that if its considered good practice). Stripped all the Gtk out of it and it ran fine, after a bit more detective work it appeared to be something to do with inheriting Gtk::Window. I realised that I didn't want to inherit anyway as i am just making a wrapper class so I included the window object as a class variable and.... problem solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. confusing error messages?
    By ssjnamek in forum C Programming
    Replies: 6
    Last Post: 01-26-2006, 07:56 PM
  2. Arrays are confusing...
    By GavinHawk in forum C Programming
    Replies: 10
    Last Post: 11-29-2005, 12:09 AM
  3. Confusing Pointer
    By loko in forum C Programming
    Replies: 4
    Last Post: 08-29-2005, 08:52 PM
  4. pointers are confusing!
    By ali1 in forum C Programming
    Replies: 10
    Last Post: 09-07-2004, 10:41 PM
  5. functions declaration, pointers, cast, .... CONFUSING
    By Rhodium in forum C Programming
    Replies: 7
    Last Post: 01-09-2003, 05:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21