Solved

#include <mycode>

Posted on 2007-11-30
10
339 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:Kdo
Kdo 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now