Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7015831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:42:15+00:00 2026-05-27T22:42:15+00:00

I’m new to using vectors…I just started studying them today. I’m trying to create

  • 0

I’m new to using vectors…I just started studying them today. I’m trying to create a vector for my enemy class so I can dynamically create them, but I can’t seem to figure out how to use the class functions…

Here is the class…

class Enemy
{
    private:
        SDL_Surface *enemy;
    public:
        Enemy();
       ~Enemy();

        enum Direction { UP, DOWN, LEFT, RIGHT } direction;

        SDL_Rect position;  
        bool alive;

        int setAttributes(int x, int y, int w, int h);
        void update();
        bool checkCollision();
};

Then I want to create a vector for the enemies in the game…

vector<Enemy> enemy;

Then I try to call the class functions like this…

enemy->setAttributes(0, 50, 32, 32);
enemy->update();

But when I compile, I get the following errors…

~/code/cpp/sdl$ g++ -o name main.cpp game.cpp player.cpp bullet.cpp enemy.cpp level.cpp -lSDL -lSDL_image -lSDL_ttf -lSDL_mixer
level.cpp: In constructor ‘Level::Level()’:
level.cpp:14: error: base operand of ‘->’ has non-pointer type ‘std::vector<Enemy, std::allocator<Enemy> >’
level.cpp: In member function ‘void Level::update()’:
level.cpp:28: error: base operand of ‘->’ has non-pointer type ‘std::vector<Enemy, std::allocator<Enemy> >’

What am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:42:15+00:00Added an answer on May 27, 2026 at 10:42 pm

    A vector is not a pointer (and does not act like one), and hence does not have an operator-> defined. Using a vector of Enemys is just like using an array of them with the subscript operator:

    std::vector<Enemy> enemies;
    enemies.push_back(Enemy());
    enemies[0].update();
    

    The reason you can use operator-> on arrays is that arrays convert to pointers, and thus

    Enemy enemies[5];
    enemies->update();
    

    is equivalent to

    Enemy enemies[5];
    (*enemies).update();
    

    which is in turn equivalent to

    Enemy enemies[5];
    enemies[0].update();
    

    However, just because this is possible does not mean it is a good idea: people will have much less trouble understanding your code if you write enemies[0].update() instead of enemies->update().

    If you want to go through a vector<Enemy> as you could with a pointer to an array, you should do

    for (std::vector<Enemy>::iterator it = enemies.begin(); it < enemies.end(); ++it) {
        it->update();
    }
    

    If you’re using C++11 and are lazy, you can write auto instead of std::vector<Enemy>::iterator and the compiler will figure out the type for you. (You may also want to store the value of enemies.end() outside the loop so that you don’t call the function each iteration, but this may be optimised out anyway).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.