• Advertisement

Archived

This topic is now archived and is closed to further replies.

Is std::vector can only take type, but not class?

This topic is 5028 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can std:vector take in class too? if i have a class like this:
class apple
{
public:
   BOOL  eaten;
   BOOL  IsEaten();
};
But if i have many "apples", then normally this could work (right?):
     
main()
{
  apple  apples[10];

}
But what if i don't know how many apples will there be? can't i use this:
      
main()
{
  vector< apple > apples;
}
The compiler is giving out error message saying it is not a type... Any way to solve it? ( or i'm just taking nonsence? ) Thanks for any helps [edited by - NewbieA on April 14, 2004 1:37:53 PM]

Share this post


Link to post
Share on other sites
Advertisement
Make sure you have included the vector header file, and qualified the namespace in someway, and that the defintion of the apple class is visible.


#include<vector>
#include"apple.h"

int main()
{
std::vector<apple> myapples;
}

Share this post


Link to post
Share on other sites
Yes, you can do that.

(BTW, you forgot the std namespace)

Should be:

std::vector apples;

or

using namespace std;
vector apples;

Also, you forgot the semi-colon after the class closing brace.

Are you sure that the vector can see your class, as in, is your class created before the function main? Your file should basically look like this:


#include <vector>

class apple
{
public:
BOOL eaten;
BOOL IsEaten(); //I hope this function is defined somewhere

};


int main()
{
std::vector< apple > apples;
return 0;
}


[edited by - MetaCipher on April 14, 2004 2:32:53 PM]

Share this post


Link to post
Share on other sites
Thanks for all the help.
Actually, i''m having another class in the middle. And the vector error thing happen in the middle.
I don''t know is that ok to post my code here. But at the moment, let;s say there is a class call "box" in the middle. And in that class there is vector< apple >, like this:

class box
{
vector< apple > apples;
};


And then the compiler return: "error C2327: ... : is not a type name, static, or enumerator"

Thanks

Share this post


Link to post
Share on other sites
Well we cannot tell what the error is with abstracts of code, but do what has been said and you should not get an error(include the vector header file and define the apple class before the definition of the box class)

Share this post


Link to post
Share on other sites
quote:
Original post by NewbieA
class box
{
vector< apple > apples;
};

And then the compiler return: "error C2327: ... : is not a type name, static, or enumerator"

Thanks


People are telling you:

#include <vector>
class box
{
std::vector< apple > apples;
};

Share this post


Link to post
Share on other sites
Ooops, found where the error come from. ( It turns out that i have two member with the same name from those two classes. Oh well, is middle of the night here... )

Thanks everyone for helping. Sorry for all the trouble...
( I think i will have a hot shower now, hopfully could wake myself up abit... )

Thanks

[edited by - NewbieA on April 14, 2004 2:20:09 PM]

Share this post


Link to post
Share on other sites

  • Advertisement