Solved

beginner: get string/int/float from other class

Posted on 2000-04-27
7
175 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

813 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

11 Experts available now in Live!

Get 1:1 Help Now