Results 1 to 10 of 10

Thread: External cpp

  1. #1
    DaoK
    Guest

    External cpp

    I have added a class in an other cpp, and I want to use it, how ? Do I need to do something because it seem that it does not see it grrr

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    you have to include it in the main cpp file

    #include "myfile.cpp"


  3. #3
    DaoK
    Guest
    thx but now I have an other problem :error C2086: 'cxHandle' : redefinition
    insideLogin.cpp

    What is that ? both have #include "mainHeader.h" and in mainHeader.h I have an other header called util.h who have the cxHandle but why the debugger tell me redefinition and how can I fix that ?

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Put the class definition into a header, and the source into a .cpp file:

    myclass.h:
    Code:
    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include <string>
    
    class MyClass {
    public:
        MyClass(const std::string &name, int number)
            : m_name(name), m_number(number) { }
    
        const std::string& Name() const { return m_name; }
        void Name(const std::string &nm) { m_name = nm; }
    
        int Number() const { return m_number; }
        void Number(int num) { m_number = num; }
    
        void Print();
    
    private:
        std::string m_name;
        int m_number;
    };
    
    #endif // MYCLASS_H
    myclass.cpp:
    Code:
    #include <iostream>
    #include "myclass.h"
    
    using namespace std;
    
    void myclass::Print() {
        cout << m_name << " : " << m_number << endl;
    }
    main.cpp:
    Code:
    #include <iostream>
    
    #include "myclass.h"
    
    int main() {
        MyClass m("Hello", 25);
        m.Print();
    
        m.Name("Mikey");
        m.Print();
    }
    ...and so on

    PS: There might be a few typos in there, I did it straight into the reply window which isn't exactly great for coding
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    DaoK
    Guest
    parksie I know that you have already take a look at my little project and I have used #define but it doesn't work, can you take an other look please, juste 2 sec.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    The #defines have to be right at the very outer edges of the file. Also, the class definitions all have to be in headers.

    PS: Don't include .cpp files.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    DaoK
    Guest
    If I do not have to include CPP taht mean that I need to put all my class in an Header?

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    All the class definitions (class whatever { } need to go into a header, and the code for the functions in those classes need to go into a .cpp file.

    Well, you can write small functions inline inside the header and they'll get expanded for extra performance (but big functions won't, so it's best to put those in source files).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    DaoK
    Guest
    well I have bought Thinking in C++ Second Edition Vol 1 and I might more understand where to put stuff because in Sam's Teach yourself C++ in 21 days all example are in 1 .cpp.

    Thx bro for all your time

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It's a damn good book, that

    It's been my reference on many occasions.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width