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

  • Home
  • SEARCH
  • 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 8820883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:43:07+00:00 2026-06-14T05:43:07+00:00

So I need my objects to be able to use functions that are within

  • 0

So I need my objects to be able to use functions that are within the main class, so when they are created I want them to get it through their parameters.

int main(int argc, char* args[])
    {
    Unit units[3]={{5,5,this},{8,8,this},{12,12,this}};
    units[0].init(true);
    for (int i=1;i<sizeof(units) / sizeof(units[0]);i++) {
        units[i].init(false);
    }

Where i put “this” it should be the main class, this is how I would do it in java but I am unsure how to do it here. I tried “*this” and “this” but all I get is an error:
Invalid use of ‘this’ in non-member function.
Searching for the error gave me nothing to work with since I am rather unknowing on c++’s class systems.

The two first parameters are for the location. The Init commands parameter sets whether they are allies or not.

I want the Unit classes to be able to access:

int getClosestHostileX(int ask_x,int ask_y,bool team) {
    return 55;
}

There is supposed to be more code here later I am just trying to get them to return.

I am using Code::Blocks IDE and GNU GCC compiler.

TL;DR How do I make my other classes access functions from my main class?

  • 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-06-14T05:43:08+00:00Added an answer on June 14, 2026 at 5:43 am

    You couldn’t do that in Java, either. In Java, the entrypoint is a static method and has no associated object instance.

    The solution is the same — instantiate your type.

    int main(int argc, char** argv)
    {
       MainClass main_object; // creates an instance
       Unit units[3]={{5,5,&main_object},{8,8,&main_object},{12,12,&main_object}};
       units[0].init(true);
       for (int i=1;i<sizeof(units) / sizeof(units[0]);i++) {
          units[i].init(false);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of DateTime objects and need to use them as strings
I have a list of pointers that reference time objects that need a turn
I need to build up a List<object> and cache the list and be able
In javascript if I need objects each to have an individual value of an
I'm moving away from Serializable objects (need to cache and move things around -
I am writing an application (a multiplayer game) where objects need to be passed
I need to write objects, sent to me from another process, to several files
I need to save objects - instances of classes - in my app like
I need to pass objects in ajax request in order to "PUT" files or
I have two objects I need to create a relationship between in Core Data.

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.