Solved

beginner: get string/int/float from other class

Posted on 2000-04-27
7
173 Views
Last Modified: 2010-04-02
win32/VC++6/beginner/stuck

in class
Chart2.ccp :
float ppdata [] = {
003.0f, 096.9f, 031.7f, 086.5f,
101.2f, 069.8f, 210.6f, 34.5f,
089.4f, 086.5f, 077.9f, 069.6f,
095.1f, 015.8f, 084.5f, 100.7f
};


in class w1Dlg.cpp
big need of those floats.
trying in w1Dlg.cpp:
float newppdata;
newppdata = chart2.ppdata[0];
but compiler is saying that ppdata is no element of CChart2.

if you want to help me please write some lines of accessing other classes string/int/float

thanks
0
Comment
Question by:peter_b
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Expert Comment

by:GlennDean
Comment Utility
Some clarification questions:
Do you have a something like
class Chart2
{
float ppdata[];
....
};
 
Somewhere in your code you instantiate an object like
Chart2 chart2;

Then you want to access chart2.ppdata[0];

   Glenn
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
We need to see your real code. and the exact error message.  You shoudl be able to copy and paste them into the text box.
0
 

Author Comment

by:peter_b
Comment Utility
w1dlg.cpp:

void CW1Dlg::SaveProfile()
{
      CWinApp* app = AfxGetApp();
      app->WriteProfileInt("profile", "COM", comCombo.GetCurSel());
      app->WriteProfileInt("profile", "phone", phoneCombo.GetCurSel());
      app->WriteProfileInt("profile", "pulse", pulseCheck.GetCheck() & 1);
      app->WriteProfileString("profile", "prefix",m_strMessage);



      char newdata[6];
      float newppdata;
      for (int i = 0; i < 15; i++)
      {
            newppdata = chart2.ppdata[i];
            sprintf(newdata,"%f",ppdata[i]);
            app->WriteProfileString("profile","winds[i]", newdata);
            printf(newdata);
      }
      


}


chart2.cpp

// Chart2.cpp : implementation file
//

#include "stdafx.h"
#include "w1.h"
#include "Chart2.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChart2

CChart2::CChart2()
{
      yAxisTicks = 15;
      X1 = 40;
      X2 = 520;
      Y1 = 40;
      Y2 = 320;
      height;
      arrowMainOffset = 3;
      arrowMidOffset = 5;
      lineWidth = 4;
      maxValue = 0;
      
}

CChart2::~CChart2()
{
}


BEGIN_MESSAGE_MAP(CChart2, CStatic)
      //{{AFX_MSG_MAP(CChart2)
      ON_WM_PAINT()
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()

float ppdata [] = {
003.0f, 096.9f, 031.7f, 086.5f,
101.2f, 069.8f, 210.6f, 34.5f,
089.4f, 086.5f, 077.9f, 069.6f,
095.1f, 015.8f, 084.5f, 100.7f
};
....
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Accepted Solution

by:
Zoppo earned 30 total points
Comment Utility
Hi peter_b,

you declared the ppdata as a global variable, not as member of a class. Now we just would need to know what you need this for (do you want to use this array for all instances of the class or one array for each instantiated object of the class)

If you need only one array for all instances of the class you should declare it as static member of the class like this:

// in .h file
class CChart2
{
 ...
 static float ppdata[];
 ...
}

// in .cpp file
float CChart2::ppdata [] = {
         003.0f, 096.9f, 031.7f, 086.5f,
         101.2f, 069.8f, 210.6f, 34.5f,
         089.4f, 086.5f, 077.9f, 069.6f,
         095.1f, 015.8f, 084.5f, 100.7f
         };

so you can access it in two way like this:

void CW1Dlg::SaveProfile()
{
....
for (int i = 0; i < 15; i++)
{
 newppdata = chart2.ppdata[i]; // via the object
 // or
 newppdata = CChart2::ppdate[i]; // via the class
....
}
....
}


If you need one array for each instantiated object of this class the way to do it depends on what exactly you need. You can either use fixed-sized or dynamic arrays.


At last, as your code implies you need only one global array, there may be no need to hold ppdata as a member. Just declare it as you did as global (at file scope) declared array and declare it as extern wherever you need it like this:

// in chart2.cpp
float ppdata [] = {
         003.0f, 096.9f, 031.7f, 086.5f,
         101.2f, 069.8f, 210.6f, 34.5f,
         089.4f, 086.5f, 077.9f, 069.6f,
         095.1f, 015.8f, 084.5f, 100.7f
         };

// in w1dlg.cpp:
extern float ppdata[];
void CW1Dlg::SaveProfile()
{
 ...
 for (int i = 0; i < 15; i++)
 {
  newppdata = ppdata[i];
  ...
 }
 ...
}


hope that helps,

ZOPPO
0
 
LVL 3

Expert Comment

by:GlennDean
Comment Utility
ZOPPO's right on all counts!
0
 

Author Comment

by:peter_b
Comment Utility
thanks !
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
you're welcome...

thanks for compliment, GlennDean...

regards,

ZOPPO
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now