Thread: creating a function with return value of an array with 3 dimensions

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    creating a function with return value of an array with 3 dimensions

    Dear C++ programmers,
    I want to make a function that has a return value array with three dimensions.
    I tried first
    Code:
    vector <struct>  look(....)
    {
    ....
    }
    to create a function that will return vector of a structure that has 3 elements., but I got a compilation error C2143 saying that ";" is missing. I don't know how many elements will be in vector but I know that each element should have 3 values. I would put an array as a return value but how to fill out array dinamically?
    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Post more of what you tried.

    Posting an error message about a missing ; then posting code which has no ; at all doesn't help us figure out what the problem is.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    50
    Quote Originally Posted by Salem
    Post more of what you tried.

    Posting an error message about a missing ; then posting code which has no ; at all doesn't help us figure out what the problem is.
    I apologize, I simply wanted to focus on the part where the error lies. Again I had to cut a code because it is huge I just left most important parts.
    Code:
    
    vector<struct> look(double d, int xw, int yh, double dh)
    {
         struct pt
        {
            int xc;
            int yc;
            double h;
        };
       vector <pt> v;
        pt p;
    ...
    for ( )
    {
    .....
    v.push_back(pt);
    }
    }
    I have problem that I cannot define return value of the function look as vector<struct>(error C2332 'struct' missing tag name). Neither can I defined as vector<pt> because I did not declare it. The problem is in the fist line.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Declare the struct pt outside of the function, then have the function return a vector<pt>
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    struct is a C++ keyword, it is not a type. Vectors must specify the type of the thing they hold. What type of things is your vector going to hold?

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    50

    vector holding structure

    My vector is to hold structure containing three elements (int, int, double).

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > Again I had to cut a code because it is huge I just left most important parts.
    So rather than trying to make it work in your large project, create another small project (typically, just one file) and copy across the essence of the problem.

    As well as being quicker to compile, test and run, it also offers the opportunity for you to see what the problem is anyway. If you don't, it means you have a nice simple example to post to the board.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    My vector is to hold structure containing three elements (int, int, double).
    When you create a struct, you create a new type--just like int, double, float, etc. The new type is the name of the struct. Your vector has to declare what type of things it will contain. 'struct' is not a type.
    Last edited by 7stud; 01-23-2007 at 11:17 AM.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Do as Lasarlight suggested.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM