Solved

how to return a struct

Posted on 2004-08-29
11
210 Views
Last Modified: 2010-04-01
I'm having difficutly returning a struct from a function

say I defined my struct as:

struct foo{
int index[8][2];
};

and my function is:

foo a::fxnToDoSomething()
{
      return foo;
}

it complains... what am I doing wrong?

Or maybe you can suggest something better.  What I'm trying to do is return that 8x2 array to another function so that it can examine those values.  That struct is currently in my private section of my class declarations.

thanks
0
Comment
Question by:CBMLude
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
11 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 50 total points
ID: 11924868
foo a::fxnToDoSomething()
{
      foo f;
      return f;
}
0
 

Author Comment

by:CBMLude
ID: 11924874
I get one of these errors:

dice.cpp(50): error C2143: syntax error : missing ';' before 'dice::getTestIndex'

struct testIndex
      {
            int testIndices[8][2];  
      };

testIndex dice::getTestIndex()
{
      testIndex *testPositions = new testIndex();
      return testPositions;
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 11925205
You're returing a pointer to a struct.
You need the following return type.

testIndex* dice::getTestIndex()
{
    testIndex *testPositions = new testIndex();
    return testPositions;
}
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 11926015
As an alternative, you can pass it by reference. Don't have to return, just use it directly:

void a::fxnToDoSomething(foo &myStruct)
{
      foo.Member1 = anyvalue;
      foo.Method1(something);
}
0
 
LVL 4

Expert Comment

by:anthony_w
ID: 11951523
Or dereference the pointer:

testIndex dice::getTestIndex()
{
     testIndex *testPositions = new testIndex();
     return *testPositions;
}

Be aware that this copies the return value, whereas if you return by pointer, as Axter suggests, then the point refers to the original.
0
 
LVL 30

Expert Comment

by:Axter
ID: 11951933
FYI:
The above code has a memory leak.

I don't recommend returning by value.  It's not efficient.
0
 
LVL 4

Expert Comment

by:anthony_w
ID: 11961557
> The above code has a memory leak.

Sorry yes, that's what you get for not reading code that you've cut and pasted. I read it as calling a funcion that returned a pointer, rather than creating one with new. Equivalent code for returning by value:

testIndex dice::getTestIndex()
{
     return testIndex();
}

However, I guess in most cases you want to do more than just construct the object.

As for returning by value not being efficient, that depends. In the code above, a good compiler will omit the copy, and just construct the object in the return value. The same optimization can also be done with a named object:

testIndex dice::getTestIndex()
{
     testIndex someVar;
     //do stuff
     return someVar;
}

In any case, I would rather focus on getting code correct rather than guessing what needs to be efficient, and then profile if things are too slow
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12328014
Have provided a valid alternative
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question