Thread: Function that returns a class...

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    22

    Function that returns a class...

    So I am working on a software based 3d engine. I have a structure called triangle, and a class called object. The object class contains an array of triangles that make up that object, and position/orientation data.

    Anyway, I am writing a function called createCube, which is supposed to return an instance of object (or a pointer to an instance of object). The returned object will contain a bunch of triangles that together resemble a cube (duh). Anyway, Im having some difficulty writing this function. This is what I have, minus the irrelevant parts:

    Code:
    object createCube(int xpos, ..... )
    {
       triangle theTriangles[12];
       //.....
       //code to set triangles to form cube
       //......
    
       object theCube = new object(xpos, ypos, zpos,0,0,0);
       for (int i=0;i<12;i++)
       {
          theCube.addTriangle(theTriangles[i]);
       }
       return theCube;
    }
    This does not compile. It says "conversion from 'object*' to nonscalar type 'object' requested" on the line "object theCube= new obj...." So I added a couple astericks (*), changing it to:

    Code:
    object* createCube(int xpos, ..... )
    {
       triangle theTriangles[12];
       //.....
       //code to set triangles to form cube
       //......
    
       object* theCube = new object(xpos, ypos, zpos,0,0,0);
       for (int i=0;i<12;i++)
       {
          theCube.addTriangle(theTriangles[i]);
       }
       return theCube;
    }
    But now, it finds a problem with the line "theCube.addTriangle(....)" It says that addTriangle has not been declared (it has) and that their is a request for member of non-aggregate type before '(' token. I tried putting a '&' in front of that line, but it didn't help.

    I know the problem has something to do with pointers and reference and dereference operators or something like that. I know its sad that I can figure out 3d math, but not simple classes and pointers. Can anyone help me out? It would be greatly appreciated.

    Thanks!
    Last edited by cjmdjm; 02-20-2009 at 06:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Is it possible to have callback function as a class member?
    By Aidman in forum Windows Programming
    Replies: 11
    Last Post: 08-01-2003, 11:45 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM