header file question

Ok so i have a header file and i have this function in it:

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

void until(int from, int until)
{
    from;
    until;

    for(from; from < until; from++)
    {
        cout << from +1 << endl;
    }
}

#endif // UNTIL_H_INCLUDED 


Now it needs std::cout but i have been told you should never put namespace in a header file and such, so how can i do this? I am going to put until in a namespace also, not sure if that will affect anything.
You shouldn't be putting executable code in a header file.

However, if you're going to do that, then you need to qualify cout and endl with the std namespace.

 
    std::cout << from +1 << std::endl;


What's the purpose of lines 6 and 7? They don't do anything.

It's bad practice to name your arguments the same as your function (until).

Im making a namespace thoug so what should i put it in?
+1 @ AbstractionAnon


Plus, if your body is in a header file, the function must be marked as inline or else you'll get linker errors if you include in multiple files.

Also, you have some nonsense there....

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

inline void until(int from, int until) // <- mark as inline
{
//    from; <- these 2 lines do nothing, get rid of them
//    until;

    for(/*from*/; from < until; from++)  <- that first 'from' also does nothing
    {
        std::cout << from +1 << std::endl; // <- std:: prefixes
    }
}

#endif // UNTIL_H_INCLUDED  
Ok i fixed everything you guys suggested but what type of file should i put them in? updated code:

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

namespace userTools
{
    inline void until(int from, int until)
    {
        for(from; from < until; from++)
        {
            std::cout << from +1 << std::endl;
        }
    }
}

#endif // UNTIL_H_INCLUDED 



Main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "until.h"

using namespace std;
using namespace userTools;

/*Until Keyword
//
//The until function will loop through and output a
//vector or array from the first element all the way
//to the last element. If used with an integer it
//will output all the numbers from 0 to the last number.
//or it will start at the number specified.
*/

int main()
{
    int numbers = 50;

    until(0, numbers);
}
Last edited on
Ok now that the integer until loop is working i wanted to do it with an array but the problem is that if i have an array like this

 
int array_numbers[6] = {3,6,1,2,9,15};


the until function outputs this:

4 5 6 7 8 9 10 11 12 13 14 15

its supposed to output the array elements and it doesnt:

main

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include "until.h"

using namespace std;
using namespace userTools;

/*Until Keyword
//
//The until keyword will loop through and output a
//vector or array from the first element all the way
//to the last element. If used with an integer it
//will output all the numbers from 0 to the last number.
*/

int main()
{
    int numbers = 200;
    int array_numbers[6] = {3,6,1,2,9,15};

    //int_until(50, numbers);
    array_until(array_numbers[0], array_numbers[5]);
}


until.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef UNTIL_H_INCLUDED
#define UNTIL_H_INCLUDED

namespace userTools
{
    inline void int_until(float int_startVar, float int_endVar)
    {
        for(int_startVar; int_startVar < int_endVar; int_startVar++)
        {
            std::cout << int_startVar + 1 << std::endl;
        }
    }

    inline void array_until(float array_startVar, float array_endVar)
    {
        for(array_startVar; array_startVar < array_endVar; array_startVar++)
        {
            std::cout << array_startVar + 1 << std::endl;
        }
    }
}

#endif // UNTIL_H_INCLUDED 
bump
the until function outputs this:
yes, the functions are designed to that.

if you want to output the array element you have to pass the array and the size of the array
Or you can pass pointers to the beginning and end of the array (see below)

Notes:
1. I prefer int* to int[] as it makes it clearer (to me) that array params decay to a pointer. (It's also the form I encounter in API calls the most.)
2. In the begin/end pointer case I am following the C++ standard library approach of an inclusive start pointer and an exclusive (one past the end) end pointer.

Andy

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>

// in #include "until.h"

namespace userTools
{
    inline void array_until_ptr_size(const int* pdata, size_t count)
    {
        for(size_t i = 0; i < count; ++i)
        {
            std::cout << " " << pdata[i];
        }
    }

    inline void array_until_ptr_ptr(const int* pbegin, const int* pend)
    {
        for( ; pbegin < pend; ++pbegin)
        {
            std::cout << " " << *pbegin;
        }
    }
}

using namespace std;
using namespace userTools;

/*Until Keyword
//
//The until keyword will loop through and output a
//vector or array from the first element all the way
//to the last element. If used with an integer it
//will output all the numbers from 0 to the last number.
*/

int main()
{
    int array_numbers[6] = {3, 6, 1, 2, 9, 15};
    const size_t size = sizeof(array_numbers) / sizeof(array_numbers[0]);

    cout << "array_until_ptr_size =";
    array_until_ptr_size(array_numbers, size);
    cout << endl;

    cout << "array_until_ptr_ptr  =";
    array_until_ptr_ptr(array_numbers, array_numbers + size);
    cout << endl;

    cout << endl;

    cout << "array_until_ptr_size:" << endl;
    for(int i = 0, s = (int)size; 0 < s; ++i, s -= 2)
    {
        cout << "i=" << i << ",s=" << s << " = ";
        array_until_ptr_size(&array_numbers[i], s);
        cout << endl;
    }

    cout << endl;

    cout << "array_until_ptr_ptr:" << endl;
    for(const int *b = array_numbers, *e = (b + size); b < e; ++b, --e)
    {
        cout << "b=" << b << ",e=" << e << " = ";
        array_until_ptr_ptr(b, e);
        cout << endl;
    }

    cout << endl;

    return 0;
}

Last edited on
Well I want to just be able to type array_until(value1, value2);

all your code completley defeats the purpose of what the function is meant to do. Which is make it simple to loop through values. I dont want to have to write all that out every time i want to use the until function.
what the function is meant to do. Which is make it simple to loop through values.


I'm a little confused as to the goal here. Are you're trying to recreate the for loop?
I am not sure what your value1 and value2 are

For array

int data[] = {1, 2, 3};

you need to use either

array_until(data, 3); // with the function that takes a char* and an int

or

array_until(data, data + 3); // with the function that takes char*s for the begin and end

The key lines in my code above are 41 and 45; the later ones are just extra tests.

Andy

@Ch1156


If I have understood you mean the following function

1
2
3
4
5
6
7
void array_until( const int *a, int from, int until )
{
   for ( ; from < until; from++ )
   {
      std::cout << a[from] << std::endl;
   }
}
Last edited on
Topic archived. No new replies allowed.