header files are driving me up the wall

This is a discussion on header files are driving me up the wall within the C++ Programming forums, part of the General Programming Boards category; I have gone thru the FAQ and have gone thru search but I am still confused. My header file has ...

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    32

    header files are driving me up the wall

    I have gone thru the FAQ and have gone thru search but I am still confused. My header file has only the class and is using the macro guards and my cpp file has the actual functions with an include. I quickly wrote up a litte test prog, thinking my actual program may have other issues.
    Code:
    #include "tst.h"
    
    int main()
    {
    	tst x;
    	x.run();
    	return (0);
    }
    Code:
    #include "tst.h"
    
    void tst::run()
    {
     	cout<<"working";
    }
    Code:
    #ifndef TST
    #define TST
    
    class tst
    {
    	private:
    	  char ch;
    	public:
    	  void run();
    };
    #endif
    If I try to run this exact program I get the following errors

    Compiling RUN.CPP:
    Linking run.exe:
    Linker Warning: No module definition file specified: using defaults
    Linker Error: Undefined symbol tst::run() in module RUN.CPP

    But if I change the include in main to #include "tst.cpp" the program runs. Why is this

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You need to link the files together. You'd do something like

    g++ -o myprog.exe run.cpp test.cp

    You're compiling only run.cpp, which when linked on it's own, can't find the run() function.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    g++ -o myprog.exe run.cpp test.cp


    Huh!!! What is that?? Where do I put it...and can you please explain it. I have never seen anything like that. They surely didn't show or say anything at uni or in any of the books I have.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Huh!!! What is that??
    Perhaps you could tell us what compiler you use and then how you are compiling and linking your program?
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Jan 2004
    Posts
    32
    Its a borland 4.5. Our lecturer says that he will only run our programs on what the uni has. Well the format of the code is exactly as it is above and then I just run it. I dont have anything like hammer had written.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I dont have anything like hammer had written.
    That's because you're using an IDE instead of a command line invocation of the compiler. Unfortunately, I haven't used that dinosaur of a compiler in many a year, so you'll just have to read the help files on how to build a multi-file project.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Try this:

    in the tst.cpp file add this line:

    #include <iostream.h>

    to get the cout object available to your program and in the tst.h file change the first two lines to this:

    #ifndef TST_H
    #define TST_H

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    ur university should have given u a unix account. if not go to help desk and tell them to give u a user name and pass, after uve gotten that u can log on from a shell and link ur program the way Hammer said.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 02:42 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. C header files?
    By CyC|OpS in forum C Programming
    Replies: 16
    Last Post: 09-14-2002, 08:06 PM

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