14 Replies - 650 Views - Last Post: 02 October 2012 - 01:46 PM Rate Topic: -----

#1 sebwerner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-September 12

First attempt using header files, dosent work :/

Posted 02 October 2012 - 12:09 AM

So im a newb at c++, been learning it for a month or so. Im trying to learn how to use header files and i've runned into this problem. I get errors saying that x,y,z isnt decleared, and i know why i get them, but I dont know why it dosent work. Why does it say that x,y,z isnt deceared when I decleared them in the header file? Im really stuck at this one...


main.cpp:
#include <iostream>
#include "header.h"

using namespace std;

int main(){
    
    while(1){
        cout<<"X: "<<x<<endl<<"Y: "<<y<<endl<<"Z: "<<z<<endl; //The x,y,z decleared in header.h
    }
    
}



header.h
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <windows.h>

int example(int x, int y, int z=0){

    if(GetAsyncKeyState(VK_UP)){y++;}
    if(GetAsyncKeyState(VK_DOWN)){y--;}
    if(GetAsyncKeyState(VK_RIGHT)){x++;}
    if(GetAsyncKeyState(VK_LEFT)){x++;}
    if(GetAsyncKeyState(VK_TAB)){z=1;}
    if(GetAsyncKeyState(VK_SPACE)){z=2;}
    if(GetAsyncKeyState(VK_SHIFT)){z=3;}

    return(0);
}

#endif // HEADER_H_INCLUDED



Is This A Good Question/Topic? 0
  • +

Replies To: First attempt using header files, dosent work :/

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3535
  • View blog
  • Posts: 10,943
  • Joined: 05-May 12

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 12:14 AM

You declared x, y, and z as parameters to your example() function. You did not declare them as variables that main() can use.
Was This Post Helpful? 1
  • +
  • -

#3 sebwerner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-September 12

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 12:21 AM

Got it working by making the x,y,z variables global scope,
and put the "example();" in the main loop like this, Thank You Skydiver!

#include <iostream>
#include "header.h"

using namespace std;

int main(){
    
    while(1){
        example();
        cout<<"X: "<<x<<endl<<"Y: "<<y<<endl<<"Z: "<<z<<endl; //The x,y,z decleared in header.h
    }
    
}


Was This Post Helpful? 0
  • +
  • -

#4 prabh  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 3
  • View blog
  • Posts: 381
  • Joined: 27-December 08

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 12:28 AM

Look for comments i put.
Your main.cpp should look something like this
#include <iostream>
#include "header.h"
using namespace std;

int main()
{
// the correct way
    int x= 0;
    int y= 0;
    int z = 0;
    cout << "Hello world!" << endl;
    while(1){
// to update your x,y and z you should call your function
        example(x,x,z);
	        cout<<"X: "<<x<<endl<<"Y: "<<y<<endl<<"Z: "<<z<<endl; //The x,y,z decleared in header.h
	    }
    return 0;
}


header.h
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <windows.h>

int example( int x,int y,int z){

    if(GetAsyncKeyState(VK_UP)){y++;}
    if(GetAsyncKeyState(VK_DOWN)){y--;}
    if(GetAsyncKeyState(VK_RIGHT)){x++;}
    if(GetAsyncKeyState(VK_LEFT)){x++;}
    if(GetAsyncKeyState(VK_TAB)){z=1;}
    if(GetAsyncKeyState(VK_SPACE)){z=2;}
    if(GetAsyncKeyState(VK_SHIFT)){z=3;}

    return(0);
}

#endif // HEADER_H_INCLUDED




This code compiled, but thats a horrible way to use while loop.
You need to learn a lot buddy.
Was This Post Helpful? 0
  • +
  • -

#5 sebwerner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-September 12

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 12:36 AM

I tried to do it the way you showed me here, but now the values wont in/de-crement when i press the arrowkeys:
Was This Post Helpful? 0
  • +
  • -

#6 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 01:19 AM

prabh, you are passing arguments by value, so the changed value won't be reflected back in main.
Was This Post Helpful? 0
  • +
  • -

#7 sebwerner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-September 12

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 02:08 AM

I got it working, by doing what Skydiver siad :P
Was This Post Helpful? 0
  • +
  • -

#8 yothsoggoth  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 49
  • Joined: 19-June 12

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 02:51 AM

prabh's example should work pretty well, if you simply change line 5 as follows:

int example( int &x,int &y,int &z)


(Edit) Fixed tags

This post has been edited by yothsoggoth: 02 October 2012 - 02:51 AM

Was This Post Helpful? 1
  • +
  • -

#9 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 02:54 AM

Well, I know that. But, I just pointed out his mistake :)
Was This Post Helpful? 0
  • +
  • -

#10 joshua201  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 05-September 07

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 03:24 AM

Skydiver fixed it already
Was This Post Helpful? 0
  • +
  • -

#11 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 03:33 AM

What the hell are you talking about? I am talking about correcting prabh's mistake!
Was This Post Helpful? 0
  • +
  • -

#12 sebwerner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-September 12

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 03:49 AM

View Postyothsoggoth, on 02 October 2012 - 02:51 AM, said:

prabh's example should work pretty well, if you simply change line 5 as follows:

int example( int &x,int &y,int &z)


(Edit) Fixed tags


Why does it work if i put the "&" iin front of the variables? Im just wondering, after all im here to learn :)
Was This Post Helpful? 0
  • +
  • -

#13 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 06:17 AM

Well, the & means that you are passing the variables by reference.
Was This Post Helpful? 2
  • +
  • -

#14 jimblumberg  Icon User is online

  • member icon


Reputation: 4003
  • View blog
  • Posts: 12,359
  • Joined: 25-December 09

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 06:44 AM

Also note that you should not have function implementations inside header files, except when dealing with templates. C++ source should be placed in source files (.c, .cpp, etc) then added to your project not included using the #include statement.

Jim
Was This Post Helpful? 2
  • +
  • -

#15 sebwerner  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 30-September 12

Re: First attempt using header files, dosent work :/

Posted 02 October 2012 - 01:46 PM

View Postaresh, on 02 October 2012 - 06:17 AM, said:

Well, the & means that you are passing the variables by reference.


Thank you! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1