Custom function in header issue

Test.h
1
2
3
4
5
6
7
8
#ifndef _TEST_H
#define _TEST_H
class Test
{
public:
    void test();
};
#endif // _TEST_H 

Test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#ifndef _TEST_H
    #include "Test.h"
#endif

using namespace std;

int test()
{
    cout << "Howdy, Human!" << endl;
    return 0;
}
int main(int argc, char* argv[])
{
    cout << "HELLO I LIKE CHEESECAKE" << endl;
    return 0;
}

Output:

HELLO I LIKE CHEESECAKE

Does any one know how I can output my custom function?
Should your function test be part of a class? It doesn't need to be:

Test.h
1
2
3
4
5
6
#ifndef TEST_H
#define TEST_H

void test();

#endif // TEST_H 


Test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>

using namespace std;

// NOTE: This function returns void to match the declaration `void test();' in Test.h
void test()
{
    cout << "Howdy, Human!" << endl;
    return 0;
}
int main(int argc, char* argv[])
{
    cout << "HELLO I LIKE CHEESECAKE" << endl;
    test(); 
    return 0;
}


There's (generally) no need to conditionally include your own header files: this is taken care of by the # ifndef in the header file itself.
1
2
3
4
// Remove me!
/* #ifndef _TEST_H
    #include "Test.h"
#endif */


Also, names beginning with a underscore followed by a capital letter (e.g., _TEST_H) are reserved; they can't be used in your program. I prefer a conservatively named header guard:
#ifndef PROJECTNAME_HEADER_H_INCLUDED
But you might find this overkill and just use HEADERNAME_H (e.g. TEST_H) like a lot of folks do.
Ok thank you I will try that :)
Topic archived. No new replies allowed.