expected...

i have a prob here.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include "FGE/fge.h"

int main ( int argc, char** argv )
{
    int * obj;
    fge::fge_New(obj, int, 14);

    return 0;
}


fge.h
1
2
3
static void fge_BailOut(std::string message);
template <class T>
static void fge_New(T * target, T type, int amount);


fge.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
void fge::fge_BailOut(std::string message)
{
    std::cout << message.c_str() << std::endl;
}

template <class T>
void fge::fge_New(T * target, T type, int amount)
{
    (target) = (type *) new ((amount + 1) * sizeof(type));
    if(!target)
        fge_BailOut("could not allocate that much ram");
    target = NULL;
}


and the error compiler gives
C:\Users\Ceset\Desktop\test\FGE\FGE\fge.cpp|28|error: expected primary-expression before ')' token|
C:\Users\Ceset\Desktop\test\FGE\FGE\fge.cpp|28|error: expected ';' before 'new'|
||=== Build finished: 2 errors, 0 warnings (0 minutes, 0 seconds) ===|


i cant fix it what could be the problem??

thx everyone in advance
I do not understand the following in your code.

1. type is a variable of type T. I haven't ever tried to pass a 'type' like 'int' to a function.

2. Moreover, with what type specification 'T' are you calling the template class?

3. target is a pointer to type T but you are making it point to a variable of type 'type'.

EDIT: Your function prototype is contradictory. The variable type cannot be of type T if it is supposed to be an int or a float itself. IMO you should remove this parameter. Combining keskiverto's points,

1
2
3
4
5
6
7
8
template <class T>
void fge::fge_New(T * &target, int amount) // target is reference to pointer.
{
    target = new T [amount + 1]; // target is pointing to data of type T.
    if(!target)
        fge_BailOut("could not allocate that much ram");
    target = NULL;
}
Last edited on
@Ceset: Your syntax for new is not ok. See:
http://www.cplusplus.com/doc/tutorial/dynamic/

'target' is a local variable within fge_New. It is initialized with a copy of the value hold by the pointer parameter that the function is called with. Call to fge_New does not modify 'obj' of main().
Last edited on
dynamic memory and pointers are still hard for me i was trying to make examples to get a better understanding i tried to fix it as much as i can but still seems i have problems

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifdef __cplusplus
    #include <cstdlib>
#else
    #include <stdlib.h>
#endif

#include "FGE/fge.h"

int main ( int argc, char** argv )
{
    int * obj;
    fge::fge_New(obj, 14);

    return 0;
}


1
2
template <class T>
static void fge_New(T * target, int amount);


1
2
3
4
5
6
7
8
template <class T>
void fge::fge_New(T * target, int amount)
{
    target = new (std::nothrow)T * ((amount + 1) * sizeof(T));
    if(!target)
        fge_BailOut("could not allocate that much ram");
    target = NULL;
}


and the new error i get is
obj\Debug\main.o||In function `SDL_main':|
C:\Users\Ceset\Desktop\test\main.cpp|12|undefined reference to `void fge::fge_New<int>(int*, int)'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
Last edited on
I'm not sure about your new error. I can suggest the following:

1. Specify template specification while calling the function:
fge::fge_New<int>(obj, 14);

2. Clean your build directory and re-build.
you cannot implement a template function like other function. template functions must be entirely visible to the compiler when instantiate (called). In other words: move the function to the header file.

This is the correct way to write it:
1
2
3
4
5
6
7
8
template <class T>
void fge::fge_New(T * target, int amount)
{
    target = new (std::nothrow)T[amount + 1];
    if(!target)
        fge_BailOut("could not allocate that much ram");
    target = NULL;
}
all you do is wasting memory though

obj in main is not modified. why do you set target to null after allocating?

See:
http://www.cplusplus.com/reference/new/operator%20new%5B%5D/
thx all for your helps.

fixed one

fge.h
1
2
3
4
5
6
7
8
        template <class T>
        static void fge_New(T * target, int amount)
        {
            target = new (std::nothrow)T [amount + 1];
            if(!target)
                fge_BailOut("could not allocate that much ram");
            target = NULL;
        }


but even if i use a very huge number such as
10000000000000000000000000000000...
it doesnt seem like it is giving error any ideas or it is ok. even debugger seems ok
i dont think my computer is that strong
Last edited on
Type int cannot store so large numbers, so the value of 'amount' is not that big.

Besides, you still toss away the memory.
tossing away??

it was just an example i was working on if you mean it is useless. if you mean you are doing it wrong can you tell me why?
Because target does not actually change.
It does in the function's scope, but it will not change outside.
Use a reference-to-pointer type:
1
2
3
template <class T>
static void fge_New(T* &target, int amount)
/* ... */

Or a pointer-to-pointer type:
1
2
3
4
5
6
7
8
template <class T>
static void fge_New(T** target, int amount)
{
    *target = // ...
    if(/* ... */)
        // ...
    *target = NULL;
}

and call it like
fge_New(&mytarget,1);
thx @EssGeEich and everyone
Topic archived. No new replies allowed.