help

This is a discussion on help within the C++ Programming forums, part of the General Programming Boards category; ok i have been programming for a while, but started learning c++ a while ago. i am working on a ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    26

    help

    ok i have been programming for a while, but started learning c++ a while ago. i am working on a graphical snake game and have a lil prob. first i will show an example so you see my prob. this is just an ex, im not that dumb

    function1
    {
    function2();
    }

    function2
    {
    function1();
    }

    main
    {
    function1();
    }

    any that is what my code is like, w/ more functions
    so it says that function 2 is an undeclared identifier.
    do they have to be in the rt order or what. could i put my functions into a seperate file. how do i fix this prob.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >so it says that function 2 is an undeclared identifier.
    Create prototypes for your functions and it will work correctly.
    Code:
    function1();
    function2();
    
    main 
    { 
    function1(); 
    }
    
    function1 
    { 
    function2(); 
    } 
    
    function2 
    { 
    function1(); 
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    26
    thanks for the help

  4. #4
    Registered User Sekti's Avatar
    Join Date
    Feb 2002
    Posts
    163

    putting code in other files...

    Code:
    //bass.h
    
    #ifndef BASS_H 
    #define BASS_H
    
    void bass1 ();
    void bass2 ();
    
    #endif
    
    -----------------------------------------------------
    
    //bass01.cpp
    
    #include <iostream.h>
    #include "bass.h"
    
    void bass1()
    {
    cout<<"Going to bass 2"<<endl;
    bass2();
    }
    
    void bass2()
    {
    cout<<"Going back to bass 1"<<endl;
    bass1();
    }
    
    -----------------------------------------------------
    
    //main.cpp
    
    #include <iosteream.h>
    #include "bass.h"
    
    int main()
    {
    bass1();
    return 0;
    }
    The first file bass.h is the header file containing the prototype declarations of the functions. The second file bass01.cpp contains the code for bass1() and bass2() and has bass.h included in " "s because " " means the header file is in the same directory as the file its included in. Then in main.cpp it just calls bass1() like it was in the same file. Now just include bass01.cpp and main.cpp in your project in your compiler and bam.
    +++
    ++
    + Sekti
    ++
    +++

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21