Cant read my .h file?

Pages: 12
I made a program and i made a .h file called VirtualKeys.h so that when i use a Virtual Key i wont have to define the Virtual Keys every time. There might be something like this already but i want to make one so i can learn how to call files from main.cpp which is why im really making this. Here is the code in the main file:

1
2
3
4
5
6
7
8
#include <iostream>
#include <VirtualKeys.h>

int main()
{

    return 0;
}


Here is the code in the VirtualKeys.h file:

1
2
3
4
5
6
7
#ifndef VIRTUALKEYS_H_INCLUDED
#define VIRTUALKEYS_H_INCLUDED

#define VK_A 0x41
#define VK_B 0x42

#endif // VIRTUALKEYS_H_INCLUDED 


Now when i build it it gives me this error

C:\Users\Chay Hawk\Desktop\Include\main.cpp|2|error: VirtualKeys.h: No such file or directory|
||=== Build finished: 1 errors, 0 warnings ===|

Why is it saying this? The file exists and its in the project folder so what the heck?
<>s are for standard libraries (the compiler will look somewhere like path_to_your_compiler\include). For your own headers, use ""s.
Nevermind i got it, but i have another problem. I want to make a Word or something, not sure what the exact term for it is, a keyword of sorts. Ok i'll show you what i mean, i want to make a word called SAVE_FILE and i want to make it so that everytime SAVE_FILE is typed in main or wherever it will save the file to a text document. How do i do this? I did #define SAVE_FILE but after that im stumped.
Save what file?
I want it ot save a text file or whatever the user defines. basically SAVE_FILE will call the ofstream command so you dont have to type it. Its stupid i know but its just a test so i can make my own definitions and in turn make my own libraries.
Like #define SAVE_FILE(name) std::ofstream file(name); file << "hello world"; file.close(); ? Why not use a function?
its just a test, i usually do use functions but its hard to explain what i want to do but i'll try. Ok so VK_A 0x41 is the A key on your keyboard, it defines it and what i want to do is make my own words that do specific things, like my Virtual Key library i made:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef VIRTUALKEYS_H_INCLUDED
#define VIRTUALKEYS_H_INCLUDED

//This is a simple Virtual Key Library.
//This library can be used with any C++ project.
//Simply include the VirtualKeys.h Header file in your project
//and then type the Virtual Key you want (EX. VK_A).
//No need for defining the Key's hexidecimal value in your main file.


//Defining Virtual Keys A-Z

#define VK_A 0x41
#define VK_B 0x42
#define VK_C 0x43
#define VK_D 0x44
#define VK_E 0x45
#define VK_F 0x46
#define VK_G 0x47
#define VK_H 0x48
#define VK_I 0x49
#define VK_J 0x4A
#define VK_K 0x4B
#define VK_L 0x4C
#define VK_M 0x4D
#define VK_N 0x4E
#define VK_O 0x4F
#define VK_P 0x50
#define VK_Q 0x51
#define VK_R 0x52
#define VK_S 0x53
#define VK_T 0x54
#define VK_U 0x55
#define VK_V 0x56
#define VK_W 0x57
#define VK_X 0x58
#define VK_Y 0x59
#define VK_Z 0x5A

//End of A-Z Virtual Key Defining

//Defining Virtual Keys 0-9

#define VK_0 0x30
#define VK_1 0x31
#define VK_2 0x32
#define VK_3 0x33
#define VK_4 0x34
#define VK_5 0x35
#define VK_6 0x36
#define VK_7 0x37
#define VK_8 0x38
#define VK_9 0x39

//End of 0-9 Virtual Key Defining

#endif // VIRTUALKEYS_H_INCLUDED 


I plan on making it so that when you just type VK_A the code knows that it means when the A key is pressed do this. the reason for this is that it makes it easier on the developer, instead of having to spend all that time defining keys or whatever their doing. Now with that said what i want to do is basically make words that you can type in like the virtual key words, and they will perform an action that saves time. so if i make a word: CLEAR_SCREEN it would call system("cls") that was a bad example cause thats easy to type, but you get the main idea. so here is what i am trying to do currently without much sucess. I am trying to make a word that will clear the screen as a simple test. I want to make words that call functions and stuff too so how does the defined word know to do that? so if i type #define GET_SCREEN_CLEAR how will it know to call the function that has system("cls"); in it?
Again. #define CLEAR_SCREEN system("cls"); . Now when you write CLEAR_SCREEN in your code, the compiler will paste sytem("cls") there instead (when compiling). Though this produces the same result as writing a function void ClearScreen() { system("cls"); } (although these two work in very different ways).
Ok im starting to see, now when i do this:

Code in main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "VirtualKeys.h"

using namespace std;

int main()
{

    CLEAR_SCREEN;

    cout <<"Hello" << endl;

    return 0;
}


Code in .h file:

1
2
3
4
5
6
7
8
#ifndef VIRTUALKEYS_H_INCLUDED
#define VIRTUALKEYS_H_INCLUDED

#include <windows.h>

#define CLEAR_SCREEN system("cls");

#endif // VIRTUALKEYS_H_INCLUDED 


It doesnt clear the screen why is it not doing this?
Maybe because there is nothing on the console? You clear before printing. Unless you run your programs from cmd.. Either way, try replacing system("cls") with cout << "foo" or something.. If the compiler didn't know what to replace CLEAR_SCREEN with, you'd have gotten an error.
Also, why include windows.h?
Ok i got it now. The reason for windows.h is because system("cls") is a windows specific command i assume. i got an error when i didnt have it there so it must need it.
Now i need help with my Virtual Key library. So right now i want to be able to press A and have the key be stored in a text file, wich i did (i made a keylogger) and what i have there now is this:

1
2
3
4
5
 a=VK_A;
            if(a){
            log << "a" << flush;
            Sleep(150);
            }


Which doesnt work.

I had this which did work

1
2
3
4
5
 a=GetAsyncKeyState(0x41);
            if(a){
            log << "a" << flush;
            Sleep(150);
            }


This is what i have in the vk library. this time im trying to call it from a function.

1
2
3
4
5
6
#define VK_A akey();

int akey()
{
    GetAsyncKeyState(0x41);
}


Its basically the same thing so why doesnt it work? i tried Void in front of akey but it gave me an error saying that its not being ignored as it should be.
Last edited on
You need to return GetAsyncKeyState(0x41);
And where do i put that?
Like this?

1
2
3
4
5
6
#define VK_A akey();

int akey()
{
   return GetAsyncKeyState(0x41);
}


If so it still doesnt work.
Last edited on
It should work. I bet there's something silly. Post your whole code.
Here's mine (for no reason slightly altered) and working:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <Windows.h>
#include <iostream>

#define WAIT_FOR_A_KEY while(!akey());

int akey(){
    return GetAsyncKeyState('A');
}

int main(){
    WAIT_FOR_A_KEY
    std::cout << "yay!";
    Sleep(500);
    WAIT_FOR_A_KEY
}
Never mind about that code for now,im working on something slightly different, im still using #define but i want to know how to use a different word to output text to the console. So instead of using cout << "Hello" << endl; i would like to use print << "Text" << endl; basically make up my own commands. Im not sure how to do this though.

Here is the code i habve so far:

Main.cpp:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "Commands.h"

using namespace std;

int main()
{
    print << "Hello";

    return 0;
}


Commands.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef COMMANDS_H_INCLUDED
#define COMMANDS_H_INCLUDED

using namespace std;

#define print printline();

void printline()
{
    
}

#endif // COMMANDS_H_INCLUDED 
Last edited on
#define print cout ? You could work out a more complex system which manages several streams. But #define doesn't add anything. It's just a syntactic thing. You could have a variable ostream& print; ana (have functions that) assign cout or my_output_file or etc. to it.
That worked :D awesome, now i tried:

#define : ;

and it said:

error: macro names must be identifiers|
||=== Build finished: 1 errors, 0 warnings ===|


What does that mean?
That you can't use just any symbol for your macro, it has to be what you could use for a variable name. #define semicolon ; would work.
Pages: 12