Classes + headers

This is a discussion on Classes + headers within the C++ Programming forums, part of the General Programming Boards category; Hi I have a problem, Im trying to compile application on unix. Im using g++. Do I have to set ...

  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,185
    You have to compile all of your .cpp files together, or at the very least link all of your .o files together.

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,523
    ^^ for example:
    Code:
    g++ main.cpp otherfile.cpp -o main
    ...where main.cpp should #include the other header file.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 03:19 PM
  2. headers and objects/classes
    By l2u in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2007, 07: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, 04:18 AM
  5. Redefinition of classes (#include headers)
    By cjschw in forum C++ Programming
    Replies: 3
    Last Post: 09-04-2003, 10:24 PM

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