Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ pass inner structure as parameter

There is a structure TOut containing inner structure TIn:

template <typename T>
struct TOut
{
    struct TIn
    {
            bool b;
    };

    TIn in;
T t;
};

How to correctly pass TIn in as a formal parameter of some method?

class Test
{
public:
    template <typename T>
    static void test ( const TOut<T>::TIn &i) {} //Error
};


int main()
{
TOut <double> o;
Test::test(o.in);
}

The program compiles with the following error:

Error   4   error C2998: 'int test' : cannot be a template definition
like image 375
justik Avatar asked Jan 25 '12 20:01

justik


People also ask

Can you pass a struct as a parameter in C?

Structures and Functions in C : The C Programming allows us to pass the structures as the function parameters.

How do you pass a nested structure to a function?

A nested structure can be passed into the function in two ways: Pass the nested structure variable at once. Pass the nested structure members as an argument into the function.

Can a struct be a parameter?

Structs can be passed as parameters by reference or by value. The default behavior is to pass Struct parameters by reference in partner operations and entries, and to pass them by value in public operations, but it is possible to override this behavior when declaring the parameters.

Can we pass structure as a function argument?

Structures can be passed as function arguments like all other data types. We can pass individual members of a structure, an entire structure, or, a pointer to structure to a function. Like all other data types, a structure or a structure member or a pointer to a structure can be returned by a function.


1 Answers

You need to use the typename keyword:

template <typename T>
static void test ( const typename TOut<T>::TIn &i) {}

See Where and why do I have to put the "template" and "typename" keywords?

like image 149
Emile Cormier Avatar answered Oct 23 '22 05:10

Emile Cormier