Solved

#include <mycode>

Posted on 2007-11-30
10
343 Views
Last Modified: 2010-04-01
#include <windows.h>


is useful.


i have lots of function i want to use, but i don't want to having to copy them all over my codes that use them,

how do I #include  to use them?

they are only functions like this:



#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;

void mousemove( HWND hwnd1, int a, int b  );

int main ()
{
   return 0;
}


void mousemove( HWND hwnd1, int a, int b  )
{
      POINT p;
      p.x = a;
      p.y = b;
      ClientToScreen( hwnd1, &p );
      SetCursorPos( p.x, p.y );
      
}


0
Comment
Question by:Troudeloup
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 120 total points
ID: 20385361
in MyCode.H add the following lines

#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

void mousemove( HWND hwnd1, int a, int b  );

and in your mycode.cpp file add the following lines

#include "mycode.h"

using namespace std;

void mousemove( HWND hwnd1, int a, int b  )
{
      POINT p;
      p.x = a;
      p.y = b;
      ClientToScreen( hwnd1, &p );
      SetCursorPos( p.x, p.y );
     
}

in your main.cpp add following

#include "mycode.h"

int main ()
{
   mousemove( hwnd1, a, b  );
   return 0;
}

0
 
LVL 7

Assisted Solution

by:UrosVidojevic
UrosVidojevic earned 120 total points
ID: 20385363
1.)
Put declarations of all your functions in file "mycode.h".
For example:

#ifndef _MYCODE_H_
#define _MYCODE_H_

int myfunction1(int a);
double myfunction2(double b);

#endif

2.)
Put definitions of all your functions in file "mycode.cpp"
For example:

int myfunction1(int a) {
    return a*a;
}

double myfunction2(double b) {
    return b*b*b;
}

3.)
You need to include your header files in other files from which you want to use these functions:

#include "mycode.h"
0
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 100 total points
ID: 20385367
Hi Troudeloup,

You probably want to copy them to their own source file and create a header file with the prototypes.

//-----------------------------
//  Myheader.h

#ifndef MYHEADER_H
#define MYHEADER_H

void mousemove (HWND hwnd, int a, int b);
//  other prototypes


#endif

You'll need to #include all of the header files that this file will need.
You'll #include "myheader.h" in any and/or every source in your program.

You MAY wish to copy the functions to another cpp file and compile it into an object file.  This file is made part of your project so that it is compiled once and references to the modules are satisfied at load time.



Good Luck,
Kent
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 11

Assisted Solution

by:DeepuAbrahamK
DeepuAbrahamK earned 120 total points
ID: 20385368
you may have to add

using namespace std;
before

int main()
{..}
0
 
LVL 86

Accepted Solution

by:
jkr earned 160 total points
ID: 20385369
Separate them into several .cpp files and compile them together, e.g.


//mousemove.h
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

void mousemove( HWND hwnd1, int a, int b  );

//mousemove.cpp
#include "mousemove.h"
using namespace std;
void mousemove( HWND hwnd1, int a, int b  )
{
      POINT p;
      p.x = a;
      p.y = b;
      ClientToScreen( hwnd1, &p );
      SetCursorPos( p.x, p.y );
     
}

//main.cpp
#include "mousemove.h"
using namespace std;

int main ()
{
   return 0;
}

Compile:

g++ -o main.exe main.cpp mousemove.cpp
0
 

Author Comment

by:Troudeloup
ID: 20385444
so I type

g++ -o main.exe main.cpp 1.cpp 2.cpp 3.cpp   // and so on to compile them?



#include "n.h"



I include this line in  n.cpp and main.cpp?

or every cpp involved?
0
 

Author Comment

by:Troudeloup
ID: 20385449
also, if I use window.h in more than one cpp, would more than one window.h get includes?

i don't want to increase the compiled file size unnecesarrily.
0
 
LVL 7

Assisted Solution

by:UrosVidojevic
UrosVidojevic earned 120 total points
ID: 20385459
You need to enter #include "n.h" in every file that uses its functions.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 160 total points
ID: 20385467
>>also, if I use window.h in more than one cpp, would more than one window.h
>>get includes?

Nothing to worry about, that does not increase the size of your code. Also, "windows.h" has so-called "header guards", so it will only be included once.
0
 

Author Comment

by:Troudeloup
ID: 20385469
#include "n.h"


and in n.cpp itself?



but i didn't create n.h,  does it mean the compiler would generate .h from .cpp involved automatically?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question