Thread: Problems compiling sigc++ example

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    6

    Problems compiling sigc++ example

    Hi,

    i'm trying to get sigc++ working with the example files from the tutorial of libsigc++;
    http://libsigc.sourceforge.net/libsi...html#id2446517

    AlienDetector.h
    Code:
    #include <iostream>
    #include "sigc++/object.h"
    #include <string>
    #include "sigc++/signal.h"
    
    class AlienDetector
    {
    public:
        AlienDetector();
    
        void run();
    
        sigc::signal<void> signal_detected;
    };
    AlienDetector.cc
    Code:
    #include "AlienDetector.h"
    
    void warn_people()
    {
        cout << "There are aliens in the carpark!" << endl;
    }
    
    int main()
    {
        AlienDetector mydetector;
        mydetector.signal_detected.connect( sigc::ptr_fun(warn_people) );
    
        mydetector.run();
    
        return 0;
    }
    Compileline:
    Code:
     g++ -Wall AlienDetector.cc -o AlienDetector.o -I/usr/include/sigc++-2.0 -I/usr/lib/sigc++-2.0/include -lsigc-2.0
    Error:
    Code:
    /tmp/ccBPmiQJ.o: In function `main':AlienDetector.cc:(.text+0x50): undefined reference to `AlienDetector::AlienDetector()'
    :AlienDetector.cc:(.text+0xcb): undefined reference to `AlienDetector::run()'
    collect2: ld returned 1 exit status
    In the tutorial the compileline is using pkconfig inside, which i can't use in the project I want to use sigc with. So i wrote a compileline myself, which should work in my opinion.

    Thanks for the help
    Hork83

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You never implemented the constructor and run() function inside AlienDetector, so the link can't link to their definitions.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    6
    i added to AlienDetector.cc

    Code:
    AlienDetector(){}
    void run(){
    signal_detected();
    }
    Now coming up with the compileerrors

    Code:
    AlienDetector.cc:3: error: expected unqualified-id before ‘)’ token
    AlienDetector.cc: In function ‘void run()’:
    AlienDetector.cc:5: error: ‘signal_detected’ was not declared in this scope
    Thats the line where i wrote AlienDetector(){}
    Used same compileline as stated above.

    Thanks for help
    Last edited by Hork83; 06-19-2007 at 04:55 AM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When defining class member functions, you have to specify that they belong to the class, so you would add AlienDetector:: in front of the function names in the cc file.

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    6
    Ok, i added these...
    AlienDetector.h
    Code:
    #include <iostream>
    #include "sigc++/object.h"
    #include <string>
    #include "sigc++/signal.h"
    
    using namespace std;
    
    class AlienDetector : public SigC::Object
    {
    public:
    	AlienDetector::AlienDetector();
    
        void AlienDetector::run();
    	void AlienDetector::warn_people();
    	AlienDetector::sigc::signal<void> signal_detected;
    };
    AlienDetector.cc
    Code:
    #include "AlienDetector.h"
    
    AlienDetector::AlienDetector(){}
    void AlienDetector::run(){
    signal_detected();
    }
    
    void AlienDetector::warn_people()
    {
        cout << "There are aliens in the carpark!" << endl;
    }
    
    int main()
    {
        AlienDetector mydetector;
        mydetector.signal_detected.connect( sigc::ptr_fun(warn_people) );
    
        mydetector.run();
    
        return 0;
    }
    i get now the following errors, that seems to say that the signal isn't properly declared.

    Code:
    AlienDetector.h:16: error: ‘class AlienDetector::sigc’ has not been declared
    AlienDetector.h:16: error: ISO C++ forbids declaration of ‘signal’ with no type
    AlienDetector.h:16: error: expected ‘;’ before ‘<’ token
    AlienDetector.cc: In member function ‘void AlienDetector::run()’:
    AlienDetector.cc:5: error: ‘signal_detected’ was not declared in this scope
    AlienDetector.cc: In function ‘int main()’:
    AlienDetector.cc:16: error: ‘class AlienDetector’ has no member named ‘signal_detected’
    AlienDetector.cc:16: error: ‘warn_people’ was not declared in this scope
    Whats wrong now?

    Thanks
    Hork83

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    As I said, you would need to add AlienDetector:: in front of the function names in the cc file. You also added it in front of the names in the .h file, which is not what you want. Remove all the AlienDetector:: in the h file and try again. Your cc file looks fine now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  2. problems with including headers
    By l2u in forum C++ Programming
    Replies: 3
    Last Post: 07-23-2006, 08:06 AM
  3. Problem Compiling
    By Flakster in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 01:09 AM
  4. Linking problems, class problems
    By Kheila in forum C++ Programming
    Replies: 12
    Last Post: 11-22-2005, 01:47 AM
  5. compiling problems LNK2019 error
    By sugie in forum C++ Programming
    Replies: 2
    Last Post: 10-19-2005, 05:25 PM