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 6646837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:27:58+00:00 2026-05-26T00:27:58+00:00

I’m learning c++. I have written a small program which should compute the energy

  • 0

I’m learning c++. I have written a small program which should compute the energy
of a N particle system. Up to now I have three small files:

data.h:

    class Particle {                                                                                                                      
        public:                                                                                                                       
        double mass;                                                                                                                  
        double charge;                                                                                                                
        double posx,posy,posz;                                                                                                        
};                                                                                                                                    

        Particle part[2];  

main.cpp:

#include <iostream>                                                                                                                   
#include "data.h"                                                                                                                     
using namespace std;                                                                                                                  

double energy(Particle part );                                                                                                        

int main ()                                                                                                                           
{                                                                                                                                     
        double sd;                                                                                                                    
        part[0].mass = 10.0;                                                                                                          
        part[4].mass = 90.0;                                                                                                          
        cout << part[0].mass << "\n";                                                                                                 
        cout << part[4].mass << "\n";                                                                                                 

        sd = energy(part);                                                                                                            
        cout << "sd" << sd <<  "\n" ;                                                                                                 
        return 0;                                                                                                                     
}    

energy.cpp:

#include <iostream>                                                                                                                   
using namespace std;                                                                                                                  

double energy(Particle part)                                                                                                          
{                                                                                                                                     
        cout << part[0].mass << "\n";                                                                                                 
        double dummy;                                                                                                                 
        dummy = 2.0;                                                                                                                  
        return (dummy);                                                                                                               
}   

I have two questions:

1)I want to make visible the Class particle in the function “energy”. In other words,
I want to use the variables of the class function (with the values given in “main”)
in the energy function.
I have tried as you see energy(Particle part) but it seems Particle is not defined
in that scope.

2)As you see in “data.h” I declared “part” as an array with two members. However,
in “main” I can use more than two members, for instance part[3],part[4]… Why I
could use more members than those I declared?

I am compiling with g++ -o test energy.cpp main.cpp

thanks.

  • 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-26T00:27:58+00:00Added an answer on May 26, 2026 at 12:27 am

    1)I want to make visible the Class particle in the function “energy”. In other words, I want to use the variables of the class function (with the values given in “main”) in the energy function. I have tried as you see energy(Particle part) but it seems Particle is not defined in that scope.

    If I understand you right.. You want to have

    Particle part[2];
    

    to be use able in main.cpp and in energy.cpp ?
    If yes.. change this to:

    extern Particle part[2];
    

    and in energy.cpp add this:

    #include "data.h"
    Particle part[2];
    

    and you will be able to use

    double energy()                                                                                                          
    {              
            //main.cpp will have same part                                                                                                                       
            cout << part[0].mass << "\n";                                                                                                 
            double dummy;                                                                                                                 
            dummy = 2.0;                                                                                                                  
            return (dummy);                                                                                                               
    }
    

    2)As you see in “data.h” I declared “part” as an array with two members. However, in “main” I can use more than two members, for instance part[3],part[4]… Why I could use more members than those I declared?

    Because it’s C/C++ ? no range checks. You can do what ever you want.
    But if you do, the result will be unexpected.

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

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
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

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.