Thread: importing to project ( noob help )

  1. #1
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Question importing to project ( noob help )

    hello , I'm not too experienced with c++ yet and especially not projects. I've found out that when you have two source codes and you want to merge them together you need some kind of code binding then.

    Q1 : what is this code and can you tell me a bit more about whats so handy about different source codes.
    Q2 : can I upload an image to my program that way , and if not how can I.
    Q3 : how do I display it and can it be a .JPG instead of a Bitmap becuase I've only seen bMP's so far.

    for example :
    I wrote a little chat program ( without server yet ) and I want it to display a picture before the users name evry time the user says something , what do I have to add to this code then or do I need other graphics etc. ?

    Code:
    
    #include <iostream>
    using namespace std ;
    #include <string>
    #include <windows.h>
    
    
    int main ( int , char** )
    {
    unsigned char x = 4 ;
    char pname[64] ;
    unsigned char z = 0 ; // don't use some of these yet 
    unsigned char a = 2;
    char says[64] ;
    
    
    // --------------------colour-----------------------//
    
    //( removed this for the Xample to make it more easy to handle )//
    //-------------ask for username-------------------//
    Sleep (  x ) ;
    cout << endl << "username: " ;
    
    if ( pname != 0 ) {
    
      gets(pname);  
      
    //-------------------chat-----------------------//
    string message;
    
    while ( true ) { 
         cout << endl ;
        getline(cin,message);
        if( !message.length() ) break;
        cout  << pname << " says : " << message << endl;
    }
    
    
    //-----------when out of chat loop------------//
    cin.ignore();
    
    return 0 ;
    
    }
    }
    sorry for this newbish post , but I really wanna learn.

    thanks
    Last edited by MystWind; 03-06-2005 at 03:45 AM.
    PLay MystWind beta , within two years

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Here is an example:

    my_main.cpp:
    Code:
    #include<iostream>
    #include "Functions.h"
    
    using namespace std;
    
    int main()
    {
    	int a = 10;
    	display(a);
    
    	return 0;
    }
    Functions.h:
    Code:
    //Functions.h
    
    void display(int a);
    Functions.cpp:
    Code:
    #include<iostream>
    using namespace std;
    
    void display(int a)
    {
    	cout<<a<<endl;
    }
    When you #include a header file(i.e. a .h file) in your program, the include statement is replaced with the contents of the header file. The function header that is included above alerts the compiler something to the effect of: "hey, this function is defined in another file---go look for a .cpp file with the function definition in it."

    To show that an #include statement just inserts the code in the .h file, change my_main.cpp to this:
    Code:
    #include<iostream>
    
    void display(int a);
    
    using namespace std;
    
    int main()
    {
    	int a = 10;
    	display(a);
    
    	return 0;
    }
    ...and you should see that the program still works.

    You can also just do this:
    Functions.h:
    Code:
    #include <iostream>
    using namespace std;
    
    void display(int a)
    {
    	cout<<a<<endl;
    }
    my_main.cpp:
    Code:
    #include<iostream>
    
    #include "Functions.h"
    
    using namespace std;
    
    int main()
    {
    	int a = 10;
    	display(a);
    
    	return 0;
    }
    which includes the whole function in my_main.cpp. However, the norm is to have .h files with just function headers or class declarations and separate .cpp files for function defintions.

    As a matter of course, header files have what are called 'preprocessor directives' surrounding the code, which prevents a header file from being included more than once, which at the very least would cause code duplication, and at worst can cause errrors when there are definitions in the included file. In C++, you can't define something twice, e.g.

    int a = 10;
    int a = 10;

    will cause a redefinition error--the same thing happens with functions. Here is an example of preprocessor directives that prevent including a file more than once:

    Functions.h:
    Code:
    //Functions.h
    
    #ifndef FUNCTIONS_H  //can be any unique identifier
    #define FUNCTIONS_H
    
    void display(int a);
    
    #endif
    With those preprocessor directives, you can have this in my_main.cpp:

    #include "Functions.h"
    #include "Functions.h"

    and Functions.h will only be included once. You may think that is a stupid example because you would never do that, but when you have a large number of files it can be almost impossible to tell when you may be including the same header file more than once.
    Last edited by 7stud; 03-06-2005 at 03:24 AM.

  3. #3
    email for MystWind avatar MystWind's Avatar
    Join Date
    Feb 2005
    Location
    Holland , The Hague
    Posts
    88

    Smile

    Wow man your the best i'm studying it now to solidify
    7 min later --> ok I fully understand now thanks again
    Last edited by MystWind; 03-06-2005 at 04:10 AM.
    PLay MystWind beta , within two years

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob to programming need help with a project
    By Wheelsonbus in forum C Programming
    Replies: 6
    Last Post: 02-25-2009, 03:46 AM
  2. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM