Thread: Using header files problem!

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    8

    Using header files problem!

    Day 6 of 21

    My objective: To compile and execute a program which uses my custom "math class" for basic math functions
    My problem: I am getting a compiler error (take a look at the exact compile error I have included at the bottom of this thread)

    Here is the code of my three files (Main.cpp, MikesMath.h, MikesMath.cpp):

    Main.cpp
    Code:
    #include <iostream>
    #include "MikesMath.h"
    
    using namespace std;
    
    int main()
    {
        cout << "Hello: " << add(2,2) << endl;
        
        system("PAUSE");
        return 0;
    }
    MikesMath.h
    Code:
    #ifndef MIKESMATH_H_
    #define MIKESMATH_H_
    
    class MikesMath
    {
    public:
           MikesMath();
           ~MikesMath();
           int add(int x, int y);
           int multiply(int x, int y);
    };
    
    #endif
    MikesMath.cpp
    Code:
    #include "MikesMath.h"
    
    int MikesMath::add(int x, int y)
    {
        return x + y;
    }
    
    int MikesMath::multiply(int x, int y)
    {
        return x * y;
    }
    
    MikesMath::MikesMath()
    {
    
    }
    MikesMath::~MikesMath()
    {
                           
    }
    Here is what my compiler (GCC compiler/Using DevC++) is spitting at me:
    Image:
    http://img543.imageshack.us/img543/8...pilererror.png
    Last edited by Always; 08-05-2011 at 07:35 AM. Reason: To correct a capitalization error and a quick grammar fix

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Note that that is not a compiler error. The clue is the [Linker error] at the beginning. You need to put all your .cpp files in your project. If you don't have a project, then you need to make a project and put all your .cpp files in it.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    8
    Oh, alright. I also have one quick question. Is there a way to make my class like iostream where I can include it into any of my other programs without having to add it in the project?

    With just like a "#include <mikesmath.h>"

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    well it is still included in the project but yes., you create a header file called mikesmath.h and add it to the project, in there you put your own class definitions and function declarations. i would use " " instead of < > when it is your own header file.

    Unless you mean only include the header file and not have to have the actual working code, function definitions visible?
    Last edited by rogster001; 08-05-2011 at 07:45 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Always View Post
    Oh, alright. I also have one quick question. Is there a way to make my class like iostream where I can include it into any of my other programs without having to add it in the project?

    With just like a "#include <mikesmath.h>"
    iostream (as part of the C++ runtime) has to be added to your project. However, it has already been done for you.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well the initial problem is that you're writing a console program (with main), but the IDE tried to create a GUI program (expected to start at winmain), and this failed.

    > cout << "Hello: " << add(2,2) << endl;
    This is not invoking your class member function. You need a class instance variable to make this happen.

    Also, dev-c++ can be a rather buggy IDE, and has not been updated for a good few years now (nor will it be)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    MaikesMath math;
    std::cout << math.add(2, 2);

    Also, to address other concerns...
    iostream is never "included by default" (unless you mean the actual implementation and not the declarations/definitions). You must always include <iostream> to use it. The same goes for your class. And since the implementation is not in your header, you must also add your source file to your project.
    Are there ways around that? Yes, by using static libraries or DLLs and configuring your IDE to always search the appropriate .lib file you create. But that's probably too advanced for you right now.
    In any case, you must always include the header file. No way around that.

    You should also switch IDE and create a "Console application." Not a Windows Application, Windows Forms Application, Win32 Application or anything like it. Just a console application.
    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.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You shouldn't actually be using a class in this instance as you have no member variables. What you actually want is just a namespace.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with header files
    By Farnaz in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2011, 12:11 PM
  2. Structures and header files problem
    By Quinn90 in forum Windows Programming
    Replies: 1
    Last Post: 01-27-2011, 09:35 AM
  3. Problem with Header files.
    By chakra in forum C Programming
    Replies: 5
    Last Post: 05-10-2008, 01:30 PM
  4. Strange problem with classes in header files
    By samGwilliam in forum C++ Programming
    Replies: 2
    Last Post: 02-29-2008, 04:55 AM
  5. include library header in header files
    By Raison in forum C++ Programming
    Replies: 6
    Last Post: 09-27-2004, 02:50 AM