Solved

#include <mycode>

Posted on 2007-11-30
10
345 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Embarcadero C++ Builder XE2 TDateTime 8 80
FMX StringGrid1->Canvas->FillRect Problem 3 200
print bytes of an integer 6 27
C++ statement T∗ begin(Vector<T>& x) 5 12
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

730 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