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

This is a discussion on creating a function with return value of an array with 3 dimensions within the C++ Programming forums, part of the General Programming Boards category; Dear C++ programmers, I want to make a function that has a return value array with three dimensions. I tried ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,935
    Declare the struct pt outside of the function, then have the function return a vector<pt>
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    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 10: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, 06:15 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21