Using multipul files, c++

This is a discussion on Using multipul files, c++ within the C++ Programming forums, part of the General Programming Boards category; hey there, im a newbie, so, im very sorry is this has been asked before. Im using the anjute ide, ...

  1. #1
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44

    Using multipul files, c++

    hey there, im a newbie, so, im very sorry is this has been asked before. Im using the anjute ide, for linux. using gcc as my compiler.

    im using multipul .cc files, how can I declare a function in one file, and then call it from another ?, without getting an 'undeclared ifentifier' error ?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Read this post.

    It's in the C forum but the concept is the same. Post back if you need any clarifications or have any questions.

    gg

  3. #3
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    that post want much help

    hmm, im using a class, sorry, not a function, I dont know why I said function.

    does it make it any diferent ?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you can declare the class in an .h file and define the members of the class in the .cpp file. Then in the program file list the .h file in the list of #includes. This then makes the class available throughout the program (unless you used a namespace other than std to declare the class in in the .h file). It's almost the same as using cin and cout after listing the iostream file in the list of #includes.

  5. #5
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    yah, I was thinking it was going to be something like that. how do I 'declare' the class in the .h file ?

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,662
    Ok, here's the C++ version of that post.
    Code:
    // myclass.h
    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    class MyClass
    {
        int m_a, m_b;
    public:
        MyClass(int a, int b) : m_a(a), m_b(b) {}
    
        void Print();
    };
    
    #endif //MYCLASS_H
    Code:
    // myclass.cpp
    #include <iostream>
    using namespace std;
    #include "myclass.h"
    
    void MyClass::Print()
    {
        cout << "a = " << m_a << ", b = " << m_b << endl;
    }
    Code:
    // main.cpp
    #include "myclass.h"
    
    int main()
    {
        MyClass ms(1,2);
    
        ms.Print();
    
        return 0;
    }
    gg

  7. #7
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    hmm, ive had a play around, and Im not sure why it works, but it does. Please bare in mind I am at school, and dont have an IDE to write this in, but here goes :


    Code:
    main.cpp
    
    #include <iosteam>
    #include "class.h"
    
    using namespace std;
    
    int main ()
    
    {
    
        c_class x_class;
    
        x_class.sayhello("hello world :D ");
        return 0;
    
    }
    
    class.h
    
    class c_class 
    {
    public :
    
        void say_hello(int x)
    
        {
    
        cout << x;
         }
    
         void func2 ()
    
        { 
        // func 2 
        }
    
        void func3 ()
        {
        // func 3
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM
  5. header and source files
    By gtriarhos in forum C Programming
    Replies: 3
    Last Post: 10-02-2005, 03:16 AM

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