Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

vector : wat wrong?

Posted on 2004-11-07
28
Medium Priority
?
397 Views
Last Modified: 2013-12-14
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];


}
0
Comment
Question by:tom_mk
  • 12
  • 10
  • 2
  • +1
25 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 12520517
'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
 
LVL 86

Expert Comment

by:jkr
ID: 12520523
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
 

Author Comment

by:tom_mk
ID: 12520536
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tom_mk
ID: 12520541
oh,, i solved it

thx
0
 
LVL 86

Expert Comment

by:jkr
ID: 12520558
#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
 
LVL 4

Accepted Solution

by:
pankajtiwary earned 672 total points
ID: 12520570
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
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12520572
tom_mk,

That was pretty fast :-)
0
 
LVL 86

Expert Comment

by:jkr
ID: 12520581
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
 

Author Comment

by:tom_mk
ID: 12520637
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
 
LVL 86

Expert Comment

by:jkr
ID: 12520645
>>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
 

Author Comment

by:tom_mk
ID: 12520667
but i believe that vector can store object , isn't it?
0
 

Author Comment

by:tom_mk
ID: 12520681
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
 
LVL 86

Expert Comment

by:jkr
ID: 12520691
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
 
LVL 86

Expert Comment

by:jkr
ID: 12520698
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
 

Author Comment

by:tom_mk
ID: 12520706
then ..

if i insert COL into 1st position

how can i chekc that 1st position is COL?

thx
TOm
0
 
LVL 86

Expert Comment

by:jkr
ID: 12520738
>>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
 
LVL 86

Expert Comment

by:jkr
ID: 12520740
Argh,

obj& tmp = myvec.font();

should have read

obj& tmp = myvec.front(); // "front", not "font"
0
 

Author Comment

by:tom_mk
ID: 12520765
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
 

Author Comment

by:tom_mk
ID: 12520771
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
 
LVL 86

Expert Comment

by:jkr
ID: 12520779
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
 
LVL 86

Expert Comment

by:jkr
ID: 12520793
>> 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
 

Author Comment

by:tom_mk
ID: 12520796
very obvious mistake,

i should have figured that one out myself
lol
0
 

Author Comment

by:tom_mk
ID: 12520797
thx man
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 664 total points
ID: 12520809
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
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 664 total points
ID: 12522220
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

578 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