Solved

beginner: get string/int/float from other class

Posted on 2000-04-27
7
176 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
ID: 2757328
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
ID: 2757634
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
ID: 2758312
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 31

Accepted Solution

by:
Zoppo earned 30 total points
ID: 2758768
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
ID: 2759220
ZOPPO's right on all counts!
0
 

Author Comment

by:peter_b
ID: 2759391
thanks !
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 2759401
you're welcome...

thanks for compliment, GlennDean...

regards,

ZOPPO
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PDF library for Delphi 2 128
What is sub-make ? 2 71
Copy output image from TWindowsMediaPlayer 6 48
draw a Christmas tree by using a nested loop? 26 77
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

860 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