Need some help with class

ok here goes
I am trying to use a class to get an int from another class as one of the class is remade due to it being used on a using Multiple Forms. been trying to work this out for a good 3-4 days and looked all around the internet for help but i have given up i just can not work this out my self therefore this post, any help would be great. here is the code:

Form1.hMDI main form
[CODE]#pragma once

#include "Settings.h"
#include "plane.h"
#include "opt.h"

namespace MyProject
 private:
  Settings *dos;

 public:
  Form1(void)
  {
   InitializeComponent();
   dos = new Settings;
   optObject = new opt;

  }
 

[/CODE]

Settings Child in Main MDI this is what i want all the settings to be input into.Settings.h
[CODE]#pragma once
#include "opt.h"


namespace MyProject
{
  public __gc class Settings : public System::Windows::Forms::Form
 {
 public:
  Settings(void)
  {
   InitializeComponent();
   optObject = new opt;
   
  }
        
  private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
    {
     int SS;
     SS = Convert::ToInt32(seats->Text);
     ii = optObject->People(SS);
     seats->Text = ii.ToString();
    }

 };
}[/CODE]

This is the plane window that I want to grab all Vars from the settings screen. Plane.h
[CODE]#pragma once
#include "opt.h"

namespace MyProject
{
 public __gc class plane : public System::Windows::Forms::Form
 {
 public:
  plane(void)
  {
   InitializeComponent();
   optObject = new opt;
  }
        
  private: System::Void plane_Load(System::Object *  sender, System::EventArgs *  e)
    {
     int ii;
     ii = 0;
     ii = optObject->People(ii);
     seats->Text = ii.ToString();
    }

private: System::Void textBox5_TextChanged(System::Object *  sender, System::EventArgs *  e)
   {
   }

};
}[/CODE]

----------------------------------------------------------------------------Class  

opt.h Class
[CODE]#pragma once

class opt
{
  private:
    int num;
  public:
    void set_num (int a)
      {num=a;}
    friend class backend; 
 //
int People (int);
 void backend::convert (opt a)
{
  num = a.num;
}
  
 int pp;
 opt(void);
 virtual ~opt(void);
  
};[/CODE]

opt.cpp
[CODE]#include "StdAfx.h"
#include ".\opt.h"
#using <mscorlib.dll>

opt::opt(void)
{
}
int opt::People(int number)
{// seats on plane
pp = 6;
return pp;
}

opt::~opt(void)
{
}[/CODE]

backend.hTo store the data
[CODE]#pragma once
class opt;///calls
class backend
{
 int num;
public:
int area ()
{return (num);}
void convert (opt a);
int HoldPeople(int);
backend(void);

virtual ~backend(void);
}; [/CODE]

backend.cpp
[CODE]#include "StdAfx.h"
#include ".\backend.h"
#using <mscorlib.dll>

int backend::HoldPeople(int number)
{// seats on plane

return num;
}
backend::~backend(void)
{
}[/CODE]
I am trying to do Friends of class but if there is a easier way i am open to all routes could you explain with no geometry in it plz.

I just want to make Backend class a freind off opt class so that Backend can store all the settings and then opt class as it loads will grab all the int etc from the backend.
Thanks From MiCR0



Answer this question

Need some help with class

  • Herald Smit

    // int opt::num = ii;

    The error you're getting is correct. This is the right syntax to initialise a static variable, but if you use it again, you're trying to reinitialise it. Drop the int part. opt::num = ii; will do it for you. That way you use an existing variable, instead of redeclaring it's initialisation.



  • MrGTI

    static link as the main class would not remake the class as new forms are used therefore all the variables set will be the same thought out the program if this is so then that is all i need
  • sam_jeba

    ok i looked up staic class and did what u said but when trying to set the

    int opt::num = 0;

    at the start of the program works great eveythink is 0 when i amek new forms using that class but....

    i go to try and use

    int opt::num = ii; and i get all this:

    opt::num definition or redeclaration illegal in current scop
    int opt::num : symbol cannot be defined within namepace Myproject
    num : redefintion mulitple initalization

    any idea


  • kaushik saha

    thank you very very much i got it to work !!
    programming all night until 8am does things like that to you :P
    makes me look a fool but nonethink new there. well here is what i used in the end

    private: System::Void button1_Click(System::Object * sender, System::EventArgs * e)

    {
    int SS;
    SS = Convert::ToInt32(seats->Text);
    opt::num = SS;
    SS = optObject->People(SS);
    seats->Text = SS.ToString();
    }
    Big thanks cgraus for your time :)


  • Jamie Plenderleith

    No worries, glad to help.



  • Andreas6483

    There sure is a lot of code in your post. I'm sorry, you'll need to condense it if you want most people to read it.

    //I just want to make Backend class a freind off opt class so that Backend can store all the settings and then opt class as it loads will grab all the int etc from the backend.

    For this to work, your other class needs to hold an instance of the backend class, or you need to make the variables static, which means there can't be more than one version of the values within your class.



  • Need some help with class