Thread: Classes + headers

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    69

    Classes + headers

    Hi I have a problem, Im trying to compile application on unix. Im using g++.

    Do I have to set some special options or what? Because whenever Im trying to put class functions in *.cpp It doesnt see it. "undefined reference to ..". But if I define function in *.h file It does see it.

    A.Hello();

    Code:
    class A {
        public :
          void Hello(); // "undefined reference" even If i have defined it in *.cpp file
    };
    Code:
    class A {
        public :
          void Hello(){
              printf("ok"); //Its ok..
          };
    };

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have to compile all of your .cpp files together, or at the very least link all of your .o files together.

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    ^^ for example:
    Code:
    g++ main.cpp otherfile.cpp -o main
    ...where main.cpp should #include the other header file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c++ headers and classes
    By sjweinberg in forum C++ Programming
    Replies: 2
    Last Post: 08-29-2009, 02:19 PM
  2. headers and objects/classes
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2007, 06:38 AM
  3. Headers and Classes
    By MMD_Lynx in forum Game Programming
    Replies: 2
    Last Post: 12-01-2004, 10:40 AM
  4. Headers, Classes, and Functions question.
    By Zeusbwr in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2004, 03:18 AM
  5. Redefinition of classes (#include headers)
    By cjschw in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2003, 09:24 PM