Solved

#include <mycode>

Posted on 2007-11-30
10
336 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
you may have to add

using namespace std;
before

int main()
{..}
0
 
LVL 86

Accepted Solution

by:
jkr earned 160 total points
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Troudeloup
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
#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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

744 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

16 Experts available now in Live!

Get 1:1 Help Now