[c++] How to access nested message defined inside of another message?

47 views
Skip to first unread message

rkr...@gmail.com

unread,
Nov 28, 2016, 1:09:28 PM11/28/16
to grpc.io
I'm having a hard time figuring out how to access a nested message that's defined within a message (inline definition). However, I can easily access a nested message that's defined outside of the message.

What I mean is:

message outer
{
    global foo = 1;
    message inner
    {
        int64 j = 2;
    }
}

message global
{
    int64 k = 3;
}

Accessing the globally-defined message works fine:

std::cout << "k is " << request.foo().k() << "\n";

But I cannot access the internally-defined message via:

std::cout << "j is " << request.inner().j() << "\n";

I get a compiler message of

error: invalid use of 'helloworld::HelloRequest::inner'

I've even tried:

std::cout << "j is " << request.j() << "\n"

but of course there's no member "j" in "request".

I'm sure it's something silly that I'm doing :-) [It would be most useful to put this answer in https://developers.google.com/protocol-buffers/docs/reference/cpp-generated near the section with the text "A message can be declared inside another message. For example: message Foo { message Bar { } } " :-)

Thanks in advance!

Christian Rivasseau

unread,
Nov 28, 2016, 1:12:12 PM11/28/16
to rkr...@gmail.com, grpc.io

the inner message defines a class. if you want  a field of type inner you have a define that too


--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/cde3e5aa-b41c-477a-bf4c-a723a040c3fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

Christian Rivasseau
Co-founder and CTO @ Lefty
+33 6 67 35 26 74

rkr...@gmail.com

unread,
Nov 28, 2016, 1:16:23 PM11/28/16
to grpc.io, rkr...@gmail.com
<blush> yes -- that was it. Thank you!

For future readers:


message outer
{
    global foo = 1;
    message inner
    {
        int64 j = 2;
    }
    inner inside_message = 3;
}

message global
{
    int64 k = 3;
}

Reply all
Reply to author
Forward
0 new messages