vector : wat wrong?

this is wat i have,

how can i get the val of the first array that i insert out.

thx
TOm

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "vector"
using namespace std;

struct vertex
{
      float pt[3];
};


void main(int argc, char ** argv)
{
      vertex v;

      std::vector<vertex> myVec;
      
      v.pt[0] = 3;
      v.pt[1] = 3;
      v.pt[2] = 3;

      myVec.push_back(v);

      float temp[3];
      
      temp = myVec.end()
      
      cout << "front : " << temp[0];


}
tom_mkAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
'myVec.end()' will return a 'pseudo iterator' that os special for indicating the end of the vector - you should use

   vertex v;

    std::vector<vertex> myVec;
   
     v.pt[0] = 3;
    v.pt[1] = 3;
    v.pt[2] = 3;

    myVec.push_back(v);

    vertex temp
   
     temp = myVec.back();

This member function returns a reference to the last element of the vector.

0
jkrCommented:
Ooops, correction, that should be

    std::vector<vertex> myVec;
   
    v.pt[0] = 3;
   v.pt[1] = 3;
   v.pt[2] = 3;

   myVec.push_back(v);

   vertex temp; // missed the ';'
   
    temp = myVec.back();

   cout << "front : " << temp.pt[0];
0
tom_mkAuthor Commented:
void main(int argc, char ** argv)
{
      vertex v;

      std::vector<vertex> myVec;
      
      v.pt[0] = 3;
      v.pt[1] = 3;
      v.pt[2] = 3;

      myVec.push_back(v);

      vertex temp;
      
      temp = myVec.front();
      
      cout << "front : " << temp[0];


}




--------------------Configuration: array - Win32 Debug--------------------
Compiling...
array.cpp
C:\array.cpp(29) : error C2676: binary '[' : 'struct vertex' does not define this operator or a conversion to a type acceptable to the predefined operator
Error executing cl.exe.

array.obj - 1 error(s), 0 warning(s)
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

tom_mkAuthor Commented:
oh,, i solved it

thx
0
jkrCommented:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "vector"
using namespace std;

struct vertex
{
    float pt[3];
};


void main(int argc, char ** argv)
{

  std::vector<vertex> myVec;

  vertex v;
 
  v.pt[0] = 3;
  v.pt[1] = 3;
  v.pt[2] = 3;

  myVec.push_back(v);

  vertex temp; // missed the ';'
 
    temp = myVec.back();

  cout << "front : " << temp.pt[0];

}

compile just fine here...

As I wrote, 'temp[0];' should be 'temp.pt[0];'
0
pankajtiwaryCommented:
Hi tom_mk,

Little correction. temp[0] in incorrect because temp is not an array but a structure. I think you meant temp.pt[0]

Correct the above and it will run perfectly fine.

One important suggestion. 'void main()' is wrong as the standard specifies that main returns an int. Returning anything from main invokes undefined behaviour which can result in anything.

Since main returns an int, insert a 'return 0;' just before the closing brace. The final program will be something like this.

#include <iostream>
#include <vector>

using namespace std;

struct vertex
{
     float pt[3];
};

int main(int argc, char ** argv)
{
     vertex v;

     std::vector<vertex> myVec;

     v.pt[0] = 3;
     v.pt[1] = 3;
     v.pt[2] = 3;

     myVec.push_back(v);

     vertex temp;

     temp = myVec.front();

     cout << "front : " << temp.pt[0];

     return 0;
}


Cheers!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pankajtiwaryCommented:
tom_mk,

That was pretty fast :-)
0
jkrCommented:
BTW, for further reference, you might want to check out http://cplus.about.com/library/weekly/aa050102e.htm ("C++ Tutorial - Lesson 10: Arrays and Vectors")
0
tom_mkAuthor Commented:
i think i still have another problem

says in one vector,
i insert many kinds of object
i.e.

struct vertex
{
    float pt[3];
      
};

struct color
{
      float col[3];
};

my q is , how can i check that what is the obj in any particualr position?
e.g how can i know that the 2nd position is color or vertex?

thx
TOm
0
jkrCommented:
>>my q is , how can i check that what is the obj in any particualr position?

Err, you cannot store both 'color' and 'vertex' in one vector, tha's not gonna work. You'll need one separate vector for both of them.
0
tom_mkAuthor Commented:
but i believe that vector can store object , isn't it?
0
tom_mkAuthor Commented:
this is wat i doing
is this the right approach?

struct obj
{
    float pt[3];
      float col[3];
      
};


obj myObj;
obj myT;

std::vector<obj> myvec;

void myVertex(float x, float y, float z)
{
      // myv
      // myvtemp

      myObj.pt[0] = x;
      myObj.pt[1] = y;
      myObj.pt[2] = z;

      // v.push_back(i);
      myvec.push_back(myObj);

}

void myColor(float r, float g, float b)
{
      myObj.col[0] = r;
      myObj.col[1] = g;
      myObj.col[2] = b;

      myvec.push_back(myObj);

}
0
jkrCommented:
Oh, OK, that's different and will work. What you could do is

struct obj {

enum type  { POINT, COL};

type objType;

union
{
   float pt[3];
   float col[3];
 }  
};

void myVertex(float x, float y, float z)
{
    // myv
    obj MyObj;

    myObj.pt[0] = x;
    myObj.pt[1] = y;
    myObj.pt[2] = z;
    myObj.type = obj::POINT;

    // v.push_back(i);
    myvec.push_back(myObj);

}

void myColor(float r, float g, float b)
{

    obj MyObj;

    myObj.col[0] = r;
    myObj.col[1] = g;
    myObj.col[2] = b;
    myObj.type = obj::COL;

    myvec.push_back(myObj);

}

and evaluate obj::type when processing these structs.
0
jkrCommented:
BTW, the 'union' is just to save storage space, you could also use

struct obj {

enum type  { POINT, COL};

type objType;

  float pt[3];
  float col[3];

};

BTW, in my last post, I forgot a ';', that should have been

struct obj {

enum type  { POINT, COL};

type objType;

union
{
  float pt[3];
  float col[3];
} ; // <---
};
0
tom_mkAuthor Commented:
then ..

if i insert COL into 1st position

how can i chekc that 1st position is COL?

thx
TOm
0
jkrCommented:
>>if i insert COL into 1st position
>>how can i chekc that 1st position is COL?

By using

std::vector<obj> myvec;

//...

obj& tmp = myvec.font();

if ( tmp.type == obj::COL) {

  // is 'color'

}
0
jkrCommented:
Argh,

obj& tmp = myvec.font();

should have read

obj& tmp = myvec.front(); // "front", not "font"
0
tom_mkAuthor Commented:
void myVertex(float x, float y, float z)
{
      obj myObj;

      myObj.pt[0] = x;
      myObj.pt[1] = y;
      myObj.pt[2] = z;
      
      myObj.type = obj::POINT;

      myvec.push_back(myObj);

}


void myColor(float r, float g, float b)
{
      obj myObj;

      myObj.col[0] = r;
      myObj.col[1] = g;
      myObj.col[2] = b;

      myObj.type = obj::COL;

      myvec.push_back(myObj);

}

--------------------Configuration: mygl - Win32 Debug--------------------
Compiling...
mygl.cpp
C:\Documents and Settings\tooa\Desktop\course\314\assn2\mygl.cpp(138) : error C2274: 'function-style cast' : illegal as right side of '.' operator
C:\Documents and Settings\tooa\Desktop\course\314\assn2\mygl.cpp(138) : error C2039: 'POINT' : is not a member of 'obj'
        C:\Documents and Settings\tooa\Desktop\course\314\assn2\mygl.cpp(39) : see declaration of 'obj'
C:\Documents and Settings\tooa\Desktop\course\314\assn2\mygl.cpp(156) : error C2274: 'function-style cast' : illegal as right side of '.' operator
Error executing cl.exe.

mygl.obj - 3 error(s), 0 warning(s)
0
tom_mkAuthor Commented:
my prior post has s.t incorrect

this is the correct one

void myVertex(float x, float y, float z)
{
      obj myObj;

      myObj.pt[0] = x;
      myObj.pt[1] = y;
      myObj.pt[2] = z;
      
      myObj.type = obj::PT;  // <--- problem

      myvec.push_back(myObj);

}

//////////////////////////////////////////////////////
// FUNC:  myColor(r,g,b)
//////////////////////////////////////////////////////

void myColor(float r, float g, float b)
{
      obj myObj;

      myObj.col[0] = r;
      myObj.col[1] = g;
      myObj.col[2] = b;

      myObj.type = obj::COL;  // <---- PROB

      myvec.push_back(myObj);

}

--------------------Configuration: mygl - Win32 Debug--------------------
Compiling...
mygl.cpp
C:\Documents and Settings\tooa\Desktop\course\314\assn2\mygl.cpp(138) : error C2274: 'function-style cast' : illegal as right side of '.' operator
C:\Documents and Settings\tooa\Desktop\course\314\assn2\mygl.cpp(156) : error C2274: 'function-style cast' : illegal as right side of '.' operator
Error executing cl.exe.

mygl.obj - 2 error(s), 0 warning(s)
0
jkrCommented:
Sorry, my fault again (case and...), the following compiles fine:

struct obj {

enum type  { POINT, COL};

type objtype;

union
{
  float pt[3];
  float col[3];
};  
};

vector<obj> myvec;

void myVertex(float x, float y, float z)
{
    obj myObj;

    myObj.pt[0] = x;
    myObj.pt[1] = y;
    myObj.pt[2] = z;
   
     myObj.objtype = obj::POINT;

    myvec.push_back(myObj);

}


void myColor(float r, float g, float b)
{
    obj myObj;

    myObj.col[0] = r;
    myObj.col[1] = g;
    myObj.col[2] = b;

    myObj.objtype = obj::COL;

    myvec.push_back(myObj);

}
0
jkrCommented:
>> my prior post has s.t incorrect

OK, according to that it should be

struct obj {

enum type  { POINT, COL};

type objtype;

union
{
 float pt[3];
 float col[3];
};  
};

vector<obj> myvec; // this one needs to be declared somewhere

void myVertex(float x, float y, float z)
{
    obj myObj;

    myObj.pt[0] = x;
    myObj.pt[1] = y;
    myObj.pt[2] = z;
   
     myObj.objtype = obj::PT;  // <--- problem

    myvec.push_back(myObj);

}

//////////////////////////////////////////////////////
// FUNC:  myColor(r,g,b)
//////////////////////////////////////////////////////

void myColor(float r, float g, float b)
{
    obj myObj;

    myObj.col[0] = r;
    myObj.col[1] = g;
    myObj.col[2] = b;

    myObj.objtype = obj::COL;  // <---- PROB

    myvec.push_back(myObj);

}
0
tom_mkAuthor Commented:
very obvious mistake,

i should have figured that one out myself
lol
0
tom_mkAuthor Commented:
thx man
0
jkrCommented:
Oh, and then the test should of course be

std::vector<obj> myvec;

//...

obj& tmp = myvec.front();

if ( tmp.objtype == obj::COL) {

 // is 'color'

}
0
itsmeandnobodyelseCommented:
I made a different approach using derived classes:

#include <iostream>
#include <vector>
using namespace std;
           
 
class Obj
{
public:
    Obj() {}
    virtual ~Obj() { }
    virtual bool isPoint() { return false; }
    virtual bool isColor() { return false; }
    virtual ostream& print(ostream& os) const { return os; }
    friend ostream& operator << (ostream& os, const Obj& ob)
    {
        return ob.print(os);
    }
};

class PtObj : public Obj
{
    float pt[3];
public:

    PtObj(float x, float y, float z) { pt[0] = x; pt[1] = y; pt[2] = z; }
    virtual bool isPoint() { return true; }
    virtual ostream& print(ostream& os) const { os << (*this); return os; }
    friend ostream& operator << (ostream& os, const PtObj& pt)
    {
        os << "POINT(" << pt.pt[0] << ","   << pt.pt[1] << ","   << pt.pt[2] << ") ";
        return os;
    }
};

class ColObj : public Obj
{
    float col[3];
public:

    ColObj(float r, float g, float b) { col[0] = r; col[1] = g; col[2] = b; }
    virtual bool isColor() { return true; }
    virtual ostream& print(ostream& os) const { os << (*this); return os; }
    friend ostream& operator << (ostream& os, const ColObj& col)
    {
        os << "COLREF(" << (int)col.col[0] << ","  
           << (int)col.col[1] << ","   << (int)col.col[2] << ") ";
        return os;
    }
};

 
int main(int argc, char ** argv)
{
     std::vector<Obj*> myVec;
     
     PtObj*  ppt  = new PtObj(3, 3, 3);
     myVec.push_back(ppt);
     ColObj* pcol = new ColObj(255, 128, 128);
     myVec.push_back(pcol);

     cout << (*myVec[0]) << (*myVec[1]) << endl;

     for (int i = 0; i < myVec.size(); ++i)
         delete myVec[i];

     return 0;
}

Derived objects are type-safe while unions are not. So you couldn't change a PtObj to a ColObj.

Regards, Alex
 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.